apical-store
Advanced tools
Comparing version 0.0.76 to 0.0.77
@@ -559,7 +559,13 @@ (function (global, factory) { | ||
? target | ||
: new ObservableArrayMeta({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy; | ||
: Array.isArray(target) | ||
? new ObservableArrayMeta({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy | ||
: new ObservableObjectMeta({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy; | ||
this.observers = this.target[oMetaKey].observers; | ||
@@ -1184,7 +1190,2 @@ } | ||
/** | ||
* Enhances a React component to automatically re-render when the observed store changes. | ||
* @param store - An instance of Store that extends Document. | ||
* @returns A higher-order function that takes a React component as an argument. | ||
*/ | ||
function observe(store) { | ||
@@ -1197,4 +1198,4 @@ return function (component) { | ||
const observer = () => this.setState({}); | ||
if (Array.isArray(store)) { | ||
store.forEach((singleStore) => { | ||
store.forEach((singleStore) => { | ||
if (singleStore instanceof Store) { | ||
// @ts-ignore | ||
@@ -1205,10 +1206,11 @@ singleStore.$$observableObject.observe(observer); | ||
singleStore.$$observableObject.unobserve(observer)); | ||
}); | ||
} | ||
else { | ||
// @ts-ignore | ||
store.$$observableObject.observe(observer); | ||
// @ts-ignore | ||
store.$$observableObject.unobserve(observer); | ||
} | ||
} | ||
if (singleStore instanceof Observable) { | ||
singleStore.observe(observer); | ||
unObservers.push(() => singleStore.unobserve(observer)); | ||
} | ||
else { | ||
throw new Error("You're trying to observe something that is not an observable. Please make sure you're passing an instance of Observable or a store"); | ||
} | ||
}); | ||
const originalComponentWillUnmount = this.componentWillUnmount || (() => { }); | ||
@@ -1414,2 +1416,3 @@ this.componentWillUnmount = () => { | ||
exports.IDB = IDB; | ||
exports.Observable = Observable; | ||
exports.Store = Store; | ||
@@ -1416,0 +1419,0 @@ exports.SubDocument = SubDocument; |
@@ -6,1 +6,2 @@ export { Store } from "./store"; | ||
export { CloudFlareApexoDB } from "./persistence/remote"; | ||
export { Observable } from "./observable"; |
@@ -6,1 +6,2 @@ export { Store } from "./store"; | ||
export { CloudFlareApexoDB } from "./persistence/remote"; | ||
export { Observable } from "./observable"; |
@@ -35,3 +35,3 @@ import { proxiedArrayMethods } from "./arr"; | ||
} | ||
export declare class ObservableArrayMeta<G, T extends Array<G>> extends ObservableMeta<T> { | ||
export declare class ObservableArrayMeta<G, T extends Array<G>> extends ObservableMeta<any> { | ||
constructor(properties: MetaProperties<T>); | ||
@@ -38,0 +38,0 @@ get(target: T, key: keyof typeof proxiedArrayMethods): (<T_1 extends any[]>(this: observed<T_1>) => any) | (<T_1 extends any[]>(this: observed<T_1>) => number) | (<T_1 extends any[]>(this: observed<T_1>) => any) | (<T_1 extends any[]>(this: observed<T_1>) => number) | (<T_1 extends any[]>(this: observed<T_1>) => observed<T_1>) | (<T_1 extends any[]>(this: observed<T_1>, comparator: (a: any, b: any) => number) => observed<T_1>) | (<T_1 extends any[]>(this: observed<T_1>, filVal: any, start: number, end: number) => observed<T_1>) | (<T_1 extends any[]>(this: observed<T_1>, dest: number, start: number, end: number) => observed<T_1>) | (<T_1 extends any[]>(this: observed<T_1>) => any); |
import { observed, Observer } from "./types"; | ||
export declare class Observable<D> { | ||
export declare class Observable<D extends object> { | ||
/** | ||
@@ -7,8 +7,8 @@ * Observable array, this is the actual observable array | ||
*/ | ||
target: observed<D[]>; | ||
target: observed<D>; | ||
/** | ||
* An array of the all the observers registered to this observable | ||
*/ | ||
observers: Observer<D[]>[]; | ||
constructor(target: D[] | observed<D[]>); | ||
observers: Observer<D>[]; | ||
constructor(target: D | observed<D>); | ||
/** | ||
@@ -21,11 +21,11 @@ * | ||
*/ | ||
unobserve(observers?: Observer<D[]> | Observer<D[]>[]): Observer<D[]>[]; | ||
unobserve(observers?: Observer<D> | Observer<D>[]): Observer<D>[]; | ||
/** | ||
* Register a new observer | ||
*/ | ||
observe(observer: Observer<D[]>): void; | ||
observe(observer: Observer<D>): void; | ||
/** | ||
* Execute a callback silently (without calling the observers) | ||
*/ | ||
silently(work: (o: observed<D[]>) => any): void; | ||
silently(work: (o: observed<D>) => any): void; | ||
/** | ||
@@ -36,3 +36,3 @@ * Get a non-observed copy of the observable array | ||
*/ | ||
get copy(): D[]; | ||
get copy(): D; | ||
private __observe; | ||
@@ -39,0 +39,0 @@ private __unobserve; |
import * as utils from "./utils"; | ||
import { oMetaKey } from "./const"; | ||
import { ObservableArrayMeta } from "./meta"; | ||
import { ObservableArrayMeta, ObservableObjectMeta } from "./meta"; | ||
export class Observable { | ||
@@ -12,7 +12,13 @@ constructor(target) { | ||
? target | ||
: new ObservableArrayMeta({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy; | ||
: Array.isArray(target) | ||
? new ObservableArrayMeta({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy | ||
: new ObservableObjectMeta({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy; | ||
this.observers = this.target[oMetaKey].observers; | ||
@@ -19,0 +25,0 @@ } |
import { Document } from "./model"; | ||
import { Observable } from "./observable"; | ||
import { Store } from "./store"; | ||
@@ -8,2 +9,3 @@ /** | ||
*/ | ||
export declare function observe<D extends Document, G extends Store<D>>(store: G | G[]): (component: any) => any; | ||
export declare function observe<D extends object>(store: Observable<any>[]): <K>(component: K) => K; | ||
export declare function observe<D extends Document, G extends Store<D>>(store: G[]): <K>(component: K) => K; |
@@ -1,6 +0,3 @@ | ||
/** | ||
* Enhances a React component to automatically re-render when the observed store changes. | ||
* @param store - An instance of Store that extends Document. | ||
* @returns A higher-order function that takes a React component as an argument. | ||
*/ | ||
import { Observable } from "./observable"; | ||
import { Store } from "./store"; | ||
export function observe(store) { | ||
@@ -13,4 +10,4 @@ return function (component) { | ||
const observer = () => this.setState({}); | ||
if (Array.isArray(store)) { | ||
store.forEach((singleStore) => { | ||
store.forEach((singleStore) => { | ||
if (singleStore instanceof Store) { | ||
// @ts-ignore | ||
@@ -21,10 +18,11 @@ singleStore.$$observableObject.observe(observer); | ||
singleStore.$$observableObject.unobserve(observer)); | ||
}); | ||
} | ||
else { | ||
// @ts-ignore | ||
store.$$observableObject.observe(observer); | ||
// @ts-ignore | ||
store.$$observableObject.unobserve(observer); | ||
} | ||
} | ||
if (singleStore instanceof Observable) { | ||
singleStore.observe(observer); | ||
unObservers.push(() => singleStore.unobserve(observer)); | ||
} | ||
else { | ||
throw new Error("You're trying to observe something that is not an observable. Please make sure you're passing an instance of Observable or a store"); | ||
} | ||
}); | ||
const originalComponentWillUnmount = this.componentWillUnmount || (() => { }); | ||
@@ -31,0 +29,0 @@ this.componentWillUnmount = () => { |
{ | ||
"name": "apical-store", | ||
"version": "0.0.76", | ||
"version": "0.0.77", | ||
"description": "Mobx-Syncable-IndexedDB", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
@@ -5,2 +5,3 @@ export { Store } from "./store"; | ||
export { LocalPersistence, IDB, deferredArray } from "./persistence/local"; | ||
export { CloudFlareApexoDB } from "./persistence/remote"; | ||
export { CloudFlareApexoDB } from "./persistence/remote"; | ||
export { Observable } from "./observable"; |
@@ -198,3 +198,3 @@ import { proxiedArrayMethods } from "./arr"; | ||
export class ObservableArrayMeta<G, T extends Array<G>> extends ObservableMeta<T> { | ||
export class ObservableArrayMeta<G, T extends Array<G>> extends ObservableMeta<any> { | ||
constructor(properties: MetaProperties<T>) { | ||
@@ -201,0 +201,0 @@ super(properties, prepare.array); |
import * as utils from "./utils"; | ||
import { oMetaKey } from "./const"; | ||
import { observed, Observer } from "./types"; | ||
import { ObservableArrayMeta } from "./meta"; | ||
import { ObservableArrayMeta, ObservableObjectMeta } from "./meta"; | ||
export class Observable<D> { | ||
export class Observable<D extends object> { | ||
/** | ||
@@ -11,16 +11,21 @@ * Observable array, this is the actual observable array | ||
*/ | ||
target: observed<D[]>; | ||
target: observed<D>; | ||
/** | ||
* An array of the all the observers registered to this observable | ||
*/ | ||
observers: Observer<D[]>[] = []; | ||
constructor(target: D[] | observed<D[]>) { | ||
observers: Observer<D>[] = []; | ||
constructor(target: D | observed<D>) { | ||
this.target = Observable.isObservable(target) | ||
? (target as observed<D[]>) | ||
: new ObservableArrayMeta({ | ||
? (target as observed<D>) | ||
: Array.isArray(target) | ||
? new ObservableArrayMeta<D, D[]>({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy | ||
: new ObservableObjectMeta<D>({ | ||
target: target, | ||
ownKey: "", | ||
parent: null, | ||
}).proxy; | ||
@@ -31,3 +36,3 @@ this.observers = this.target[oMetaKey].observers; | ||
/** | ||
* | ||
* | ||
* Remove an observer from the list of observers | ||
@@ -38,3 +43,3 @@ * can be given a single observer | ||
*/ | ||
unobserve(observers?: Observer<D[]> | Observer<D[]>[]) { | ||
unobserve(observers?: Observer<D> | Observer<D>[]) { | ||
if (!observers) return this.__unobserve(); | ||
@@ -45,7 +50,6 @@ else if (Array.isArray(observers)) return this.__unobserve(observers); | ||
/** | ||
* Register a new observer | ||
*/ | ||
observe(observer: Observer<D[]>) { | ||
observe(observer: Observer<D>) { | ||
this.__observe(observer); | ||
@@ -57,3 +61,3 @@ } | ||
*/ | ||
silently(work: (o: observed<D[]>) => any) { | ||
silently(work: (o: observed<D>) => any) { | ||
this.target[oMetaKey].runningSilentWork = true; | ||
@@ -72,7 +76,7 @@ try { | ||
*/ | ||
get copy(): D[] { | ||
get copy(): D { | ||
return utils.copy(this.target); | ||
} | ||
private __observe(observer: Observer<D[]>) { | ||
private __observe(observer: Observer<D>) { | ||
const observers = this.target[oMetaKey].observers; | ||
@@ -84,3 +88,3 @@ if (!observers.some((o) => o === observer)) { | ||
private __unobserve(observers?: Observer<D[]>[]) { | ||
private __unobserve(observers?: Observer<D>[]) { | ||
const existingObs = this.target[oMetaKey].observers; | ||
@@ -96,3 +100,3 @@ let length = existingObs.length; | ||
let spliced: Observer<D[]>[] = []; | ||
let spliced: Observer<D>[] = []; | ||
for (let index = 0; index < observers.length; index++) { | ||
@@ -99,0 +103,0 @@ const observer = observers[index]; |
import { Document } from "./model"; | ||
import { Observable } from "./observable"; | ||
import { Store } from "./store"; | ||
@@ -9,5 +10,12 @@ | ||
*/ | ||
export function observe<D extends object>( | ||
store: Observable<any>[] | ||
): <K>(component: K) => K; | ||
export function observe<D extends Document, G extends Store<D>>( | ||
store: G | G[] | ||
): (component: any) => any { | ||
store: G[] | ||
): <K>(component: K) => K; | ||
export function observe<D extends Document, G extends Store<D>>( | ||
store: G[] | Observable<any>[] | ||
): <K>(component: K) => K { | ||
return function (component: any) { | ||
@@ -22,4 +30,4 @@ const originalComponentDidMount = | ||
if (Array.isArray(store)) { | ||
store.forEach((singleStore) => { | ||
store.forEach((singleStore) => { | ||
if (singleStore instanceof Store) { | ||
// @ts-ignore | ||
@@ -31,9 +39,12 @@ singleStore.$$observableObject.observe(observer); | ||
); | ||
}); | ||
} else { | ||
// @ts-ignore | ||
store.$$observableObject.observe(observer); | ||
// @ts-ignore | ||
store.$$observableObject.unobserve(observer); | ||
} | ||
} | ||
if (singleStore instanceof Observable) { | ||
singleStore.observe(observer); | ||
unObservers.push(() => singleStore.unobserve(observer)); | ||
} else { | ||
throw new Error( | ||
"You're trying to observe something that is not an observable. Please make sure you're passing an instance of Observable or a store" | ||
); | ||
} | ||
}); | ||
@@ -40,0 +51,0 @@ const originalComponentWillUnmount = |
@@ -12,3 +12,3 @@ import { Change, Observable } from "./observable"; | ||
public onSyncEnd: () => void = () => {}; | ||
private $$observableObject: Observable<T> = new Observable([] as T[]); | ||
private $$observableObject: Observable<T[]> = new Observable([] as T[]); | ||
private $$changes: Change<T[]>[] = []; | ||
@@ -15,0 +15,0 @@ private $$loaded: boolean = false; |
@@ -181,3 +181,3 @@ import { describe, test, it, expect, vi } from "vitest"; | ||
const observer = (changes: Change<number[]>[]) => {}; | ||
const observableArray = new Observable<number>([]); | ||
const observableArray = new Observable<number[]>([]); | ||
observableArray.observe(observer); | ||
@@ -331,2 +331,73 @@ const result = observableArray.unobserve( | ||
}); | ||
describe("observing objects", ()=>{ | ||
it("observing objects",async ()=>{ | ||
const object = { | ||
name: "alex", | ||
age: 12, | ||
days: [1,2,3,4], | ||
children: [ | ||
{ | ||
name: "john", | ||
age: 12, | ||
}, | ||
{ | ||
name: "jane", | ||
age: 14, | ||
} | ||
] | ||
}; | ||
const observableObject = new Observable(object); | ||
let changes: Change<any>[] = []; | ||
observableObject.observe((c) => { | ||
changes = c; | ||
}); | ||
observableObject.target.name = "bob"; | ||
observableObject.target.age = 13; | ||
observableObject.target.children[0].name = "jim"; | ||
observableObject.target.children[0].age = 13; | ||
observableObject.target.days.push(5); | ||
observableObject.target.children[1].name = "jill"; | ||
observableObject.target.children[1].age = 15; | ||
observableObject.target.days.pop(); | ||
observableObject.target.days.shift(); | ||
observableObject.target.days.unshift(0); | ||
observableObject.target.days.splice(1, 1); | ||
observableObject.target.days.splice(1, 0, 10); | ||
await new Promise((r) => setTimeout(r, 0)); | ||
expect(changes.length).toEqual(12); | ||
expect(changes[0].type).toEqual("update"); | ||
expect(changes[0].path).toEqual(["name"]); | ||
expect(changes[0].value).toEqual("bob"); | ||
expect(changes[0].oldValue).toEqual("alex"); | ||
expect(changes[1].type).toEqual("update"); | ||
expect(changes[1].path).toEqual(["age"]); | ||
expect(changes[1].value).toEqual(13); | ||
expect(changes[1].oldValue).toEqual(12); | ||
expect(changes[2].type).toEqual("update"); | ||
expect(changes[2].path).toEqual(["children", 0, "name"]); | ||
expect(changes[2].value).toEqual("jim"); | ||
expect(changes[2].oldValue).toEqual("john"); | ||
expect(changes[3].type).toEqual("update"); | ||
expect(changes[3].path).toEqual(["children", 0, "age"]); | ||
expect(changes[3].value).toEqual(13); | ||
expect(changes[3].oldValue).toEqual(12); | ||
expect(changes[4].type).toBe("insert"); | ||
expect(changes[4].path).toEqual(["days", 4]); | ||
expect(changes[4].value).toEqual(5); | ||
expect(changes[4].oldValue).toBeUndefined(); | ||
expect(observableObject.target.name).toBe("bob"); | ||
}); | ||
}); | ||
}); |
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
251227
6803