prostgles-client 4.0.364 → 4.0.366

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.
@@ -1,10 +1,9 @@
1
- import { type AnyObject } from "prostgles-types";
2
1
  import { WAL } from "prostgles-types/dist/WAL";
3
- import { type SyncedTableOptions } from "./SyncedTable";
4
2
  import type { Socket } from "socket.io-client";
3
+ import { type SyncedTableOptions } from "./SyncedTable";
5
4
  export declare const createSync: (socket: Socket, options: Omit<SyncedTableOptions, "onReady">) => Promise<{
6
- sync: <T extends AnyObject = AnyObject>(onChange: import("./SyncedTable").MultiChangeListener<T>, handlesOnData?: boolean) => import("./SyncedTable").MultiSyncHandles<T>;
7
- syncOne: <T extends AnyObject = AnyObject, Full extends boolean = false>(idObj: Partial<T>, onChange: import("./SyncedTable").SingleChangeListener<T, Full>, handlesOnData?: boolean) => import("./SyncedTable").SingleSyncHandles<T, Full>;
5
+ sync: <T extends import("prostgles-types").AnyObject = import("prostgles-types").AnyObject>(onChange: import("./SyncedTable").MultiChangeListener<T>, handlesOnData?: boolean) => import("./SyncedTable").MultiSyncHandles<T>;
6
+ syncOne: <T extends import("prostgles-types").AnyObject = import("prostgles-types").AnyObject, Full extends boolean = false>(idObj: Partial<T>, onChange: import("./SyncedTable").SingleChangeListener<T, Full>, handlesOnData?: boolean) => import("./SyncedTable").SingleSyncHandles<T, Full>;
8
7
  notifyWal: WAL;
9
8
  }>;
10
9
  //# sourceMappingURL=createSync.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createSync.d.ts","sourceRoot":"","sources":["../../lib/SyncedTable/createSync.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,SAAS,EAIf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,GAAG,EAAgB,MAAM,0BAA0B,CAAC;AAI7D,OAAO,EAIL,KAAK,kBAAkB,EACxB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE/C,eAAO,MAAM,UAAU,GAAU,QAAQ,MAAM,EAAE,SAAS,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC;;;;EAkO5F,CAAC"}
