systemlynx 1.19.12 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,10 @@ const socket = WebSocket.of(route);
11
11
  socket.on("connect", ({ id }) => {
12
12
  console.log(`socket connected with id:${id}`);
13
13
  });
14
+ socket.on("connection", (clientSocket) => {
15
+ clientSocket.on("subscribe", (name) => clientSocket.join(name));
16
+ clientSocket.on("unsubscribe", (name) => clientSocket.leave(name));
17
+ });
14
18
  SocketServer.listen(port);
15
19
 
16
20
  describe("SocketDispatcher", () => {
@@ -19,7 +23,7 @@ describe("SocketDispatcher", () => {
19
23
  it("should return an EventDispatcher object with methods on and emit", async () => {
20
24
  expect(dispatcher)
21
25
  .to.be.an("object")
22
- .that.has.all.keys("on", "emit", "$clearEvent", "disconnect")
26
+ .that.has.all.keys("on", "once", "emit", "$clearEvent", "destroy", "disconnect")
23
27
  .that.respondsTo("on")
24
28
  .that.respondsTo("emit")
25
29
  .that.respondsTo("$clearEvent")
@@ -32,7 +36,7 @@ describe("SocketDispatcher", () => {
32
36
  });
33
37
  dispatcher.on("connect", () => console.log(`I'm all the way connected!`));
34
38
  setTimeout(
35
- () => socket.emit("dispatch", { name: eventName, data: { testPassed: true } }),
39
+ () => socket.to(eventName).emit(eventName, { id: "test-id", data: { testPassed: true }, type: "WebSocket" }),
36
40
  500
37
41
  );
38
42
  });
@@ -44,7 +48,7 @@ describe("SocketDispatcher.apply()", () => {
44
48
  it("should return an EventDispatcher object with methods on and emit", async () => {
45
49
  expect(dispatcher)
46
50
  .to.be.an("object")
47
- .that.has.all.keys("on", "emit", "$clearEvent", "disconnect")
51
+ .that.has.all.keys("on", "once", "emit", "$clearEvent", "destroy", "disconnect")
48
52
  .that.respondsTo("on")
49
53
  .that.respondsTo("emit")
50
54
  .that.respondsTo("$clearEvent")
@@ -53,14 +57,16 @@ describe("SocketDispatcher.apply()", () => {
53
57
  it("Should be able to emit and handle events", (done) => {
54
58
  dispatcher.on(eventName, (data, event) => {
55
59
  expect(data).to.deep.equal({ testPassed: true });
56
- expect(event).to.deep.equal({ name: "testing-event", data: { testPassed: true } });
57
- //console.log(event);
60
+ expect(event).to.be.an("object").that.has.all.keys("id", "name", "data", "type");
61
+ expect(event.name).to.equal(eventName);
62
+ expect(event.data).to.deep.equal({ testPassed: true });
63
+ expect(event.type).to.equal("WebSocket");
58
64
  console.log(`I'm all the way connected too!`);
59
65
  done();
60
66
  });
61
67
 
62
68
  setTimeout(
63
- () => socket.emit("dispatch", { name: eventName, data: { testPassed: true } }),
69
+ () => socket.to(eventName).emit(eventName, { id: "test-id", data: { testPassed: true }, type: "WebSocket" }),
64
70
  500
65
71
  );
66
72
  });
@@ -1,49 +1,93 @@
1
+ "use strict";
1
2
  const throttle = require("../../utils/throttle");
2
3
 
3
- module.exports = function createDispatcher(events = {}, systemContext) {
4
+ module.exports = function createDispatcher(_, systemContext) {
5
+ const events = new Map();
4
6
  const Dispatcher = this || {};
5
7
 
6
- Dispatcher.emit = (eventName, data, event) => {
7
- if (events[eventName])
8
- events[eventName].forEach((callback) =>
9
- callback.apply(systemContext, [data, event])
10
- );
8
+ Dispatcher.emit = function (eventName, data, event) {
9
+ const registry = events.get(eventName);
10
+ if (!registry) return Dispatcher;
11
+ for (const listener of registry.values()) {
12
+ listener(data, event);
13
+ }
11
14
  return Dispatcher;
12
15
  };
13
16
 
14
- Dispatcher.on = (eventName, callback, { limit, interval } = {}) => {
17
+ Dispatcher.on = function (eventName, callback, { limit, interval, eventId } = {}) {
15
18
  if (typeof callback !== "function") return Dispatcher;
16
- const name = callback.name;
17
- if (typeof interval === "number") callback = throttle(callback, limit, interval);
18
- if (!events[eventName]) events[eventName] = [];
19
-
20
- if (name) {
21
- //if the function has a name and it already present don't add it
22
- const i = events[eventName].findIndex((fn) => fn.name === callback.name);
23
- if (i === -1) events[eventName].push(callback);
24
- else events[eventName][i] = callback;
25
- } else events[eventName].push(callback);
26
- return Dispatcher;
19
+
20
+ const key = eventId || Symbol();
21
+ if (!events.has(eventName)) events.set(eventName, new Map());
22
+ const registry = events.get(eventName);
23
+ if (registry.has(key)) registry.delete(key);
24
+
25
+ let fn = typeof interval === "number" ? throttle(callback, limit, interval) : callback;
26
+ if (systemContext) fn = fn.bind(systemContext);
27
+ registry.set(key, fn);
28
+
29
+ return function () {
30
+ const currentRegistry = events.get(eventName);
31
+ if (!currentRegistry) return;
32
+ currentRegistry.delete(key);
33
+ if (currentRegistry.size === 0) events.delete(eventName);
34
+ };
27
35
  };
28
36
 
29
- Dispatcher.$clearEvent = (eventName, fn) => {
30
- if (!events[eventName]) return Dispatcher;
37
+ Dispatcher.once = function (eventName, callback, { limit, interval, eventId } = {}) {
38
+ if (typeof callback !== "function") return function () {};
39
+
40
+ const key = eventId || Symbol();
41
+ if (!events.has(eventName)) events.set(eventName, new Map());
42
+ const registry = events.get(eventName);
43
+ if (registry.has(key)) registry.delete(key);
44
+
45
+ const throttled =
46
+ typeof interval === "number" ? throttle(callback, limit, interval) : callback;
47
+
48
+ const boundFn = function (...args) {
49
+ registry.delete(key);
50
+ if (registry.size === 0) events.delete(eventName);
51
+ return throttled.apply(systemContext, args);
52
+ };
53
+
54
+ registry.set(key, boundFn);
55
+
56
+ return function () {
57
+ const currentRegistry = events.get(eventName);
58
+ if (!currentRegistry) return;
59
+ currentRegistry.delete(key);
60
+ if (currentRegistry.size === 0) events.delete(eventName);
61
+ };
62
+ };
63
+
64
+ Dispatcher.$clearEvent = function (eventName, fn) {
65
+ if (!events.get(eventName)) return Dispatcher;
31
66
 
32
67
  if (!fn) {
33
- // Clear all listeners for the given event
34
- delete events[eventName];
68
+ events.delete(eventName);
35
69
  } else if (typeof fn === "function") {
36
- // Remove the listener function with the specified name from the event's listener array
37
- events[eventName] = events[eventName].filter((callback) => {
38
- return callback.name !== fn.name;
39
- });
70
+ const registry = events.get(eventName);
71
+ for (const [key, listener] of registry.entries()) {
72
+ if (listener.name === fn.name) {
73
+ registry.delete(key);
74
+ break;
75
+ }
76
+ }
77
+ if (registry.size === 0) events.delete(eventName);
40
78
  } else {
41
79
  console.error(
42
- "SystemLynxError: the second parameter of the Dispatcher.$clearEvent takes the original function to the event"
80
+ "SystemLynxError: the second parameter of the Dispatcher.$clearEvent takes the original function to the event"
43
81
  );
44
82
  }
45
83
 
46
84
  return Dispatcher;
47
85
  };
86
+
87
+ Dispatcher.destroy = function () {
88
+ events.clear();
89
+ return Dispatcher;
90
+ };
91
+
48
92
  return Dispatcher;
49
93
  };
@@ -3,15 +3,18 @@ const Dispatcher = require("./Dispatcher");
3
3
 
4
4
  describe("createDispatcher", () => {
5
5
  const dispatcher = new Dispatcher();
6
- it("should return an EventDispatcher object with methods on and emit", async () => {
6
+
7
+ it("should return an EventDispatcher object with on, emit, $clearEvent, once, and destroy", () => {
7
8
  expect(dispatcher)
8
9
  .to.be.an("object")
9
- .that.has.all.keys("on", "emit", "$clearEvent")
10
10
  .that.respondsTo("on")
11
11
  .that.respondsTo("emit")
12
- .that.respondsTo("$clearEvent");
12
+ .that.respondsTo("$clearEvent")
13
+ .that.respondsTo("once")
14
+ .that.respondsTo("destroy");
13
15
  });
14
- it("Should be able to emit and handle events", (done) => {
16
+
17
+ it("should emit and handle events", (done) => {
15
18
  dispatcher.on("test", (data) => {
16
19
  expect(data).to.deep.equal({ testPassed: true });
17
20
  done();
@@ -19,4 +22,84 @@ describe("createDispatcher", () => {
19
22
 
20
23
  dispatcher.emit("test", { testPassed: true });
21
24
  });
25
+
26
+ it("on() should return an unsubscribe function that removes the listener", (done) => {
27
+ const d = new Dispatcher();
28
+ let callCount = 0;
29
+
30
+ const unsubscribe = d.on("ping", () => callCount++);
31
+ expect(unsubscribe).to.be.a("function");
32
+
33
+ d.emit("ping");
34
+ unsubscribe();
35
+ d.emit("ping");
36
+
37
+ setTimeout(() => {
38
+ expect(callCount).to.equal(1);
39
+ done();
40
+ }, 0);
41
+ });
42
+
43
+ it("eventId should replace the existing listener on re-register", (done) => {
44
+ const d = new Dispatcher();
45
+ let callCount = 0;
46
+
47
+ d.on("ping", () => callCount++, { eventId: "my-listener" });
48
+ d.on("ping", () => callCount++, { eventId: "my-listener" });
49
+
50
+ d.emit("ping");
51
+
52
+ setTimeout(() => {
53
+ expect(callCount).to.equal(1);
54
+ done();
55
+ }, 0);
56
+ });
57
+
58
+ it("once() should fire the callback only once", (done) => {
59
+ const d = new Dispatcher();
60
+ let callCount = 0;
61
+
62
+ d.once("ping", () => callCount++);
63
+ d.emit("ping");
64
+ d.emit("ping");
65
+ d.emit("ping");
66
+
67
+ setTimeout(() => {
68
+ expect(callCount).to.equal(1);
69
+ done();
70
+ }, 0);
71
+ });
72
+
73
+ it("destroy() should remove all listeners", (done) => {
74
+ const d = new Dispatcher();
75
+ let callCount = 0;
76
+
77
+ d.on("a", () => callCount++);
78
+ d.on("b", () => callCount++);
79
+ d.destroy();
80
+ d.emit("a");
81
+ d.emit("b");
82
+
83
+ setTimeout(() => {
84
+ expect(callCount).to.equal(0);
85
+ done();
86
+ }, 0);
87
+ });
88
+
89
+ it("$clearEvent should still work with a named function (backward compat)", (done) => {
90
+ const d = new Dispatcher();
91
+ let callCount = 0;
92
+
93
+ function myHandler() { callCount++; }
94
+
95
+ d.on("ping", myHandler);
96
+ d.emit("ping");
97
+ d.$clearEvent("ping", myHandler);
98
+ d.emit("ping");
99
+
100
+ setTimeout(() => {
101
+ expect(callCount).to.equal(1);
102
+ done();
103
+ }, 0);
104
+ });
22
105
  });
@@ -26,8 +26,10 @@ describe("LoadBalancer()", () => {
26
26
  .to.be.an("object")
27
27
  .that.has.all.keys(
28
28
  "on",
29
+ "once",
29
30
  "emit",
30
31
  "$clearEvent",
32
+ "destroy",
31
33
  "before",
32
34
  "after",
33
35
  "clones",
@@ -93,9 +95,11 @@ describe("LoadBalancer.clones (Module)", () => {
93
95
  .to.be.an("Object")
94
96
  .that.has.all.keys(
95
97
  "on",
98
+ "once",
96
99
  "emit",
97
100
  "$emit",
98
101
  "$clearEvent",
102
+ "destroy",
99
103
  "before",
100
104
  "after",
101
105
  "register",
@@ -34,6 +34,7 @@ module.exports = function createRouter(server, config) {
34
34
  [`/${route}`],
35
35
  (req, res, next) => {
36
36
  req.module_name = module_name;
37
+ req.moduleName = module_name;
37
38
  req.fn = method;
38
39
  req.Module = Module;
39
40
  req.module = Module;
@@ -6,13 +6,19 @@ module.exports = function SocketEmitter(namespace, WebSocket) {
6
6
  (this || {}).on && (this || {}).emit ? this : createDispatcher.apply(this);
7
7
 
8
8
  const socket = WebSocket.of(`/${namespace}`);
9
+
10
+ socket.on("connection", (clientSocket) => {
11
+ clientSocket.on("subscribe", (name) => clientSocket.join(name));
12
+ clientSocket.on("unsubscribe", (name) => clientSocket.leave(name));
13
+ });
14
+
9
15
  //use $emit to emit events locally only
10
16
  Emitter.$emit = Emitter.emit;
11
17
 
12
18
  Emitter.emit = (name, data) => {
13
19
  const id = shortid();
14
20
  const type = "WebSocket";
15
- socket.emit("dispatch", { id, name, data, type });
21
+ socket.to(name).emit(name, { id, data, type });
16
22
  //emit the same event locally
17
23
  Emitter.$emit(name, data);
18
24
  };
@@ -11,19 +11,22 @@ describe("SocketEmiiter", () => {
11
11
  SocketServer.listen(port);
12
12
  const emmiter = SocketEmiiter(namespace, WebSocket);
13
13
 
14
- setTimeout(() => {
15
- emmiter.emit(eventName, { testPassed: true });
16
- }, 500);
17
14
  const socket = io.connect(`http://localhost:${port}/${namespace}`);
18
- socket.on("dispatch", dispatch => {
19
- expect(dispatch)
15
+ socket.on("connect", () => {
16
+ console.log(`socket connected to namespace: ${namespace}`);
17
+ socket.emit("subscribe", eventName);
18
+ });
19
+ socket.on(eventName, (payload) => {
20
+ expect(payload)
20
21
  .to.be.an("object")
21
- .that.has.all.keys("id", "name", "data", "type");
22
- expect(dispatch.name).to.equal(eventName);
23
- expect(dispatch.data).to.deep.equal({ testPassed: true });
22
+ .that.has.all.keys("id", "data", "type");
23
+ expect(payload.data).to.deep.equal({ testPassed: true });
24
24
  done();
25
25
  });
26
26
  socket.on("disconnect", () => console.log("---------> disconnect"));
27
- socket.on("connect", () => console.log(`socket connected to namespace: ${namespace}`));
27
+
28
+ setTimeout(() => {
29
+ emmiter.emit(eventName, { testPassed: true });
30
+ }, 500);
28
31
  });
29
32
  });
@@ -70,7 +70,7 @@ describe("Service.module(constructor)", () => {
70
70
 
71
71
  expect(mod)
72
72
  .to.be.an("Object")
73
- .that.has.all.keys("on", "emit", "$clearEvent", "before", "after", "test", "test2")
73
+ .that.has.all.keys("on", "once", "emit", "$clearEvent", "destroy", "before", "after", "test", "test2")
74
74
  .that.respondsTo("on")
75
75
  .that.respondsTo("emit")
76
76
  .that.respondsTo("$clearEvent")
@@ -205,8 +205,10 @@ describe("Service.module(object)", () => {
205
205
  "action1",
206
206
  "action2",
207
207
  "on",
208
+ "once",
208
209
  "emit",
209
210
  "$clearEvent",
211
+ "destroy",
210
212
  "before",
211
213
  "after"
212
214
  )