bagman
Version:
js/ts client for Bagman
93 lines (92 loc) • 3.74 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Channel = void 0;
/**
* A class representing a Channel for the Bagman client.
*/
class Channel {
/**
* Creates a new instance of Channel.
*
* @param socket - The socket.io client socket to use for communication.
* @param channel - The name of the channel to subscribe to.
*/
constructor(socket, channel) {
this.socket = socket;
this.channel = channel;
/**
* @param deactivated - Whether this channel is deactivated.
*/
this.deactivated = false;
}
/**
* Registers a callback function to be executed when the specified event is emitted.
*
* @template T - The type of data to be passed as arguments to the callback function.
* @param event - The name of the event to listen for.
* @param cb - The callback function to be executed when the specified event is emitted.
*
* @returns void
*/
listen(event, cb) {
this.socket.on(`channel:${this.channel}:${event}`, cb);
}
/**
* Fetch Presences of the current channel.
*
* @template T - the type of presence that you expected to receive
* @returns - list of presences that's within this channel
*/
presences() {
return __awaiter(this, void 0, void 0, function* () {
const ack = yield this.socket.emitWithAck(`presence:fetch`, { channel: this.channel });
if ("status" in ack) {
throw new Error(ack.message);
}
return ack.presences;
});
}
/**
* Publishes an event to the channel.
*
* @template Data - The type of data to be passed along with the event.
* @param event - The name of the event to be published.
* @param data - The data to be passed along with the event.
*
* @returns Promise<void> - A promise that resolves if the event was successfully published, and rejects if there was an error.
*/
publish(event, data) {
return __awaiter(this, void 0, void 0, function* () {
if (this.deactivated)
throw new Error(`Channel: ${this.channel} is deactivated. Please subscribe to this channel again.`);
const ack = yield this.socket.emitWithAck('client:emit', { channel: this.channel, event, data });
if (ack.status !== "ok") {
throw new Error(ack.message);
}
});
}
/**
* Unsubscribes from the channel.
*
* @returns Promise<void> - A promise that resolves if the unsubscribe was successful, and rejects if there was an error.
*/
unsubscribe() {
return __awaiter(this, void 0, void 0, function* () {
this.deactivated = true;
const ack = yield this.socket.emitWithAck('client:unsubscribe', { channel: this.channel });
if (ack.status !== "ok") {
throw new Error(ack.message);
}
});
}
}
exports.Channel = Channel;