1
+ {"version":3,"file":"createSync.d.ts","sourceRoot":"","sources":["../../lib/SyncedTable/createSync.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,GAAG,EAAgB,MAAM,0BAA0B,CAAC;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI/C,OAAO,EAIL,KAAK,kBAAkB,EACxB,MAAM,eAAe,CAAC;AAEvB,eAAO,MAAM,UAAU,GAAU,QAAQ,MAAM,EAAE,SAAS,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC;;;;EAyO5F,CAAC"}
@@ -15,12 +15,18 @@ const createSync = async (socket, options) => {
15
15
  console.error("Sync internal error: ", err);
16
16
  };
17
17
  const onSyncRequest = (syncBatchParams) => {
18
- let clientSyncInfo = { c_lr: undefined, c_fr: undefined, c_count: 0 };
18
+ let clientSyncInfo = {
19
+ state: "syncing",
20
+ c_lr: undefined,
21
+ c_fr: undefined,
22
+ c_count: 0,
23
+ };
19
24
  const batch = store.getBatch(syncBatchParams);
20
25
  const firstRow = batch[0];
21
26
  const lastRow = batch[batch.length - 1];
22
27
  if (firstRow && lastRow) {
23
28
  clientSyncInfo = {
29
+ state: "syncing",
24
30
  c_fr: store.getRowSyncObj(firstRow),
25
31
  c_lr: store.getRowSyncObj(lastRow),
26
32
  c_count: batch.length,
@@ -35,14 +41,14 @@ const createSync = async (socket, options) => {
35
41
  // }
36
42
  const data = store.getBatch(syncBatchParams);
37
43
  await onDebug({ command: "onPullRequest", data: { syncBatchParams, data } });
38
- return { data };
44
+ return { data, success: true };
39
45
  };
40
46
  const onUpdates = async (onUpdatesParams) => {
41
47
  await onDebug({ command: "onUpdates", data: { onUpdatesParams } });
42
- if ("err" in onUpdatesParams && onUpdatesParams.err) {
48
+ if (onUpdatesParams.state === "error" && onUpdatesParams.err) {
43
49
  onError(onUpdatesParams.err);
44
50
  }
45
- else if ("isSynced" in onUpdatesParams && onUpdatesParams.isSynced && !state.isSynced) {
51
+ else if (onUpdatesParams.state === "synced" && onUpdatesParams.isSynced && !state.isSynced) {
46
52
  state.isSynced = onUpdatesParams.isSynced;
47
53
  const items = store.getItems().map((d) => ({ ...d }));
48
54
  store.setItems([]);
@@ -65,7 +71,7 @@ const createSync = async (socket, options) => {
65
71
  else {
66
72
  console.error("Unexpected onUpdates");
67
73
  }
68
- return true;
74
+ return { success: true };
69
75
  };
70
76
  const opts = {
71
77
  id_fields,
@@ -185,12 +191,12 @@ const createSync = async (socket, options) => {
185
191
  };
186
192
  const subscriptionManager = (0, createSyncSubscriptionManager_1.createSyncSubscriptionManager)({ id_fields, synced_field }, store, stateUtils, upsert);
187
193
  store.setItems(dbSync.syncInfo.data);
188
- if (!dbSync.syncInfo.isSynced) {
189
- dbSync.syncData();
190
- }
191
- else {
192
- state.isSynced = true;
193
- }
194
+ /** TODO */
195
+ // if (!dbSync.syncInfo.isSynced) {
196
+ dbSync.syncData();
197
+ // } else {
198
+ // state.isSynced = true;
199
+ // }
194
200
  return subscriptionManager;
195
201
  };
196
202
  exports.createSync = createSync;
@@ -1,8 +1,8 @@
1
- import { type ClientSyncHandles } from "prostgles-types";
1
+ import { ReplicationProtocol, type ClientSyncHandles } from "prostgles-types";
2
2
  import type { Socket } from "socket.io-client";
3
3
  import { type SyncDebugEvent } from "../prostgles";
4
4
  import type { SyncedTableOptions } from "./SyncedTable";
5
- export declare const createSyncStateUtils: (socket: Socket, options: Omit<SyncedTableOptions, "onReady">) => {
5
+ export declare const createSyncStateUtils: (socket: Socket, options: SyncedTableOptions) => {
6
6
  state: {
7
7
  isSynced: boolean;
8
8
  };
@@ -14,12 +14,16 @@ export declare const createSyncStateUtils: (socket: Socket, options: Omit<Synced
14
14
  columns: import("prostgles-types").ValidatedColumnInfo[];
15
15
  _syncInfo: import("prostgles-types").SyncTableInfo;
16
16
  initializeSync: (handles: ClientSyncHandles) => Promise<{
17
- syncInfo: import("prostgles-types").SyncConfig & {
18
- data: import("prostgles-types").AnyObject[];
19
- isSynced: boolean;
20
- };
17
+ syncInfo: import("prostgles-types").JSONB.GetObjectType<{
18
+ readonly id_fields: "string[]";
19
+ readonly synced_field: "string";
20
+ readonly channelName: "string";
21
+ readonly data: "any[]";
22
+ readonly isSynced: "boolean";
23
+ }>;
21
24
  unsync: () => Promise<unknown>;
22
25
  syncData: (data?: import("prostgles-types").AnyObject[], deleted?: import("prostgles-types").AnyObject[], cb?: (err?: any) => void) => void;
26
+ handlers: ReplicationProtocol.OutgoingHandlers<"client">;
23
27
  }>;
24
28
  filter: import("prostgles-types").EqualityFilter<import("prostgles-types").AnyObject>;
25
29
  select: import("prostgles-types").FieldFilter;
@@ -1 +1 @@
1
- {"version":3,"file":"createSyncStateUtils.d.ts","sourceRoot":"","sources":["../../lib/SyncedTable/createSyncStateUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,iBAAiB,EAEvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAoB,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,KAAK,EAAe,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAErE,eAAO,MAAM,oBAAoB,GAC/B,QAAQ,MAAM,EACd,SAAS,IAAI,CAAC,kBAAkB,EAAE,SAAS,CAAC;;;;mBAKtB,IAAI,CAAC,cAAc,EAAE,MAAM,GAAG,WAAW,GAAG,aAAa,GAAG,SAAS,CAAC;;;;;;;8BAyBrD,iBAAiB;;;;;;2HA6HuuB,CAAC;;;;;CADjyB,CAAC"}
1
+ {"version":3,"file":"createSyncStateUtils.d.ts","sourceRoot":"","sources":["../../lib/SyncedTable/createSyncStateUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,mBAAmB,EACnB,KAAK,iBAAiB,EACvB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAe,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAErE,eAAO,MAAM,oBAAoB,GAAI,QAAQ,MAAM,EAAE,SAAS,kBAAkB;;;;mBAIxD,IAAI,CAAC,cAAc,EAAE,MAAM,GAAG,WAAW,GAAG,aAAa,GAAG,SAAS,CAAC;;;;;;;8BAyBrD,iBAAiB;;;;;;;;;2HAuHqtB,CAAC;;;;;;CAD/wB,CAAC"}
@@ -38,76 +38,74 @@ const createSyncStateUtils = (socket, options) => {
38
38
  console.error(err);
39
39
  reject(err);
40
40
  }
41
- else if (syncInfo) {
42
- // const { channelName } = syncInfo;
43
- // socket.emit(
44
- // channelName,
45
- // { onSyncRequest: handles.onSyncRequest({}) },
46
- // (response: any) => {
47
- // console.log(response);
48
- // },
49
- // );
41
+ else {
50
42
  resolve(syncInfo);
51
43
  }
52
44
  });
53
45
  });
54
- const onCall = function (data, cb) {
55
- /*
56
- Client will:
57
- 1. Send last_synced on(onSyncRequest)
58
- 2. Send data >= server_synced on(onPullRequest)
59
- 3. Send data on CRUD emit(data.data)
60
- 4. Upsert data.data on(data.data)
61
- */
62
- if (!data)
63
- return;
64
- const { onUpdates, onSyncRequest, onPullRequest } = handles;
65
- onDebug({
66
- command: data.data ? "onUpdates"
67
- : data.onSyncRequest ? "onSyncRequest"
68
- : "onPullRequest",
69
- data,
70
- });
71
- if (data.data) {
72
- Promise.resolve(onUpdates(data))
73
- .then(() => {
74
- cb({ ok: true });
75
- })
76
- .catch((err) => {
77
- cb({ err });
78
- });
79
- }
80
- else if (data.onSyncRequest) {
81
- Promise.resolve(onSyncRequest(data.onSyncRequest))
82
- .then((res) => cb({ onSyncRequest: res }))
83
- .catch((err) => {
84
- cb({ err });
85
- });
86
- }
87
- else if (data.onPullRequest) {
88
- Promise.resolve(onPullRequest(data.onPullRequest))
89
- .then((result) => {
90
- cb(result);
91
- })
92
- .catch((err) => {
93
- cb({ err });
94
- });
95
- }
96
- else {
97
- console.log("unexpected response");
98
- }
99
- };
100
- socket.on(channelName, onCall);
101
- const syncData = function (data, deleted, cb) {
102
- socket.emit(channelName, {
103
- onSyncRequest: {
104
- ...handles.onSyncRequest({}),
105
- ...{ data },
106
- ...{ deleted },
107
- },
108
- }, !cb ? null : ((response) => {
109
- cb(response);
110
- }));
46
+ const handlers = prostgles_types_1.ReplicationProtocol.getClientHandlers(channelName, socket, {
47
+ PullRequest: handles.onPullRequest,
48
+ ServerSyncRequest: handles.onSyncRequest,
49
+ UpdateRequest: handles.onUpdates,
50
+ });
51
+ // const onCall = function (data: any | undefined, cb: AnyFunction) {
52
+ // /*
53
+ // Client will:
54
+ // 1. Send last_synced on(onSyncRequest)
55
+ // 2. Send data >= server_synced on(onPullRequest)
56
+ // 3. Send data on CRUD emit(data.data)
57
+ // 4. Upsert data.data on(data.data)
58
+ // */
59
+ // if (!data) {
60
+ // return;
61
+ // }
62
+ // const { onUpdates, onSyncRequest, onPullRequest } = handles;
63
+ // onDebug({
64
+ // command:
65
+ // data.data ? "onUpdates"
66
+ // : data.onSyncRequest ? "onSyncRequest"
67
+ // : "onPullRequest",
68
+ // data,
69
+ // });
70
+ // if (data.data) {
71
+ // Promise.resolve(onUpdates(data))
72
+ // .then(() => {
73
+ // cb({ ok: true });
74
+ // })
75
+ // .catch((err) => {
76
+ // cb({ err });
77
+ // });
78
+ // } else if (data.onSyncRequest) {
79
+ // Promise.resolve(onSyncRequest(data.onSyncRequest))
80
+ // .then((res) => cb({ onSyncRequest: res }))
81
+ // .catch((err) => {
82
+ // cb({ err });
83
+ // });
84
+ // } else if (data.onPullRequest) {
85
+ // Promise.resolve(onPullRequest(data.onPullRequest))
86
+ // .then((result) => {
87
+ // cb(result);
88
+ // })
89
+ // .catch((err) => {
90
+ // cb({ err });
91
+ // });
92
+ // } else {
93
+ // console.log("unexpected response");
94
+ // }
95
+ // };
96
+ // socket.on(channelName, onCall);
97
+ const syncData = async (data) => {
98
+ const { c_count, c_fr, c_lr } = await handles.onSyncRequest({});
99
+ handlers.ClientSyncRequest(data && c_fr && c_lr ?
100
+ { state: "syncing-data", c_count, c_fr, c_lr, data }
101
+ : { state: "syncing", c_count, c_fr, c_lr });
102
+ // socket.emit(channelName, {
103
+ // onSyncRequest: {
104
+ // ...handles.onSyncRequest({}),
105
+ // ...{ data },
106
+ // ...{ deleted },
107
+ // },
108
+ // });
111
109
  };
112
110
  const unsync = () => {
113
111
  return new Promise((resolve, reject) => {
@@ -117,10 +115,10 @@ const createSyncStateUtils = (socket, options) => {
117
115
  else
118
116
  resolve(res);
119
117
  });
120
- socket.removeListener(channelName, onCall);
118
+ socket.removeAllListeners(channelName);
121
119
  });
122
120
  };
123
- return { syncInfo, unsync, syncData };
121
+ return { syncInfo, unsync, syncData, handlers };
124
122
  };
125
123
  return {
126
124
  state,