@byearlybird/starling 0.7.1 → 0.8.0

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/index.js CHANGED
@@ -141,16 +141,21 @@ const processDocument = (doc, process) => {
141
141
 
142
142
  //#endregion
143
143
  //#region src/eventstamp.ts
144
- const encodeEventstamp = (timestampMs, counter) => {
145
- return `${new Date(timestampMs).toISOString()}|${counter.toString(16).padStart(8, "0")}`;
144
+ const generateNonce = () => {
145
+ return Math.random().toString(16).slice(2, 6).padStart(4, "0");
146
+ };
147
+ const encodeEventstamp = (timestampMs, counter, nonce) => {
148
+ return `${new Date(timestampMs).toISOString()}|${counter.toString(16).padStart(4, "0")}|${nonce}`;
146
149
  };
147
150
  const decodeEventstamp = (eventstamp) => {
148
- const pipeIndex = eventstamp.indexOf("|");
149
- const isoString = eventstamp.slice(0, pipeIndex);
150
- const hexCounter = eventstamp.slice(pipeIndex + 1);
151
+ const parts = eventstamp.split("|");
152
+ const isoString = parts[0];
153
+ const hexCounter = parts[1];
154
+ const nonce = parts[2];
151
155
  return {
152
156
  timestampMs: new Date(isoString).getTime(),
153
- counter: parseInt(hexCounter, 16)
157
+ counter: parseInt(hexCounter, 16),
158
+ nonce
154
159
  };
155
160
  };
156
161
 
@@ -159,23 +164,29 @@ const decodeEventstamp = (eventstamp) => {
159
164
  const createClock = () => {
160
165
  let counter = 0;
161
166
  let lastMs = Date.now();
167
+ let lastNonce = generateNonce();
162
168
  return {
163
169
  now: () => {
164
170
  const wallMs = Date.now();
165
171
  if (wallMs > lastMs) {
166
172
  lastMs = wallMs;
167
173
  counter = 0;
168
- } else counter++;
169
- return encodeEventstamp(lastMs, counter);
174
+ lastNonce = generateNonce();
175
+ } else {
176
+ counter++;
177
+ lastNonce = generateNonce();
178
+ }
179
+ return encodeEventstamp(lastMs, counter, lastNonce);
170
180
  },
171
181
  latest() {
172
- return encodeEventstamp(lastMs, counter);
182
+ return encodeEventstamp(lastMs, counter, lastNonce);
173
183
  },
174
184
  forward(eventstamp) {
175
185
  if (eventstamp > this.latest()) {
176
186
  const newer = decodeEventstamp(eventstamp);
177
187
  lastMs = newer.timestampMs;
178
188
  counter = newer.counter;
189
+ lastNonce = newer.nonce;
179
190
  }
180
191
  }
181
192
  };
@@ -10,6 +10,7 @@ type UnstorageConfig = {
10
10
  pollIntervalMs?: number;
11
11
  onBeforeSet?: UnstorageOnBeforeSet;
12
12
  onAfterGet?: UnstorageOnAfterGet;
13
+ skip?: () => boolean;
13
14
  };
14
15
  declare const unstoragePlugin: <T>(key: string, storage: Storage<StoreSnapshot>, config?: UnstorageConfig) => Plugin<T>;
15
16
  //#endregion
@@ -1,6 +1,6 @@
1
1
  //#region src/plugins/unstorage/plugin.ts
2
2
  const unstoragePlugin = (key, storage, config = {}) => {
3
- const { debounceMs = 0, pollIntervalMs, onBeforeSet, onAfterGet } = config;
3
+ const { debounceMs = 0, pollIntervalMs, onBeforeSet, onAfterGet, skip } = config;
4
4
  let debounceTimer = null;
5
5
  let pollInterval = null;
6
6
  let store = null;
@@ -11,6 +11,7 @@ const unstoragePlugin = (key, storage, config = {}) => {
11
11
  await storage.set(key, persisted);
12
12
  };
13
13
  const schedulePersist = () => {
14
+ if (skip?.()) return;
14
15
  const runPersist = () => {
15
16
  debounceTimer = null;
16
17
  persistSnapshot();
@@ -23,8 +24,8 @@ const unstoragePlugin = (key, storage, config = {}) => {
23
24
  debounceTimer = setTimeout(runPersist, debounceMs);
24
25
  };
25
26
  const pollStorage = async () => {
26
- console.log("poll");
27
27
  if (!store) return;
28
+ if (skip?.()) return;
28
29
  const persisted = await storage.get(key);
29
30
  if (!persisted) return;
30
31
  const data = onAfterGet !== void 0 ? await onAfterGet(persisted) : persisted;
@@ -32,7 +33,7 @@ const unstoragePlugin = (key, storage, config = {}) => {
32
33
  store.forwardClock(data.latestEventstamp);
33
34
  store.begin((tx) => {
34
35
  for (const doc of data.docs) tx.merge(doc);
35
- }, { silent: true });
36
+ });
36
37
  };
37
38
  return { hooks: {
38
39
  onInit: async (s) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@byearlybird/starling",
3
- "version": "0.7.1",
3
+ "version": "0.8.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",