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
4 changes: 2 additions & 2 deletions client/Event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ interface InitOptions {
protocol: string;
}
type Callback<T = any> = (payload: T) => void;
declare const nodeEvent: {
declare const event: {
init({ host, port, protocol }: InitOptions): void;
subscribe<T = any>(type: string, callback: Callback<T>): () => void;
publish<T = any>(...args: [...string[], T]): void;
};
export { nodeEvent };
export { event };
10 changes: 5 additions & 5 deletions client/Event.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeEvent = void 0;
exports.event = void 0;
var socket_io_client_1 = require("socket.io-client");
var socket = null;
var callbacks = {};
var nodeEvent = {
var event = {
init: function (_a) {
var host = _a.host, port = _a.port, protocol = _a.protocol;
if (socket)
Expand All @@ -19,7 +19,7 @@ var nodeEvent = {
},
subscribe: function (type, callback) {
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");
throw new Error("Event not initialized. Call event.init first.");
if (!callbacks[type])
callbacks[type] = new Set();
callbacks[type].add(callback);
Expand All @@ -38,7 +38,7 @@ var nodeEvent = {
args[_i] = arguments[_i];
}
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");
throw new Error("Event not initialized. Call event.init first.");
if (args.length < 2) {
throw new Error("publish requires at least one event type and a payload");
}
Expand All @@ -50,4 +50,4 @@ var nodeEvent = {
});
},
};
exports.nodeEvent = nodeEvent;
exports.event = event;
8 changes: 4 additions & 4 deletions client/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface InitOptions {
}
type Callback<T = any> = (payload: T) => void;

const nodeEvent = {
const event = {
init({ host, port, protocol }: InitOptions) {
if (socket) return;
socket = io(`${protocol}://${host}:${port}`);
Expand All @@ -23,7 +23,7 @@ const nodeEvent = {

subscribe<T = any>(type: string, callback: Callback<T>): () => void {
if (!socket)
throw new Error("nodeEvent not initialized. Call nodeEvent.init first.");
throw new Error("Event not initialized. Call event.init first.");
if (!callbacks[type]) callbacks[type] = new Set();
callbacks[type].add(callback as Callback);
socket!.emit("subscribe", type);
Expand All @@ -38,7 +38,7 @@ const nodeEvent = {

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

if (args.length < 2) {
throw new Error("publish requires at least one event type and a payload");
Expand All @@ -54,4 +54,4 @@ const nodeEvent = {
},
};

export { nodeEvent };
export { event };
6 changes: 3 additions & 3 deletions sample/publisher/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const express = require("express");
const { nodeEvent } = require("nuc-node-event-test/client");
const { event } = require("@nucleoidai/node-event/client");

const app = express();
app.use(express.json());

nodeEvent.init({ host: "localhost", port: 8080 });
event.init({ host: "localhost", port: 8080 });

app.post("/send", (req, res) => {
const { type = "test", payload = {} } = req.body;
nodeEvent.publish(type, payload);
event.publish(type, payload);
res.json({ status: "sent", type, payload });
});

Expand Down
46 changes: 23 additions & 23 deletions sample/publisher/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sample/publisher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.2",
"nuc-node-event-test": "^1.0.13"
"@nucleoidai/node-event": "^1.1.1",
"express": "^4.18.2"
}
}
6 changes: 3 additions & 3 deletions sample/subscriber/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const express = require('express');
const { nodeEvent } = require('nuc-node-event-test/client');
const { event } = require('@nucleoidai/node-event/client');

const app = express();

nodeEvent.init({ host: 'localhost', port: 8080 });
event.init({ host: 'localhost', port: 8080 });

nodeEvent.subscribe('test', (payload) => {
event.subscribe('test', (payload) => {
console.log('Received event:', payload);
});

Expand Down
46 changes: 23 additions & 23 deletions sample/subscriber/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions sample/subscriber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"start": "node index.js"
},
"dependencies": {
"express": "^4.18.2",
"nuc-node-event-test": "^1.0.13"
"@nucleoidai/node-event": "^1.1.1",
"express": "^4.18.2"
}
}