systemlynx 1.12.8 → 1.12.10
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.
- package/client.js +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/systemlynx/App/components/loadModules.js +1 -0
- package/systemlynx/Client/components/ServiceRequestHandler.js +5 -3
- package/systemlynx/Client/components/SocketDispatcher.js +1 -1
- package/systemlynx/Client/tests/SocketDispatcher.test.js +1 -1
- package/systemlynx/Dispatcher/Dispatcher.js +6 -4
- package/systemlynx/Dispatcher/Dispatcher.test.js +1 -1
- package/systemlynx/ServerManager/ServerManager.js +1 -0
- package/systemlynx/ServerManager/components/Router.js +2 -0
- package/utils/throttle.js +19 -0
package/client.js
CHANGED
|
@@ -5,7 +5,7 @@ const createDispatcher = require("./systemlynx/Dispatcher/Dispatcher");
|
|
|
5
5
|
|
|
6
6
|
const HttpClient = createHttpClient();
|
|
7
7
|
const Client = createClient();
|
|
8
|
-
const Dispatcher = createDispatcher();
|
|
8
|
+
const Dispatcher = new createDispatcher();
|
|
9
9
|
|
|
10
10
|
module.exports = {
|
|
11
11
|
//Export these pre-created objects for convenient object destructuring
|
package/index.js
CHANGED
|
@@ -15,7 +15,7 @@ const LoadBalancer = isNode ? createLoadBalancer() : null;
|
|
|
15
15
|
const App = createApp();
|
|
16
16
|
const HttpClient = createHttpClient();
|
|
17
17
|
const Client = createClient();
|
|
18
|
-
const Dispatcher = createDispatcher();
|
|
18
|
+
const Dispatcher = new createDispatcher();
|
|
19
19
|
|
|
20
20
|
module.exports = {
|
|
21
21
|
//Export these pre-created objects for convenient object destructuring
|
package/package.json
CHANGED
|
@@ -63,9 +63,11 @@ module.exports = function ServiceRequestHandler(
|
|
|
63
63
|
};
|
|
64
64
|
|
|
65
65
|
const ErrorHandler = (err, errCount, cb) => {
|
|
66
|
-
if (!err.isAxiosError)
|
|
67
|
-
|
|
68
|
-
|
|
66
|
+
if (!err.isAxiosError) throw err;
|
|
67
|
+
if (!err.response) throw err;
|
|
68
|
+
if (!err.response.data) throw err;
|
|
69
|
+
|
|
70
|
+
if (err.response.data.SystemLynxService) {
|
|
69
71
|
cb(err.response.data);
|
|
70
72
|
} else if (errCount <= 3) {
|
|
71
73
|
errCount++;
|
|
@@ -7,7 +7,7 @@ module.exports = function SocketDispatcher(namespace, events = {}, systemContext
|
|
|
7
7
|
(this || {}).on && (this || {}).emit
|
|
8
8
|
? this
|
|
9
9
|
: createDispatcher.apply(this, [events, systemContext]);
|
|
10
|
-
const socket = io.connect(namespace);
|
|
10
|
+
const socket = io.connect(namespace, { reconnection: false });
|
|
11
11
|
socket.on("dispatch", (event) => dispatcher.emit(event.name, event.data, event));
|
|
12
12
|
socket.on("disconnect", () => {
|
|
13
13
|
socket.disconnect();
|
|
@@ -11,7 +11,7 @@ SocketServer.listen(port);
|
|
|
11
11
|
|
|
12
12
|
describe("SocketDispatcher", () => {
|
|
13
13
|
const eventName = "test-event";
|
|
14
|
-
const dispatcher = SocketDispatcher(`http://localhost:${port}/${namespace}`);
|
|
14
|
+
const dispatcher = new SocketDispatcher(`http://localhost:${port}/${namespace}`);
|
|
15
15
|
it("should return an EventDispatcher object with methods on and emit", async () => {
|
|
16
16
|
expect(dispatcher)
|
|
17
17
|
.to.be.an("object")
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
const throttle = require("../../utils/throttle");
|
|
2
|
+
|
|
2
3
|
module.exports = function createDispatcher(events = {}, systemContext) {
|
|
3
4
|
const Dispatcher = this || {};
|
|
4
5
|
|
|
@@ -10,12 +11,13 @@ module.exports = function createDispatcher(events = {}, systemContext) {
|
|
|
10
11
|
return Dispatcher;
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
Dispatcher.on = (eventName, callback) => {
|
|
14
|
+
Dispatcher.on = (eventName, callback, { limit, interval } = {}) => {
|
|
14
15
|
if (typeof callback !== "function") return Dispatcher;
|
|
15
|
-
|
|
16
|
+
const name = callback.name;
|
|
17
|
+
if (typeof interval === "number") callback = throttle(callback, limit, interval);
|
|
16
18
|
if (!events[eventName]) events[eventName] = [];
|
|
17
19
|
|
|
18
|
-
if (
|
|
20
|
+
if (name) {
|
|
19
21
|
//if the function has a name and it already present don't add it
|
|
20
22
|
const i = events[eventName].findIndex((fn) => fn.name === callback.name);
|
|
21
23
|
if (i === -1) events[eventName].push(callback);
|
|
@@ -2,7 +2,7 @@ const { expect } = require("chai");
|
|
|
2
2
|
const Dispatcher = require("./Dispatcher");
|
|
3
3
|
|
|
4
4
|
describe("createDispatcher", () => {
|
|
5
|
-
const dispatcher = Dispatcher();
|
|
5
|
+
const dispatcher = new Dispatcher();
|
|
6
6
|
it("should return an EventDispatcher object with methods on and emit", async () => {
|
|
7
7
|
expect(dispatcher)
|
|
8
8
|
.to.be.an("object")
|
|
@@ -146,6 +146,7 @@ module.exports = function createServerManager(customServer, customWebSocketServe
|
|
|
146
146
|
});
|
|
147
147
|
|
|
148
148
|
function addMiddleware(middleware) {
|
|
149
|
+
if (Array.isArray(middleware)) return middleware.map(addMiddleware);
|
|
149
150
|
if (!serverConfigurations.validators[name])
|
|
150
151
|
serverConfigurations.validators[name] = [];
|
|
151
152
|
serverConfigurations.validators[name].push(async function (req, res, next) {
|
|
@@ -11,6 +11,7 @@ module.exports = function createRouter(server, config) {
|
|
|
11
11
|
req.module_name = module_name;
|
|
12
12
|
req.fn = fn;
|
|
13
13
|
req.Module = Module;
|
|
14
|
+
req.module = Module;
|
|
14
15
|
next();
|
|
15
16
|
},
|
|
16
17
|
setHelpers,
|
|
@@ -26,6 +27,7 @@ module.exports = function createRouter(server, config) {
|
|
|
26
27
|
req.module_name = module_name;
|
|
27
28
|
req.fn = method;
|
|
28
29
|
req.Module = Module;
|
|
30
|
+
req.module = Module;
|
|
29
31
|
next();
|
|
30
32
|
},
|
|
31
33
|
setHelpers,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module.exports = function throttleByCount(func, limit = 1, interval) {
|
|
2
|
+
let callCount = 0;
|
|
3
|
+
let resetTimer;
|
|
4
|
+
|
|
5
|
+
return function (...args) {
|
|
6
|
+
callCount++;
|
|
7
|
+
|
|
8
|
+
if (callCount <= limit) {
|
|
9
|
+
func.apply(this, args);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!resetTimer) {
|
|
13
|
+
resetTimer = setTimeout(() => {
|
|
14
|
+
callCount = 0;
|
|
15
|
+
resetTimer = null;
|
|
16
|
+
}, interval);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|