@basictech/react 0.2.0-beta.5 → 0.2.0-beta.7

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/src/sync/index.ts CHANGED
@@ -9,7 +9,7 @@ import 'dexie-syncable';
9
9
  import 'dexie-observable';
10
10
 
11
11
  import { syncProtocol } from './syncProtocol'
12
- import { SERVER_URL } from '../config'
12
+ import { SERVER_URL, log } from '../config'
13
13
  syncProtocol()
14
14
 
15
15
 
@@ -67,7 +67,7 @@ export class BasicSync extends Dexie {
67
67
 
68
68
  // Proceed with the WebSocket connection
69
69
 
70
- console.log('Starting connection...')
70
+ log('Starting connection...')
71
71
  return this.syncable.connect("websocket", WS_URL, { authToken: access_token });
72
72
  }
73
73
 
@@ -75,7 +75,7 @@ export class BasicSync extends Dexie {
75
75
  try {
76
76
  const syncNodes = await this.table('_syncNodes').toArray();
77
77
  const localSyncNodes = syncNodes.filter(node => node.type === 'local');
78
- console.log('Local sync nodes:', localSyncNodes);
78
+ log('Local sync nodes:', localSyncNodes);
79
79
 
80
80
  if (localSyncNodes.length > 1) {
81
81
 
@@ -84,19 +84,19 @@ export class BasicSync extends Dexie {
84
84
  // Check if the largest node is already the master
85
85
  const largestNode = localSyncNodes.find(node => node.id === largestNodeId);
86
86
  if (largestNode && largestNode.isMaster === 1) {
87
- console.log('Largest node is already the master. No changes needed.');
87
+ log('Largest node is already the master. No changes needed.');
88
88
  return; // Exit the function early as no changes are needed
89
89
  }
90
90
 
91
91
 
92
- console.log('Largest node id:', largestNodeId);
93
- console.error('HEISENBUG: More than one local sync node found.')
92
+ log('Largest node id:', largestNodeId);
93
+ log('HEISENBUG: More than one local sync node found.')
94
94
 
95
95
  for (const node of localSyncNodes) {
96
- console.log(`Local sync node keys:`, node.id, node.isMaster);
96
+ log(`Local sync node keys:`, node.id, node.isMaster);
97
97
  await this.table('_syncNodes').update(node.id, { isMaster: node.id === largestNodeId ? 1 : 0 });
98
98
 
99
- console.log(`HEISENBUG: Setting ${node.id} to ${node.id === largestNodeId ? 'master' : '0'}`);
99
+ log(`HEISENBUG: Setting ${node.id} to ${node.id === largestNodeId ? 'master' : '0'}`);
100
100
  }
101
101
 
102
102
  // Add a 1 second delay before returning // i dont think this helps?
@@ -104,7 +104,7 @@ export class BasicSync extends Dexie {
104
104
 
105
105
  }
106
106
 
107
- console.log('Sync nodes updated');
107
+ log('Sync nodes updated');
108
108
  } catch (error) {
109
109
  console.error('Error updating _syncNodes table:', error);
110
110
  }
@@ -136,7 +136,6 @@ export class BasicSync extends Dexie {
136
136
  return this.syncable
137
137
  }
138
138
 
139
-
140
139
  collection(name: string) {
141
140
  // TODO: check against schema
142
141
 
@@ -150,7 +149,7 @@ export class BasicSync extends Dexie {
150
149
 
151
150
  // --- WRITE ---- //
152
151
  add: (data: any) => {
153
- console.log("Adding data to", name, data)
152
+ log("Adding data to", name, data)
154
153
  return this.table(name).add({
155
154
  id: uuidv7(),
156
155
  ...data
@@ -175,12 +174,12 @@ export class BasicSync extends Dexie {
175
174
 
176
175
  // --- READ ---- //
177
176
 
178
- get: (id: string) => {
179
- return this.table(name).get(id)
177
+ get: async (id: string) => {
178
+ return this.table(name).get(id)
180
179
  },
181
180
 
182
- getAll: () => {
183
- return this.table(name).toArray()
181
+ getAll: async () => {
182
+ return this.table(name).toArray();
184
183
  },
185
184
 
186
185
  // --- QUERY ---- //
@@ -193,3 +192,7 @@ export class BasicSync extends Dexie {
193
192
  }
194
193
  }
195
194
  }
195
+
196
+ class QueryMethod {
197
+
198
+ }
@@ -1,8 +1,9 @@
1
1
  "use client"
2
2
  import { Dexie } from "dexie";
3
+ import { log } from "../config";
3
4
 
4
5
  export const syncProtocol = function () {
5
- console.log("Initializing syncProtocol");
6
+ log("Initializing syncProtocol");
6
7
  // Constants:
7
8
  var RECONNECT_DELAY = 5000; // Reconnect delay in case of errors such as network down.
8
9
 
@@ -31,7 +32,7 @@ export const syncProtocol = function () {
31
32
 
32
33
  // sendChanges() method:
33
34
  function sendChanges(changes, baseRevision, partial, onChangesAccepted) {
34
- console.log("sendChanges", changes.length, baseRevision);
35
+ log("sendChanges", changes.length, baseRevision);
35
36
  ++requestId;
36
37
  acceptCallbacks[requestId.toString()] = onChangesAccepted;
37
38
 
@@ -65,7 +66,7 @@ export const syncProtocol = function () {
65
66
  // Initiate this socket connection by sending our clientIdentity. If we dont have a clientIdentity yet,
66
67
  // server will call back with a new client identity that we should use in future WebSocket connections.
67
68
 
68
- console.log("Opening socket - sending clientIdentity", context.clientIdentity);
69
+ log("Opening socket - sending clientIdentity", context.clientIdentity);
69
70
  ws.send(
70
71
  JSON.stringify({
71
72
  type: "clientIdentity",
@@ -79,7 +80,7 @@ export const syncProtocol = function () {
79
80
  // If network down or other error, tell the framework to reconnect again in some time:
80
81
  ws.onerror = function (event) {
81
82
  ws.close();
82
- console.log("ws.onerror", event);
83
+ log("ws.onerror", event);
83
84
  onError(event?.message, RECONNECT_DELAY);
84
85
  };
85
86
 
@@ -109,7 +110,7 @@ export const syncProtocol = function () {
109
110
  // partial: true if server has additionalChanges to send. False if these changes were the last known. (applicable if type="changes")
110
111
  // }
111
112
  var requestFromServer = JSON.parse(event.data);
112
- console.log("requestFromServer", requestFromServer, { acceptCallback, isFirstRound });
113
+ log("requestFromServer", requestFromServer, { acceptCallback, isFirstRound });
113
114
 
114
115
  if (requestFromServer.type == "clientIdentity") {
115
116
  context.clientIdentity = requestFromServer.clientIdentity;
@@ -164,7 +165,7 @@ export const syncProtocol = function () {
164
165
  ws.close();
165
166
  onError(requestFromServer.message, Infinity); // Don't reconnect - an error in application level means we have done something wrong.
166
167
  } else {
167
- console.log("unknown message", requestFromServer);
168
+ log("unknown message", requestFromServer);
168
169
  ws.close();
169
170
  onError("unknown message", Infinity);
170
171
  }