not-node 6.1.9 → 6.2.1

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/index.js CHANGED
@@ -54,3 +54,9 @@ module.exports.Generic = require("./src/generic/index.js");
54
54
  module.exports.notMetasStyler = require("./src/metas.js");
55
55
  //for http headers files, web pages, XHR etc
56
56
  module.exports.notHeadersStyler = require("./src/headers.js");
57
+
58
+ /** Interconnect between instances */
59
+ //static interface
60
+ module.exports.notCluster = require("./src/cluster");
61
+ //static provider
62
+ module.exports.notClusterRedisProvider = require("./src/cluster/cluster.redis");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.1.9",
3
+ "version": "6.2.1",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,120 @@
1
+ const notEnv = require("../env.js");
2
+ const notCommon = require("../common");
3
+ const { error } = require("not-log")(module, "ClusterRedis");
4
+
5
+ module.exports = class notClusterRedisProvider {
6
+ static #clientGetter = null;
7
+ static #clientName = "db.redis";
8
+ static #subscriber = null;
9
+ static #publisher = null;
10
+
11
+ static async setClientName(name) {
12
+ try {
13
+ this.#clientName = name;
14
+ await this.#resetClient();
15
+ } catch (e) {
16
+ error(e);
17
+ }
18
+ }
19
+
20
+ static async setClientGetter(getter) {
21
+ try {
22
+ this.#clientGetter = getter;
23
+ await this.#resetClient();
24
+ } catch (e) {
25
+ error(e);
26
+ }
27
+ }
28
+
29
+ static async #resetClient() {
30
+ try {
31
+ if (this.#publisher) {
32
+ await this.#publisher.disconnect();
33
+ this.#publisher = null;
34
+ }
35
+ if (this.#subscriber) {
36
+ await this.#subscriber.disconnect();
37
+ this.#subscriber = null;
38
+ }
39
+ } catch (e) {
40
+ error(e);
41
+ }
42
+ }
43
+
44
+ static #getClient() {
45
+ try {
46
+ if (this.#clientGetter) {
47
+ return this.#clientGetter();
48
+ } else {
49
+ return notEnv.getEnv(this.#clientName);
50
+ }
51
+ } catch (e) {
52
+ error(e);
53
+ }
54
+ }
55
+
56
+ static async #getPublisher() {
57
+ try {
58
+ if (!this.#publisher) {
59
+ this.#publisher = this.#getClient().duplicate();
60
+ await this.#publisher.connect();
61
+ }
62
+ return this.#publisher;
63
+ } catch (e) {
64
+ error(e);
65
+ }
66
+ }
67
+
68
+ static async #getSubscriber() {
69
+ try {
70
+ if (!this.#subscriber) {
71
+ this.#subscriber = this.#getClient().duplicate();
72
+ await this.#subscriber.connect();
73
+ }
74
+ return this.#subscriber;
75
+ } catch (e) {
76
+ error(e);
77
+ }
78
+ }
79
+
80
+ static #createListener(func) {
81
+ return async (message) => {
82
+ try {
83
+ await notCommon.executeFunctionAsAsync(func, [
84
+ JSON.parse(message),
85
+ ]);
86
+ } catch (e) {
87
+ error(e);
88
+ }
89
+ };
90
+ }
91
+
92
+ static async on(eventName, func) {
93
+ try {
94
+ const listener = this.#createListener(func);
95
+ const subscriber = await this.#getSubscriber();
96
+ await subscriber.subscribe(eventName, listener);
97
+ return listener;
98
+ } catch (e) {
99
+ error(e);
100
+ }
101
+ }
102
+
103
+ static async off(eventName, listener) {
104
+ try {
105
+ const subscriber = await this.#getSubscriber();
106
+ return subscriber.unsubscribe(eventName, listener);
107
+ } catch (e) {
108
+ error(e);
109
+ }
110
+ }
111
+
112
+ static async emit(eventName, message) {
113
+ try {
114
+ const publisher = await this.#getPublisher();
115
+ return publisher.publish(eventName, JSON.stringify(message));
116
+ } catch (e) {
117
+ error(e);
118
+ }
119
+ }
120
+ };
@@ -0,0 +1,25 @@
1
+ const { log } = require("not-log")(module, "notCluster");
2
+ const notClusterRedis = require("./cluster.redis.js");
3
+
4
+ module.exports = class notCluster {
5
+ static #provider = notClusterRedis;
6
+
7
+ static setProvider(newProvider) {
8
+ log(
9
+ `Setting new cluster messaging provider: ${newProvider.prototype.constructor.name}`
10
+ );
11
+ this.#provider = newProvider;
12
+ }
13
+
14
+ static on() {
15
+ return this.#provider.on(...arguments);
16
+ }
17
+
18
+ static off() {
19
+ return this.#provider.off(...arguments);
20
+ }
21
+
22
+ static emit() {
23
+ return this.#provider.emit(...arguments);
24
+ }
25
+ };
@@ -180,3 +180,27 @@ function getMutationForField(name, list) {
180
180
  return false;
181
181
  }
182
182
  module.exports.getMutationForField = getMutationForField;
183
+
184
+ function mutateFieldSide(field, mutation, side) {
185
+ if (Object.hasOwn(mutation, side)) {
186
+ if (Object.hasOwn(field, side)) {
187
+ field[side] = { ...field[side], ...mutation[side] };
188
+ } else {
189
+ field[side] = mutation[side];
190
+ }
191
+ }
192
+ }
193
+
194
+ module.exports.mutateFieldSide = mutateFieldSide;
195
+
196
+ function mutateField(sourceField, sourceMutation) {
197
+ const sides = ["ui", " model"];
198
+ let field = structuredClone(sourceField);
199
+ let mutation = structuredClone(sourceMutation);
200
+ if (mutation) {
201
+ sides.forEach((side) => mutateFieldSide(field, mutation, side));
202
+ }
203
+ return field;
204
+ }
205
+
206
+ module.exports.mutateField = mutateField;