prostgles-client 4.0.356 → 4.0.358

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.
@@ -4,47 +4,53 @@ exports.getSyncHandler = void 0;
4
4
  const prostgles_types_1 = require("prostgles-types");
5
5
  const FunctionQueuer_1 = require("./FunctionQueuer");
6
6
  const prostgles_1 = require("./prostgles");
7
- const preffix = prostgles_types_1.CHANNELS._preffix;
8
- const getSyncHandler = ({ socket }) => {
9
- let syncedTables = {};
10
- let syncs = {};
7
+ const getSyncHandler = ({ socket, onDebug }) => {
8
+ const syncedTables = new Map();
9
+ const syncs = new Map();
11
10
  const destroySyncs = async () => {
12
11
  (0, prostgles_1.debug)("destroySyncs", { syncedTables });
13
- syncs = {};
14
- Object.values(syncedTables).forEach((s) => {
12
+ syncs.clear();
13
+ Array.from(syncedTables.values()).forEach((s) => {
15
14
  s.then((s) => s.destroy());
16
15
  });
17
- syncedTables = {};
16
+ syncedTables.clear();
18
17
  };
19
18
  function _unsync(channelName, triggers) {
20
19
  (0, prostgles_1.debug)("_unsync", { channelName, triggers });
21
20
  return new Promise((resolve, reject) => {
22
- if (syncs[channelName]) {
23
- syncs[channelName].triggers = syncs[channelName].triggers.filter((tr) => tr.onPullRequest !== triggers.onPullRequest &&
21
+ const sync = syncs.get(channelName);
22
+ if (sync) {
23
+ sync.clientSyncHandles = sync.clientSyncHandles.filter((tr) => tr.onPullRequest !== triggers.onPullRequest &&
24
24
  tr.onSyncRequest !== triggers.onSyncRequest &&
25
25
  tr.onUpdates !== triggers.onUpdates);
26
- if (!syncs[channelName].triggers.length) {
26
+ if (!sync.clientSyncHandles.length) {
27
27
  socket.emit(channelName + "unsync", {}, (err, res) => {
28
28
  if (err)
29
29
  reject(err);
30
30
  else
31
31
  resolve(res);
32
32
  });
33
- socket.removeListener(channelName, syncs[channelName].onCall);
34
- delete syncs[channelName];
33
+ socket.removeListener(channelName, sync.onCall);
34
+ syncs.delete(channelName);
35
35
  }
36
36
  }
37
37
  });
38
38
  }
39
- function addServerSync({ tableName, command, param1, param2 }, onSyncRequest) {
39
+ function addServerSync({ tableName, command, filter, select }, onSyncRequest) {
40
40
  return new Promise((resolve, reject) => {
41
- socket.emit(preffix, { tableName, command, param1, param2 }, (err, res) => {
41
+ socket.emit(prostgles_types_1.CHANNEL_PREFIX, { tableName, command, param1: filter, param2: select }, (err, syncInfo) => {
42
+ onDebug === null || onDebug === void 0 ? void 0 : onDebug({
43
+ type: "table",
44
+ command: "getSync",
45
+ tableName,
46
+ data: { filter, select },
47
+ });
42
48
  if (err) {
43
49
  console.error(err);
44
50
  reject(err);
45
51
  }
46
- else if (res) {
47
- const { id_fields, synced_field, channelName } = res;
52
+ else if (syncInfo) {
53
+ const { id_fields, synced_field, channelName } = syncInfo;
48
54
  socket.emit(channelName, { onSyncRequest: onSyncRequest({}) }, (response) => {
49
55
  console.log(response);
50
56
  });
@@ -57,11 +63,11 @@ const getSyncHandler = ({ socket }) => {
57
63
  async function addSync(params, triggers) {
58
64
  return addSyncQueuer.run([params, triggers]);
59
65
  }
60
- async function _addSync({ tableName, command, param1, param2 }, triggers) {
61
- const { onSyncRequest } = triggers;
66
+ async function _addSync({ tableName, command, filter, select }, clientSyncHandlers) {
67
+ const { onSyncRequest } = clientSyncHandlers;
62
68
  function makeHandler(channelName) {
63
69
  const unsync = function () {
64
- _unsync(channelName, triggers);
70
+ _unsync(channelName, clientSyncHandlers);
65
71
  };
66
72
  const syncData = function (data, deleted, cb) {
67
73
  socket.emit(channelName, {
@@ -76,20 +82,19 @@ const getSyncHandler = ({ socket }) => {
76
82
  };
77
83
  return Object.freeze({ unsync, syncData });
78
84
  }
79
- const existingChannel = Object.keys(syncs).find((ch) => {
80
- const s = syncs[ch];
81
- return (s &&
82
- s.tableName === tableName &&
85
+ const matchingSync = Array.from(syncs.entries()).find(([ch, s]) => {
86
+ return (s.tableName === tableName &&
83
87
  s.command === command &&
84
- (0, prostgles_types_1.isEqual)(s.param1, param1) &&
85
- (0, prostgles_types_1.isEqual)(s.param2, param2));
88
+ (0, prostgles_types_1.isEqual)(s.filter, filter) &&
89
+ (0, prostgles_types_1.isEqual)(s.select, select));
86
90
  });
87
- if (existingChannel) {
88
- syncs[existingChannel].triggers.push(triggers);
91
+ if (matchingSync) {
92
+ const [existingChannel, existingSync] = matchingSync;
93
+ existingSync.clientSyncHandles.push(clientSyncHandlers);
89
94
  return makeHandler(existingChannel);
90
95
  }
91
96
  else {
92
- const sync_info = await addServerSync({ tableName, command, param1, param2 }, onSyncRequest);
97
+ const sync_info = await addServerSync({ tableName, command, filter, select }, onSyncRequest);
93
98
  const { channelName } = sync_info;
94
99
  const onCall = function (data, cb) {
95
100
  /*
@@ -101,9 +106,23 @@ const getSyncHandler = ({ socket }) => {
101
106
  */
102
107
  if (!data)
103
108
  return;
104
- if (!syncs[channelName])
109
+ const matchingSync = syncs.get(channelName);
110
+ if (!matchingSync)
105
111
  return;
106
- syncs[channelName].triggers.map(({ onUpdates, onSyncRequest, onPullRequest }) => {
112
+ matchingSync.clientSyncHandles.map(({ onUpdates, onSyncRequest, onPullRequest }) => {
113
+ var _a;
114
+ (_a = syncedTables.get(channelName)) === null || _a === void 0 ? void 0 : _a.then((syncedTable) => {
115
+ onDebug === null || onDebug === void 0 ? void 0 : onDebug({
116
+ type: "sync",
117
+ command: data.data ? "onUpdates"
118
+ : data.onSyncRequest ? "onSyncRequest"
119
+ : "onPullRequest",
120
+ tableName,
121
+ channelName,
122
+ data,
123
+ syncedTable,
124
+ });
125
+ });
107
126
  if (data.data) {
108
127
  Promise.resolve(onUpdates(data))
109
128
  .then(() => {
@@ -134,23 +153,23 @@ const getSyncHandler = ({ socket }) => {
134
153
  }
135
154
  });
136
155
  };
137
- syncs[channelName] = {
156
+ syncs.set(channelName, {
138
157
  tableName,
139
158
  command,
140
- param1,
141
- param2,
142
- triggers: [triggers],
159
+ filter,
160
+ select,
161
+ clientSyncHandles: [clientSyncHandlers],
143
162
  syncInfo: sync_info,
144
163
  onCall,
145
- };
164
+ });
146
165
  socket.on(channelName, onCall);
147
166
  return makeHandler(channelName);
148
167
  }
149
168
  }
150
169
  const reAttachAll = async () => {
151
170
  let reAttached = 0;
152
- Object.entries(syncs).forEach(async ([ch, s]) => {
153
- const firstTrigger = s.triggers[0];
171
+ Array.from(syncs.entries()).forEach(async ([ch, s]) => {
172
+ const firstTrigger = s.clientSyncHandles[0];
154
173
  if (firstTrigger) {
155
174
  try {
156
175
  await addServerSync(s, firstTrigger.onSyncRequest);