prostgles-client 4.0.317 → 4.0.318
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/dist/getSubscriptionHandler.d.ts +1 -4
- package/dist/getSubscriptionHandler.d.ts.map +1 -1
- package/dist/getSubscriptionHandler.js +17 -17
- package/dist/index.js +1 -1
- package/dist/index.no-sync.js +1 -1
- package/dist/prostgles.d.ts +2 -2
- package/dist/prostgles.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -11,12 +11,9 @@ export type Subscription = CoreParams & {
|
|
|
11
11
|
unsubChannel: string;
|
|
12
12
|
reAttach: () => Promise<void>;
|
|
13
13
|
};
|
|
14
|
-
type Subscriptions = {
|
|
15
|
-
[key: string]: Subscription;
|
|
16
|
-
};
|
|
17
14
|
export declare const getSubscriptionHandler: (initOpts: Pick<InitOptions, "socket" | "onDebug">) => {
|
|
18
15
|
addSub: (dbo: any, params: CoreParams, onChange: AnyFunction, _onError?: AnyFunction) => Promise<SubscriptionHandler>;
|
|
19
|
-
subscriptions:
|
|
16
|
+
subscriptions: Map<string, Subscription>;
|
|
20
17
|
addServerSub: ({ tableName, command, param1, param2, }: CoreParams) => Promise<SubscriptionChannels>;
|
|
21
18
|
_unsubscribe: (channelName: string, unsubChannel: string, handler: AnyFunction) => Promise<true>;
|
|
22
19
|
reAttachAll: () => Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getSubscriptionHandler.d.ts","sourceRoot":"","sources":["../lib/getSubscriptionHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,SAAS,EAEd,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,EAEzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAS,KAAK,WAAW,EAAE,KAAK,UAAU,EAAE,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAGzF,KAAK,QAAQ,GAAG,CAAC,IAAI,EAAE,SAAS,EAAE,EAAE,GAAG,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AAW,aACV,WAAW,KACrB,OAAO,CAAC,mBAAmB,CAAC;;4DA1B5B,UAAU,KAAG,OAAO,CAAC,oBAAoB,CAAC;gCAzC9B,MAAM,gBACL,MAAM,WACX,WAAW,KACnB,OAAO,CAAC,IAAI,CAAC;;CAqMjB,CAAC"}
|
|
@@ -7,7 +7,7 @@ const FunctionQueuer_1 = require("./FunctionQueuer");
|
|
|
7
7
|
const preffix = prostgles_types_1.CHANNELS._preffix;
|
|
8
8
|
const getSubscriptionHandler = (initOpts) => {
|
|
9
9
|
const { socket, onDebug } = initOpts;
|
|
10
|
-
const subscriptions =
|
|
10
|
+
const subscriptions = new Map();
|
|
11
11
|
const removeServerSub = (unsubChannel) => {
|
|
12
12
|
return new Promise((resolve, reject) => {
|
|
13
13
|
socket.emit(unsubChannel, {}, (err, _res) => {
|
|
@@ -24,7 +24,7 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
24
24
|
function _unsubscribe(channelName, unsubChannel, handler) {
|
|
25
25
|
(0, prostgles_1.debug)("_unsubscribe", { channelName, handler });
|
|
26
26
|
return new Promise((resolve) => {
|
|
27
|
-
const sub = subscriptions
|
|
27
|
+
const sub = subscriptions.get(channelName);
|
|
28
28
|
if (sub) {
|
|
29
29
|
sub.handlers = sub.handlers.filter((h) => h !== handler);
|
|
30
30
|
onDebug === null || onDebug === void 0 ? void 0 : onDebug({
|
|
@@ -37,7 +37,7 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
37
37
|
if (!sub.handlers.length) {
|
|
38
38
|
removeServerSub(unsubChannel);
|
|
39
39
|
socket.removeListener(channelName, sub.onCall);
|
|
40
|
-
delete
|
|
40
|
+
subscriptions.delete(channelName);
|
|
41
41
|
/* Not waiting for server confirmation to speed things up */
|
|
42
42
|
resolve(true);
|
|
43
43
|
}
|
|
@@ -101,22 +101,22 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
101
101
|
}
|
|
102
102
|
return Object.freeze(subHandlers);
|
|
103
103
|
};
|
|
104
|
-
const existing =
|
|
104
|
+
const existing = Array.from(subscriptions.entries()).find(([ch, s]) => {
|
|
105
105
|
return (s.tableName === tableName &&
|
|
106
106
|
s.command === command &&
|
|
107
107
|
(0, prostgles_types_1.isEqual)(s.param1 || {}, param1 || {}) &&
|
|
108
108
|
(0, prostgles_types_1.isEqual)(s.param2 || {}, param2 || {}));
|
|
109
109
|
});
|
|
110
110
|
if (existing) {
|
|
111
|
-
const
|
|
112
|
-
|
|
111
|
+
const [existingChannel, existingSub] = existing;
|
|
112
|
+
existingSub.handlers.push(onChange);
|
|
113
113
|
setTimeout(() => {
|
|
114
|
-
|
|
115
|
-
if (
|
|
116
|
-
onChange(
|
|
114
|
+
const sub = subscriptions.get(existingChannel);
|
|
115
|
+
if (sub === null || sub === void 0 ? void 0 : sub.lastData) {
|
|
116
|
+
onChange(sub.lastData);
|
|
117
117
|
}
|
|
118
118
|
}, 10);
|
|
119
|
-
return makeHandler(
|
|
119
|
+
return makeHandler(existingChannel, existingSub.unsubChannel);
|
|
120
120
|
}
|
|
121
121
|
const { channelName, channelNameReady, channelNameUnsubscribe } = await addServerSub({
|
|
122
122
|
tableName,
|
|
@@ -127,16 +127,16 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
127
127
|
const onCall = function (subData) {
|
|
128
128
|
/* TO DO: confirm receiving data or server will unsubscribe */
|
|
129
129
|
// if(cb) cb(true);
|
|
130
|
-
const sub = subscriptions
|
|
130
|
+
const sub = subscriptions.get(channelName);
|
|
131
131
|
const { data, err } = subData;
|
|
132
132
|
if (sub) {
|
|
133
133
|
if (data !== undefined || err !== undefined) {
|
|
134
|
-
sub.lastData = data
|
|
135
|
-
sub.handlers.forEach((
|
|
134
|
+
sub.lastData = data;
|
|
135
|
+
sub.handlers.forEach((handler) => {
|
|
136
136
|
if (err !== undefined) {
|
|
137
137
|
console.error(`Error within running subscription \n ${channelName}`, err);
|
|
138
138
|
}
|
|
139
|
-
|
|
139
|
+
handler(data !== null && data !== void 0 ? data : [], err);
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
142
|
else {
|
|
@@ -148,7 +148,7 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
148
148
|
}
|
|
149
149
|
};
|
|
150
150
|
socket.on(channelName, onCall);
|
|
151
|
-
subscriptions
|
|
151
|
+
subscriptions.set(channelName, {
|
|
152
152
|
lastData: undefined,
|
|
153
153
|
tableName,
|
|
154
154
|
command,
|
|
@@ -162,7 +162,7 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
162
162
|
await addServerSub({ tableName, command, param1, param2 });
|
|
163
163
|
socket.emit(channelNameReady, { now: Date.now() });
|
|
164
164
|
},
|
|
165
|
-
};
|
|
165
|
+
});
|
|
166
166
|
socket.emit(channelNameReady, { now: Date.now() });
|
|
167
167
|
return makeHandler(channelName, channelNameUnsubscribe);
|
|
168
168
|
}
|
|
@@ -172,7 +172,7 @@ const getSubscriptionHandler = (initOpts) => {
|
|
|
172
172
|
*/
|
|
173
173
|
const reAttachAll = async () => {
|
|
174
174
|
await (onDebug === null || onDebug === void 0 ? void 0 : onDebug({ type: "subscriptions", command: "reAttachAll.start", subscriptions }));
|
|
175
|
-
for await (const s of
|
|
175
|
+
for await (const s of Array.from(subscriptions.values())) {
|
|
176
176
|
try {
|
|
177
177
|
await s.reAttach();
|
|
178
178
|
}
|