apical-store
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -970,4 +970,2 @@ (function (global, factory) { | ||
this.sync = debounce(this.$$sync.bind(this), this.$$debounceRate); | ||
this.$$idb = new IDB(name); | ||
this.$$token = token; | ||
this.$$model = model || Document; | ||
@@ -983,5 +981,9 @@ if (encode) { | ||
} | ||
if (persist && endpoint) { | ||
if (name && persist) { | ||
this.$$idb = new IDB(name); | ||
this.$$loadFromLocal(); | ||
this.$$setupObservers(); | ||
} | ||
if (token && endpoint && name && persist) { | ||
this.$$token = token; | ||
this.$$syncService = new SyncService(endpoint, this.$$token, name); | ||
@@ -1019,2 +1021,4 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) | ||
return; | ||
const deserialized = (yield this.$$idb.values()).map((x) => this.$$deserialize(x)); | ||
@@ -1028,2 +1032,4 @@ this.$$observableObject.silently((o) => { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) | ||
return; | ||
this.$$lastProcessChanges = Date.now(); | ||
@@ -1088,2 +1094,4 @@ const toWriteLocally = []; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) | ||
return 0; | ||
return Number((yield this.$$idb.getMetadata("version")) || 0); | ||
@@ -1126,2 +1134,7 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) { | ||
return { | ||
exception: "IDB not available", | ||
}; | ||
} | ||
if (!this.$$syncService) { | ||
@@ -1271,11 +1284,11 @@ return { | ||
function observe(input) { | ||
function observe(store) { | ||
return function (component) { | ||
let oCWM = component.prototype.componentWillMount || (() => { }); | ||
component.prototype.componentWillMount = function () { | ||
let oCDM = component.prototype.componentDidMount || (() => { }); | ||
component.prototype.componentDidMount = function () { | ||
let unObservers = []; | ||
this.setState({}); | ||
const observer = () => this.setState({}); | ||
input.observe(observer); | ||
unObservers.push(() => input.unobserve(observer)); | ||
store.$$observableObject.observe(observer); | ||
unObservers.push(() => store.$$observableObject.unobserve(observer)); | ||
const oCWU = this.componentWillUnmount || (() => { }); | ||
@@ -1286,4 +1299,5 @@ this.componentWillUnmount = () => { | ||
}; | ||
oCWM.call(this); | ||
oCDM.call(this); | ||
}; | ||
return component; | ||
}; | ||
@@ -1290,0 +1304,0 @@ } |
import { Document } from "./model"; | ||
import { ObservableArray } from "./observable"; | ||
declare class Component { | ||
setState(obj: any): void; | ||
componentWillUnmount(): void; | ||
componentWillMount(): void; | ||
} | ||
export declare function observe<D extends Document, G extends ObservableArray<D[]>>(input: G): <C extends typeof Component>(component: C) => void; | ||
export {}; | ||
import { Store } from "./store"; | ||
export declare function observe<D extends Document, G extends Store<D>>(store: G): (component: any) => any; |
@@ -1,15 +0,10 @@ | ||
class Component { | ||
setState(obj) { } | ||
componentWillUnmount() { } | ||
componentWillMount() { } | ||
} | ||
export function observe(input) { | ||
export function observe(store) { | ||
return function (component) { | ||
let oCWM = component.prototype.componentWillMount || (() => { }); | ||
component.prototype.componentWillMount = function () { | ||
let oCDM = component.prototype.componentDidMount || (() => { }); | ||
component.prototype.componentDidMount = function () { | ||
let unObservers = []; | ||
this.setState({}); | ||
const observer = () => this.setState({}); | ||
input.observe(observer); | ||
unObservers.push(() => input.unobserve(observer)); | ||
store.$$observableObject.observe(observer); | ||
unObservers.push(() => store.$$observableObject.unobserve(observer)); | ||
const oCWU = this.componentWillUnmount || (() => { }); | ||
@@ -20,5 +15,6 @@ this.componentWillUnmount = () => { | ||
}; | ||
oCWM.call(this); | ||
oCDM.call(this); | ||
}; | ||
return component; | ||
}; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { ObservableArray } from "./observable"; | ||
import { Document } from "./model"; | ||
@@ -10,3 +11,3 @@ export type deferredArray = { | ||
private $$idb; | ||
private $$observableObject; | ||
$$observableObject: ObservableArray<T[]>; | ||
private $$changes; | ||
@@ -21,4 +22,4 @@ private $$token; | ||
constructor({ name, token, persist, endpoint, debounceRate, model, encode, decode, }: { | ||
name: string; | ||
token: string; | ||
name?: string; | ||
token?: string; | ||
persist?: boolean; | ||
@@ -69,7 +70,3 @@ endpoint?: string; | ||
*/ | ||
$$syncTry(): Promise<{ | ||
pushed?: number; | ||
pulled?: number; | ||
exception?: string; | ||
}>; | ||
private $$syncTry; | ||
private $$sync; | ||
@@ -76,0 +73,0 @@ /** |
@@ -27,4 +27,2 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
this.sync = debounce(this.$$sync.bind(this), this.$$debounceRate); | ||
this.$$idb = new IDB(name); | ||
this.$$token = token; | ||
this.$$model = model || Document; | ||
@@ -40,5 +38,9 @@ if (encode) { | ||
} | ||
if (persist && endpoint) { | ||
if (name && persist) { | ||
this.$$idb = new IDB(name); | ||
this.$$loadFromLocal(); | ||
this.$$setupObservers(); | ||
} | ||
if (token && endpoint && name && persist) { | ||
this.$$token = token; | ||
this.$$syncService = new SyncService(endpoint, this.$$token, name); | ||
@@ -76,2 +78,4 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) | ||
return; | ||
const deserialized = (yield this.$$idb.values()).map((x) => this.$$deserialize(x)); | ||
@@ -85,2 +89,4 @@ this.$$observableObject.silently((o) => { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) | ||
return; | ||
this.$$lastProcessChanges = Date.now(); | ||
@@ -145,2 +151,4 @@ const toWriteLocally = []; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) | ||
return 0; | ||
return Number((yield this.$$idb.getMetadata("version")) || 0); | ||
@@ -183,2 +191,8 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.$$idb) { | ||
return { | ||
exception: "IDB not available", | ||
}; | ||
} | ||
; | ||
if (!this.$$syncService) { | ||
@@ -185,0 +199,0 @@ return { |
{ | ||
"name": "apical-store", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Mobx-Syncable-IndexedDB", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
import { Document } from "./model"; | ||
import { ObservableArray } from "./observable"; | ||
import { Store } from "./store"; | ||
class Component { | ||
setState(obj: any) {} | ||
componentWillUnmount() {} | ||
componentWillMount() {} | ||
} | ||
export function observe<D extends Document, G extends ObservableArray<D[]>>( | ||
input: G | ||
) { | ||
return function <C extends typeof Component>(component: C) { | ||
let oCWM = component.prototype.componentWillMount || (() => {}); | ||
component.prototype.componentWillMount = function () { | ||
export function observe<D extends Document, G extends Store<D>>( | ||
store: G | ||
): (component: any) => any { | ||
return function (component: any) { | ||
let oCDM = component.prototype.componentDidMount || (() => {}); | ||
component.prototype.componentDidMount = function () { | ||
let unObservers: (() => void)[] = []; | ||
this.setState({}); | ||
const observer = () => this.setState({}); | ||
input.observe(observer); | ||
unObservers.push(() => input.unobserve(observer)); | ||
(store as any).$$observableObject.observe(observer); | ||
unObservers.push(() => (store as any).$$observableObject.unobserve(observer)); | ||
const oCWU = this.componentWillUnmount || (() => {}); | ||
@@ -26,5 +20,6 @@ this.componentWillUnmount = () => { | ||
} | ||
oCWM.call(this); | ||
oCDM.call(this); | ||
}; | ||
return component; | ||
}; | ||
} |
@@ -5,3 +5,3 @@ import { Change, observable, ObservableArray } from "./observable"; | ||
import { debounce } from "./debounce"; | ||
import { Document, RecursivePartial } from "./model"; | ||
import { Document } from "./model"; | ||
@@ -13,7 +13,8 @@ export type deferredArray = { ts: number; data: string }[]; | ||
public deferredPresent: boolean = false; | ||
private $$idb: IDB; | ||
public onSyncStart: () => void = () => {}; | ||
public onSyncEnd: () => void = () => {}; | ||
private $$idb: IDB | undefined; | ||
private $$observableObject: ObservableArray<T[]> = observable([] as T[]); | ||
private $$changes: Change<T[]>[] = []; | ||
private $$token: string; | ||
private $$token: string | undefined; | ||
private $$syncService: SyncService | null = null; | ||
@@ -35,5 +36,7 @@ private $$debounceRate: number = 100; | ||
decode, | ||
onSyncStart, | ||
onSyncEnd, | ||
}: { | ||
name: string; | ||
token: string; | ||
name?: string; | ||
token?: string; | ||
persist?: boolean; | ||
@@ -45,6 +48,12 @@ endpoint?: string; | ||
decode?: (input: string) => string; | ||
onSyncStart?: () => void; | ||
onSyncEnd?: () => void; | ||
}) { | ||
this.$$idb = new IDB(name); | ||
this.$$token = token; | ||
this.$$model = model || Document; | ||
if (onSyncStart) { | ||
this.onSyncStart = onSyncStart; | ||
} | ||
if (onSyncEnd) { | ||
this.onSyncEnd = onSyncEnd; | ||
} | ||
if (encode) { | ||
@@ -59,5 +68,9 @@ this.$$encode = encode; | ||
} | ||
if (persist && endpoint) { | ||
if (name && persist) { | ||
this.$$idb = new IDB(name); | ||
this.$$loadFromLocal(); | ||
this.$$setupObservers(); | ||
} | ||
if (token && endpoint && name && persist) { | ||
this.$$token = token; | ||
this.$$syncService = new SyncService(endpoint, this.$$token, name); | ||
@@ -93,2 +106,3 @@ } | ||
private async $$loadFromLocal() { | ||
if (!this.$$idb) return; | ||
const deserialized = (await this.$$idb.values()).map((x) => | ||
@@ -103,2 +117,5 @@ this.$$deserialize(x) | ||
private async $$processChanges() { | ||
if (!this.$$idb) return; | ||
if (this.$$changes.length === 0) return; | ||
this.onSyncStart(); | ||
this.$$lastProcessChanges = Date.now(); | ||
@@ -131,2 +148,3 @@ | ||
await this.$$syncService.sendUpdates(toSendRemotely); | ||
this.onSyncEnd(); | ||
return; | ||
@@ -148,2 +166,3 @@ } catch (e) { | ||
this.deferredPresent = true; | ||
this.onSyncEnd(); | ||
} | ||
@@ -176,2 +195,3 @@ | ||
private async $$localVersion() { | ||
if (!this.$$idb) return 0; | ||
return Number((await this.$$idb.getMetadata("version")) || 0); | ||
@@ -196,3 +216,3 @@ } | ||
* *************************************************************************** | ||
* | ||
* | ||
* The sync mechanism is as follows: | ||
@@ -213,3 +233,3 @@ * 1. Fetch the local version | ||
*/ | ||
async $$syncTry(): Promise<{ | ||
private async $$syncTry(): Promise<{ | ||
pushed?: number; | ||
@@ -219,2 +239,7 @@ pulled?: number; | ||
}> { | ||
if (!this.$$idb) { | ||
return { | ||
exception: "IDB not available", | ||
}; | ||
} | ||
if (!this.$$syncService) { | ||
@@ -249,5 +274,12 @@ return { | ||
const conflict = remoteUpdates.rows.findIndex((y) => y.id === item.id); | ||
const comparison = Number( | ||
( | ||
remoteUpdates.rows[conflict] as | ||
| { id: string; data: string; ts?: string } | ||
| undefined | ||
)?.ts || remoteVersion | ||
); | ||
if (conflict === -1) { | ||
return true; | ||
} else if (x.ts > remoteVersion) { | ||
} else if (x.ts > comparison) { | ||
// there's a conflict, but the local change is newer | ||
@@ -308,11 +340,17 @@ remoteUpdates.rows.splice(conflict, 1); | ||
private async $$sync() { | ||
this.onSyncStart(); | ||
let tries: { exception?: string; pushed?: number; pulled?: number }[] = []; | ||
let exceptionOccurred = false; | ||
while (!exceptionOccurred) { | ||
const result = await this.$$syncTry(); | ||
if (result.exception) { | ||
exceptionOccurred = true; | ||
try { | ||
let exceptionOccurred = false; | ||
while (!exceptionOccurred) { | ||
const result = await this.$$syncTry(); | ||
if (result.exception) { | ||
exceptionOccurred = true; | ||
} | ||
tries.push(result); | ||
} | ||
tries.push(result); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
this.onSyncEnd(); | ||
return tries; | ||
@@ -379,4 +417,7 @@ } | ||
async isUpdated() { | ||
return this.$$syncService ? (await this.$$syncService.latestVersion() === await this.$$localVersion()) : true; | ||
return this.$$syncService | ||
? (await this.$$syncService.latestVersion()) === | ||
(await this.$$localVersion()) | ||
: true; | ||
} | ||
} | ||
} |
@@ -1019,3 +1019,3 @@ import { Store } from "../src/store"; | ||
it("Deferred changes must pushes only the latest change", async () =>{ | ||
it("Deferred changes must pushes only the latest change", async () => { | ||
{ | ||
@@ -1053,3 +1053,2 @@ // clearing local database before starting | ||
expect(store.list.length).toBe(1); | ||
@@ -1102,3 +1101,3 @@ expect( | ||
it("If there are already deferred changes, no updates shall be sent unless there's a sync process", async () =>{ | ||
it("If there are already deferred changes, no updates shall be sent unless there's a sync process", async () => { | ||
{ | ||
@@ -1105,0 +1104,0 @@ // clearing local database before starting |
@@ -112,3 +112,3 @@ { | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"include": ["src/**/*.ts", "tests/react.test.ts"], | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
211916
36
5815
1
42