Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions client/Event.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
interface InitOptions {
host: string;
port: number;
protocol: string;
}
type Callback<T = any> = (payload: T) => void;
declare const nodeEvent: {
init({ host, port, protocol }: InitOptions): void;
subscribe<T = any>(type: string, callback: Callback<T>): () => void;
publish<T = any>(...args: [...string[], T]): void;
};
export { nodeEvent };
53 changes: 53 additions & 0 deletions client/Event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeEvent = void 0;
var socket_io_client_1 = require("socket.io-client");
var socket = null;
var callbacks = {};
var nodeEvent = {
init: function (_a) {
var host = _a.host, port = _a.port, protocol = _a.protocol;
if (socket)
return;
socket = (0, socket_io_client_1.io)("".concat(protocol, "://").concat(host, ":").concat(port));
socket.on("event", function (_a) {
var type = _a.type, payload = _a.payload;
if (callbacks[type]) {
callbacks[type].forEach(function (cb) { return cb(payload); });
}
});
},
subscribe: function (type, callback) {
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");
if (!callbacks[type])
callbacks[type] = new Set();
callbacks[type].add(callback);
socket.emit("subscribe", type);
return function () {
callbacks[type].delete(callback);
if (callbacks[type].size === 0) {
delete callbacks[type];
socket.emit("unsubscribe", type);
}
};
},
publish: function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");
if (args.length < 2) {
throw new Error("publish requires at least one event type and a payload");
}
var payload = args[args.length - 1];
var types = args.slice(0, -1);
// Publish to all specified event types
types.forEach(function (type) {
socket.emit("publish", { type: type, payload: payload });
});
},
};
exports.nodeEvent = nodeEvent;
57 changes: 57 additions & 0 deletions client/Event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Socket, io } from "socket.io-client";

let socket: Socket | null = null;
const callbacks: Record<string, Set<Callback>> = {};

interface InitOptions {
host: string;
port: number;
protocol: string;
}
type Callback<T = any> = (payload: T) => void;

const nodeEvent = {
init({ host, port, protocol }: InitOptions) {
if (socket) return;
socket = io(`${protocol}://${host}:${port}`);
socket.on("event", ({ type, payload }: { type: string; payload: any }) => {
if (callbacks[type]) {
callbacks[type].forEach((cb) => cb(payload));
}
});
},

subscribe<T = any>(type: string, callback: Callback<T>): () => void {
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");
if (!callbacks[type]) callbacks[type] = new Set();
callbacks[type].add(callback as Callback);
socket!.emit("subscribe", type);
return () => {
callbacks[type].delete(callback as Callback);
if (callbacks[type].size === 0) {
delete callbacks[type];
socket!.emit("unsubscribe", type);
}
};
},

publish<T = any>(...args: [...string[], T]): void {
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");

if (args.length < 2) {
throw new Error("publish requires at least one event type and a payload");
}

const payload = args[args.length - 1];
const types = args.slice(0, -1) as string[];

// Publish to all specified event types
types.forEach(type => {
socket!.emit("publish", { type, payload });
});
},
};

export { nodeEvent };
Loading