@aurox/distributed-observables
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -43,4 +43,5 @@ import { TypedEmitter } from 'tiny-typed-emitter'; | ||
private persist; | ||
setValue(value: T): Promise<void>; | ||
update(recipe: (draft: T) => void | T): void; | ||
detach(): void; | ||
} |
@@ -129,2 +129,8 @@ "use strict"; | ||
} | ||
async setValue(value) { | ||
this._debouncedPersist.cancel(); | ||
this._currentState = value; | ||
this._patchesInQueue = [{ op: 'replace', path: [], value }]; | ||
await this.persist(); | ||
} | ||
update(recipe) { | ||
@@ -135,3 +141,3 @@ if (!this._initialized) { | ||
if (this._currentState === null) { | ||
return; | ||
throw new Error('Observable state is null'); | ||
} | ||
@@ -138,0 +144,0 @@ const [newState, patches] = (0, immer_1.produceWithPatches)(this._currentState, recipe); |
@@ -18,5 +18,5 @@ import { Patch } from 'immer'; | ||
setChannelClient(channel: ChannelClient): void; | ||
getObserver<T extends ObservableState>(topic: string, options?: Partial<ObservableOptions>): Observable<any>; | ||
getObserver<T extends ObservableState>(topic: string, options?: Partial<ObservableOptions>): Observable<T>; | ||
private requestSnapshot; | ||
private applyPatch; | ||
} |
export { Draft, Patch } from 'immer'; | ||
export declare type ObservableState<T = Record<string, unknown>> = T extends Record<string, unknown> ? T : never; | ||
export declare type ObservableState<T = object> = T extends object ? T : never; | ||
export interface Identity { | ||
@@ -4,0 +4,0 @@ uuid: string; |
@@ -9,2 +9,2 @@ import { IdentityWithOwner, PatchRequest, PatchResponse } from '../types'; | ||
} | ||
export declare function applyPatchRequest<T extends Record<string, unknown> | null>(state: T, stateRevision: number, request: PatchRequest<IdentityWithOwner>): ApplyPatchRequestResult<T>; | ||
export declare function applyPatchRequest<T extends object | null>(state: T, stateRevision: number, request: PatchRequest<IdentityWithOwner>): ApplyPatchRequestResult<T>; |
{ | ||
"name": "@aurox/distributed-observables", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A set of isomorphic helpers to enable distributed object sharing using the observer pattern", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -24,3 +24,3 @@ import { enablePatches } from 'immer'; | ||
const observer = manager.getObserver('person1'); | ||
const observer = manager.getObserver<{ foo: { bar: number } }>('person1'); | ||
@@ -30,3 +30,3 @@ await observer.initialize(); | ||
expect(observer.getStateSync().foo).toBe(undefined); | ||
expect(observer.getStateSync()?.foo).toBe(undefined); | ||
@@ -37,3 +37,3 @@ observer.update(draft => { | ||
expect(observer.getStateSync().foo.bar).toBe(2); | ||
expect(observer.getStateSync()?.foo.bar).toBe(2); | ||
}); | ||
@@ -46,3 +46,3 @@ | ||
const observer = manager.getObserver('person1'); | ||
const observer = manager.getObserver<{ prop: number[]; baz: number; foo: { bar: number } }>('person1'); | ||
@@ -52,3 +52,3 @@ await observer.initialize(); | ||
expect(observer.getStateSync().foo).toBe(undefined); | ||
expect(observer.getStateSync()?.foo).toBe(undefined); | ||
@@ -74,7 +74,7 @@ observer.update(draft => { | ||
expect(observer.getStateSync().foo.bar).toBe(4); | ||
expect(observer.getStateSync().baz).toBe(10); | ||
expect(observer.getStateSync().prop[0]).toBe(12); | ||
expect(observer.getStateSync().prop[2]).toBe(56); | ||
expect(observer.getStateSync()?.foo.bar).toBe(4); | ||
expect(observer.getStateSync()?.baz).toBe(10); | ||
expect(observer.getStateSync()?.prop[0]).toBe(12); | ||
expect(observer.getStateSync()?.prop[2]).toBe(56); | ||
}); | ||
}); |
@@ -196,2 +196,12 @@ import { TypedEmitter } from 'tiny-typed-emitter'; | ||
public async setValue(value: T) { | ||
this._debouncedPersist.cancel(); | ||
this._currentState = value; | ||
this._patchesInQueue = [{ op: 'replace', path: [], value }]; | ||
await this.persist(); | ||
} | ||
public update(recipe: (draft: T) => void | T) { | ||
@@ -203,3 +213,3 @@ if (!this._initialized) { | ||
if (this._currentState === null) { | ||
return; | ||
throw new Error('Observable state is null'); | ||
} | ||
@@ -206,0 +216,0 @@ |
@@ -31,3 +31,3 @@ import { v4 as uuidV4 } from 'uuid'; | ||
public getObserver<T extends ObservableState>(topic: string, options?: Partial<ObservableOptions>) { | ||
public getObserver<T extends ObservableState>(topic: string, options?: Partial<ObservableOptions>): Observable<T> { | ||
let observer = this._observers.get(topic); | ||
@@ -34,0 +34,0 @@ |
export { Draft, Patch } from 'immer'; | ||
export type ObservableState<T = Record<string, unknown>> = T extends Record<string, unknown> ? T : never; | ||
export type ObservableState<T = object> = T extends object ? T : never; | ||
@@ -5,0 +5,0 @@ export interface Identity { |
@@ -10,3 +10,3 @@ import { applyPatches } from 'immer'; | ||
export function applyPatchRequest<T extends Record<string, unknown> | null>( | ||
export function applyPatchRequest<T extends object | null>( | ||
state: T, | ||
@@ -13,0 +13,0 @@ stateRevision: number, |
Sorry, the diff of this file is not supported yet
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
82425
1496