@instantdb/core
Advanced tools
Comparing version 0.12.31 to 0.12.32
@@ -65,2 +65,4 @@ // https://www.npmjs.com/package/fake-indexeddb | ||
await reactor.querySubs.waitForSync(); | ||
// Create a new reactor | ||
@@ -67,0 +69,0 @@ const reactor2 = new Reactor({ appId }); |
@@ -41,2 +41,4 @@ /** | ||
}; | ||
_beforeUnloadCbs: any[]; | ||
_beforeUnload(): void; | ||
_initStorage(Storage: any): void; | ||
@@ -43,0 +45,0 @@ /** |
@@ -74,2 +74,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this._currentUserCached = { isLoading: true, error: undefined, user: undefined }; | ||
this._beforeUnloadCbs = []; | ||
/** | ||
@@ -275,2 +276,6 @@ * merge querySubs from storage and in memory. Has the following side | ||
}); | ||
if (typeof addEventListener !== "undefined") { | ||
this._beforeUnload = this._beforeUnload.bind(this); | ||
addEventListener("beforeunload", this._beforeUnload); | ||
} | ||
} | ||
@@ -285,3 +290,12 @@ _initStorage(Storage) { | ||
}); | ||
this._beforeUnloadCbs.push(() => { | ||
this.pendingMutations.flush(); | ||
this.querySubs.flush(); | ||
}); | ||
} | ||
_beforeUnload() { | ||
for (const cb of this._beforeUnloadCbs) { | ||
cb(); | ||
} | ||
} | ||
/** | ||
@@ -288,0 +302,0 @@ * @param {'enqueued' | 'pending' | 'synced' | 'timeout' | 'error' } status |
export class PersistedObject { | ||
constructor(persister: any, key: any, defaultValue: any, onMerge: any, toJSON?: (x: any) => string, fromJSON?: (x: any) => any); | ||
constructor(persister: any, key: any, defaultValue: any, onMerge: any, toJSON?: (x: any) => string, fromJSON?: (x: any) => any, saveThrottleMs?: number); | ||
_persister: any; | ||
@@ -11,6 +11,13 @@ _key: any; | ||
fromJSON: (x: any) => any; | ||
_saveThrottleMs: number; | ||
_pendingSaveCbs: any[]; | ||
_load(): Promise<void>; | ||
waitForLoaded(): Promise<void>; | ||
waitForSync(): Promise<void>; | ||
_writeToStorage(): void; | ||
flush(): Promise<void>; | ||
_enqueuePersist(cb: any): void; | ||
_nextSave: any; | ||
set(f: any, cb: any): void; | ||
} | ||
//# sourceMappingURL=PersistedObject.d.ts.map |
@@ -29,3 +29,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return JSON.parse(x); | ||
}) { | ||
}, saveThrottleMs = 100) { | ||
this._persister = persister; | ||
@@ -39,2 +39,4 @@ this._key = key; | ||
this.fromJSON = fromJSON; | ||
this._saveThrottleMs = saveThrottleMs; | ||
this._pendingSaveCbs = []; | ||
this._load(); | ||
@@ -63,14 +65,51 @@ } | ||
} | ||
waitForSync() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._nextSave) { | ||
return; | ||
} | ||
const syncedPromise = new Promise((resolve) => { | ||
this._pendingSaveCbs.push(resolve); | ||
}); | ||
yield syncedPromise; | ||
}); | ||
} | ||
_writeToStorage() { | ||
this._persister.setItem(this._key, this.toJSON(this.currentValue)); | ||
for (const cb of this._pendingSaveCbs) { | ||
cb(); | ||
} | ||
this._pendingSaveCbs.length = 0; | ||
} | ||
flush() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._nextSave) { | ||
return; | ||
} | ||
clearTimeout(this._nextSave); | ||
this._writeToStorage(); | ||
}); | ||
} | ||
_enqueuePersist(cb) { | ||
if (this._nextSave) { | ||
if (cb) { | ||
this._pendingSaveCbs.push(cb); | ||
} | ||
return; | ||
} | ||
this._nextSave = setTimeout(() => { | ||
this._nextSave = null; | ||
this._writeToStorage(); | ||
}, this._saveThrottleMs); | ||
} | ||
set(f, cb) { | ||
this.currentValue = f(this.currentValue); | ||
if (!this._isLoading) { | ||
setTimeout(() => { | ||
this._persister.setItem(this._key, this.toJSON(this.currentValue)); | ||
if (cb) { | ||
cb(); | ||
} | ||
}, 0); | ||
if (this._isLoading) { | ||
this._loadedCbs.push(() => this._enqueuePersist(cb)); | ||
} | ||
else { | ||
this._enqueuePersist(cb); | ||
} | ||
} | ||
} | ||
//# sourceMappingURL=PersistedObject.js.map |
@@ -41,2 +41,4 @@ /** | ||
}; | ||
_beforeUnloadCbs: any[]; | ||
_beforeUnload(): void; | ||
_initStorage(Storage: any): void; | ||
@@ -43,0 +45,0 @@ /** |
@@ -102,2 +102,3 @@ "use strict"; | ||
this._currentUserCached = { isLoading: true, error: undefined, user: undefined }; | ||
this._beforeUnloadCbs = []; | ||
/** | ||
@@ -303,2 +304,6 @@ * merge querySubs from storage and in memory. Has the following side | ||
}); | ||
if (typeof addEventListener !== "undefined") { | ||
this._beforeUnload = this._beforeUnload.bind(this); | ||
addEventListener("beforeunload", this._beforeUnload); | ||
} | ||
} | ||
@@ -313,3 +318,12 @@ _initStorage(Storage) { | ||
}); | ||
this._beforeUnloadCbs.push(() => { | ||
this.pendingMutations.flush(); | ||
this.querySubs.flush(); | ||
}); | ||
} | ||
_beforeUnload() { | ||
for (const cb of this._beforeUnloadCbs) { | ||
cb(); | ||
} | ||
} | ||
/** | ||
@@ -316,0 +330,0 @@ * @param {'enqueued' | 'pending' | 'synced' | 'timeout' | 'error' } status |
export class PersistedObject { | ||
constructor(persister: any, key: any, defaultValue: any, onMerge: any, toJSON?: (x: any) => string, fromJSON?: (x: any) => any); | ||
constructor(persister: any, key: any, defaultValue: any, onMerge: any, toJSON?: (x: any) => string, fromJSON?: (x: any) => any, saveThrottleMs?: number); | ||
_persister: any; | ||
@@ -11,6 +11,13 @@ _key: any; | ||
fromJSON: (x: any) => any; | ||
_saveThrottleMs: number; | ||
_pendingSaveCbs: any[]; | ||
_load(): Promise<void>; | ||
waitForLoaded(): Promise<void>; | ||
waitForSync(): Promise<void>; | ||
_writeToStorage(): void; | ||
flush(): Promise<void>; | ||
_enqueuePersist(cb: any): void; | ||
_nextSave: any; | ||
set(f: any, cb: any): void; | ||
} | ||
//# sourceMappingURL=PersistedObject.d.ts.map |
@@ -32,3 +32,3 @@ "use strict"; | ||
return JSON.parse(x); | ||
}) { | ||
}, saveThrottleMs = 100) { | ||
this._persister = persister; | ||
@@ -42,2 +42,4 @@ this._key = key; | ||
this.fromJSON = fromJSON; | ||
this._saveThrottleMs = saveThrottleMs; | ||
this._pendingSaveCbs = []; | ||
this._load(); | ||
@@ -66,12 +68,49 @@ } | ||
} | ||
waitForSync() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._nextSave) { | ||
return; | ||
} | ||
const syncedPromise = new Promise((resolve) => { | ||
this._pendingSaveCbs.push(resolve); | ||
}); | ||
yield syncedPromise; | ||
}); | ||
} | ||
_writeToStorage() { | ||
this._persister.setItem(this._key, this.toJSON(this.currentValue)); | ||
for (const cb of this._pendingSaveCbs) { | ||
cb(); | ||
} | ||
this._pendingSaveCbs.length = 0; | ||
} | ||
flush() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this._nextSave) { | ||
return; | ||
} | ||
clearTimeout(this._nextSave); | ||
this._writeToStorage(); | ||
}); | ||
} | ||
_enqueuePersist(cb) { | ||
if (this._nextSave) { | ||
if (cb) { | ||
this._pendingSaveCbs.push(cb); | ||
} | ||
return; | ||
} | ||
this._nextSave = setTimeout(() => { | ||
this._nextSave = null; | ||
this._writeToStorage(); | ||
}, this._saveThrottleMs); | ||
} | ||
set(f, cb) { | ||
this.currentValue = f(this.currentValue); | ||
if (!this._isLoading) { | ||
setTimeout(() => { | ||
this._persister.setItem(this._key, this.toJSON(this.currentValue)); | ||
if (cb) { | ||
cb(); | ||
} | ||
}, 0); | ||
if (this._isLoading) { | ||
this._loadedCbs.push(() => this._enqueuePersist(cb)); | ||
} | ||
else { | ||
this._enqueuePersist(cb); | ||
} | ||
} | ||
@@ -78,0 +117,0 @@ } |
{ | ||
"name": "@instantdb/core", | ||
"version": "v0.12.31", | ||
"version": "v0.12.32", | ||
"description": "Instant's core local abstraction", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -83,2 +83,3 @@ // @ts-check | ||
_currentUserCached = { isLoading: true, error: undefined, user: undefined }; | ||
_beforeUnloadCbs = []; | ||
@@ -134,2 +135,7 @@ constructor( | ||
}); | ||
if (typeof addEventListener !== "undefined") { | ||
this._beforeUnload = this._beforeUnload.bind(this); | ||
addEventListener("beforeunload", this._beforeUnload); | ||
} | ||
} | ||
@@ -159,4 +165,14 @@ | ||
); | ||
this._beforeUnloadCbs.push(() => { | ||
this.pendingMutations.flush(); | ||
this.querySubs.flush(); | ||
}); | ||
} | ||
_beforeUnload() { | ||
for (const cb of this._beforeUnloadCbs) { | ||
cb(); | ||
} | ||
} | ||
/** | ||
@@ -163,0 +179,0 @@ * @param {'enqueued' | 'pending' | 'synced' | 'timeout' | 'error' } status |
@@ -27,2 +27,3 @@ // PersistedObjects save data outside of memory. | ||
}, | ||
saveThrottleMs = 100, | ||
) { | ||
@@ -39,2 +40,4 @@ this._persister = persister; | ||
this.fromJSON = fromJSON; | ||
this._saveThrottleMs = saveThrottleMs; | ||
this._pendingSaveCbs = []; | ||
@@ -64,13 +67,49 @@ this._load(); | ||
async waitForSync() { | ||
if (!this._nextSave) { | ||
return; | ||
} | ||
const syncedPromise = new Promise((resolve) => { | ||
this._pendingSaveCbs.push(resolve); | ||
}); | ||
await syncedPromise; | ||
} | ||
_writeToStorage() { | ||
this._persister.setItem(this._key, this.toJSON(this.currentValue)); | ||
for (const cb of this._pendingSaveCbs) { | ||
cb(); | ||
} | ||
this._pendingSaveCbs.length = 0; | ||
} | ||
async flush() { | ||
if (!this._nextSave) { | ||
return; | ||
} | ||
clearTimeout(this._nextSave); | ||
this._writeToStorage(); | ||
} | ||
_enqueuePersist(cb) { | ||
if (this._nextSave) { | ||
if (cb) { | ||
this._pendingSaveCbs.push(cb); | ||
} | ||
return; | ||
} | ||
this._nextSave = setTimeout(() => { | ||
this._nextSave = null; | ||
this._writeToStorage(); | ||
}, this._saveThrottleMs); | ||
} | ||
set(f, cb) { | ||
this.currentValue = f(this.currentValue); | ||
if (!this._isLoading) { | ||
setTimeout(() => { | ||
this._persister.setItem(this._key, this.toJSON(this.currentValue)); | ||
if (cb) { | ||
cb(); | ||
} | ||
}, 0); | ||
if (this._isLoading) { | ||
this._loadedCbs.push(() => this._enqueuePersist(cb)); | ||
} else { | ||
this._enqueuePersist(cb); | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
1991885
40504