supastash 0.1.21 → 0.1.22

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 +1 @@
1
- {"version":3,"file":"mainQueryHelpers.d.ts","sourceRoot":"","sources":["../../../../src/utils/query/helpers/mainQueryHelpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,mBAAmB,EACnB,cAAc,EACf,MAAM,4BAA4B,CAAC;AAOpC,wBAAgB,8BAA8B,CAC5C,MAAM,EAAE,WAAW,EACnB,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,GACZ,IAAI,CAUN;AAED,wBAAgB,eAAe,CAAC,CAAC,EAC/B,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,GACtB,CAAC,GAAG,CAAC,EAAE,GAAG,IAAI,GAAG,SAAS,CAc5B;AAED,wBAAgB,cAAc,CAAC,CAAC,SAAS,OAAO,EAAE,CAAC,SAAS,WAAW,EAAE,CAAC,EAAE,CAAC,EAC3E,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,WAAW,EACnB,WAAW,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAChD,YAAY,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,GAC7C,CAAC,KAAK,GAAG;IAAE,aAAa,CAAC,EAAE,cAAc,CAAA;CAAE,CAAC,GAAG,IAAI,CAwBrD;AAiBD,wBAAgB,UAAU,CAAC,KAAK,EAAE,cAAc,CAAC,WAAW,EAAE,OAAO,EAAE,GAAG,CAAC,QAQ1E;AAqDD,wBAAsB,eAAe,CACnC,CAAC,SAAS,WAAW,EACrB,CAAC,SAAS,OAAO,EACjB,CAAC,EACD,CAAC,EAED,KAAK,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,EAAE,OAAO,EAAE,GAAG,CAAC,QAO1E;AA2DD,wBAAsB,eAAe,CACnC,CAAC,SAAS,WAAW,EACrB,CAAC,SAAS,OAAO,EACjB,CAAC,EACD,CAAC,EAED,KAAK,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAC7B,OAAO,CAAC;IACT,WAAW,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IACjD,YAAY,EAAE,mBAAmB,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC;CAChD,CAAC,CA2CD"}
@@ -1,5 +1,5 @@
1
1
  import { isOnline } from "../../../utils/connection";
2
- import { log, logError } from "../../../utils/logs";
2
+ import { log, logError, logWarn } from "../../../utils/logs";
3
3
  import { generateUUIDv4 } from "../../genUUID";
4
4
  import { queryLocalDb } from "../localDbQuery";
5
5
  import { querySupabase } from "../remoteQuery/supabaseQuery";
@@ -47,9 +47,10 @@ export function getCommonError(table, method, localResult, remoteResult) {
47
47
  return null;
48
48
  }
49
49
  const stateCache = new Map();
50
- const runningStates = new Set();
51
- const calledOfflineRetries = new Map();
52
50
  const retryCount = new Map();
51
+ const calledOfflineRetries = new Map();
52
+ const opQueue = [];
53
+ let isProcessing = false;
53
54
  const MAX_RETRIES = 2;
54
55
  const MAX_OFFLINE_RETRIES = 10;
55
56
  const getOpKey = (state) => `${state.method}:${state.table}:${state.id}`;
@@ -58,50 +59,56 @@ function delay(ms) {
58
59
  }
59
60
  export function addToCache(state) {
60
61
  const opKey = getOpKey(state);
61
- if (runningStates.has(opKey))
62
+ if (stateCache.has(opKey))
62
63
  return;
63
- runningStates.add(opKey);
64
64
  stateCache.set(opKey, state);
65
- runRemoteQuery(opKey);
65
+ opQueue.push(opKey);
66
+ processQueue();
66
67
  }
67
- async function runRemoteQuery(opKey) {
68
- const state = stateCache.get(opKey);
69
- if (!state)
68
+ async function processQueue() {
69
+ if (isProcessing)
70
70
  return;
71
- retryCount.set(opKey, retryCount.get(opKey) ?? 0);
72
- try {
73
- while ((retryCount.get(opKey) ?? 0) <= MAX_RETRIES) {
74
- const isConnected = await isOnline();
75
- if (!isConnected) {
76
- const offlineRetries = (calledOfflineRetries.get(opKey) || 0) + 1;
77
- if (offlineRetries > MAX_OFFLINE_RETRIES) {
78
- logError(`[Supastash] Gave up on ${opKey} after ${MAX_OFFLINE_RETRIES} offline retries`);
71
+ isProcessing = true;
72
+ while (opQueue.length > 0) {
73
+ const opKey = opQueue.shift();
74
+ const state = stateCache.get(opKey);
75
+ if (!state)
76
+ continue;
77
+ retryCount.set(opKey, retryCount.get(opKey) ?? 0);
78
+ try {
79
+ while ((retryCount.get(opKey) ?? 0) <= MAX_RETRIES) {
80
+ const isConnected = await isOnline();
81
+ if (!isConnected) {
82
+ const offlineRetries = (calledOfflineRetries.get(opKey) || 0) + 1;
83
+ if (offlineRetries > MAX_OFFLINE_RETRIES) {
84
+ logError(`[Supastash] Gave up on ${opKey} after ${MAX_OFFLINE_RETRIES} offline retries`);
85
+ break;
86
+ }
87
+ calledOfflineRetries.set(opKey, offlineRetries);
88
+ await delay(1000);
89
+ continue;
90
+ }
91
+ calledOfflineRetries.delete(opKey);
92
+ const { error } = await querySupabase({ ...state }, true);
93
+ if (!error) {
94
+ log(`[Supastash] Synced item on ${state.table} with ${state.method} to supabase`);
79
95
  break;
80
96
  }
81
- calledOfflineRetries.set(opKey, offlineRetries);
82
- await delay(1000);
83
- continue;
84
- }
85
- calledOfflineRetries.delete(opKey);
86
- const { error } = await querySupabase({ ...state }, true);
87
- if (!error) {
88
- log(`[Supastash] Synced item on ${state.table} with ${state.method} to supabase`);
89
- break;
97
+ const currentRetry = (retryCount.get(opKey) ?? 0) + 1;
98
+ logError(`[Supastash] Remote sync failed: ${opKey} (${currentRetry}/${MAX_RETRIES}) → ${error.message}`);
99
+ retryCount.set(opKey, currentRetry);
100
+ await delay(100 * currentRetry);
90
101
  }
91
- const currentRetry = (retryCount.get(opKey) ?? 0) + 1;
92
- logError(`[Supastash] Remote sync failed: ${opKey} (${currentRetry}/${MAX_RETRIES}) → ${error.message}`);
93
- retryCount.set(opKey, currentRetry);
94
- await delay(100 * currentRetry);
102
+ }
103
+ catch (error) {
104
+ logWarn(`[Supastash] Error running remote query: ${opKey} → ${error}`);
105
+ }
106
+ finally {
107
+ retryCount.delete(opKey);
108
+ stateCache.delete(opKey);
95
109
  }
96
110
  }
97
- catch (error) {
98
- logError(`[Supastash] Error running remote query: ${opKey} → ${error}`);
99
- }
100
- finally {
101
- retryCount.delete(opKey);
102
- stateCache.delete(opKey);
103
- runningStates.delete(opKey);
104
- }
111
+ isProcessing = false;
105
112
  }
106
113
  export async function runSyncStrategy(state) {
107
114
  const { type } = state;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supastash",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",