Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions core/ArSyncModel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ interface Change {
declare type ChangeCallback = (change: Change) => void;
declare type LoadCallback = () => void;
declare type ConnectionCallback = (status: boolean) => void;
declare type SubscriptionType = 'load' | 'change' | 'connection' | 'destroy';
declare type SubscriptionCallback = ChangeCallback | LoadCallback | ConnectionCallback;
declare type SubscriptionCallbackMap = {
load: LoadCallback;
change: ChangeCallback;
connection: ConnectionCallback;
destroy: LoadCallback;
};
declare type SubscriptionType = keyof SubscriptionCallbackMap;
declare type ArSyncModelRef = {
key: string;
count: number;
Expand Down Expand Up @@ -44,12 +49,12 @@ export default class ArSyncModel<T> {
immutable: boolean;
});
onload(callback: LoadCallback): void;
subscribeOnce(event: SubscriptionType, callback: SubscriptionCallback): {
subscribeOnce<T extends SubscriptionType>(event: T, callback: SubscriptionCallbackMap[T]): {
unsubscribe: () => void;
};
dig<P extends Path>(path: P): DigResult<T, P> | null;
static digData<Data, P extends Path>(data: Data, path: P): DigResult<Data, P>;
subscribe(event: SubscriptionType, callback: SubscriptionCallback): {
subscribe<T extends SubscriptionType>(event: T, callback: SubscriptionCallbackMap[T]): {
unsubscribe: () => void;
};
release(): void;
Expand Down
4 changes: 2 additions & 2 deletions core/ArSyncModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ var ArSyncModel = /** @class */ (function () {
this.subscribeOnce('load', callback);
};
ArSyncModel.prototype.subscribeOnce = function (event, callback) {
var subscription = this.subscribe(event, function (arg) {
callback(arg);
var subscription = this.subscribe(event, function (e) {
callback(e);
subscription.unsubscribe();
});
return subscription;
Expand Down
4 changes: 3 additions & 1 deletion core/ArSyncStore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ export declare type Request = {
api: string;
query: any;
params?: any;
id?: any;
id?: IDType;
};
declare type IDType = number | string;
export declare class ArSyncStore {
immutable: boolean;
markedForFreezeObjects: any[];
Expand Down Expand Up @@ -34,3 +35,4 @@ export declare class ArSyncStore {
freezeMarked(): void;
release(): void;
}
export {};
17 changes: 11 additions & 6 deletions src/core/ArSyncModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ interface Change { path: Path; value: any }
type ChangeCallback = (change: Change) => void
type LoadCallback = () => void
type ConnectionCallback = (status: boolean) => void
type SubscriptionType = 'load' | 'change' | 'connection' | 'destroy'
type SubscriptionCallback = ChangeCallback | LoadCallback | ConnectionCallback
type SubscriptionCallbackMap = {
load: LoadCallback
change: ChangeCallback
connection: ConnectionCallback
destroy: LoadCallback
}
type SubscriptionType = keyof SubscriptionCallbackMap
type ArSyncModelRef = { key: string; count: number; timer: number | null; model: ArSyncStore }

type PathFirst<P extends Readonly<any[]>> = ((...args: P) => void) extends (first: infer First, ...other: any) => void ? First : never
Expand Down Expand Up @@ -62,9 +67,9 @@ export default class ArSyncModel<T> {
onload(callback: LoadCallback) {
this.subscribeOnce('load', callback)
}
subscribeOnce(event: SubscriptionType, callback: SubscriptionCallback) {
const subscription = this.subscribe(event, (arg) => {
(callback as (arg: any) => void)(arg)
subscribeOnce<T extends SubscriptionType>(event: T, callback: SubscriptionCallbackMap[T]) {
const subscription = this.subscribe(event, (e?: any) => {
callback(e)
subscription.unsubscribe()
})
return subscription
Expand All @@ -86,7 +91,7 @@ export default class ArSyncModel<T> {
}
return dig(data, path)
}
subscribe(event: SubscriptionType, callback: SubscriptionCallback): { unsubscribe: () => void } {
subscribe<T extends SubscriptionType>(event: T, callback: SubscriptionCallbackMap[T]): { unsubscribe: () => void } {
const id = this._listenerSerial++
const subscription = this._ref.model.subscribe(event, callback)
let unsubscribed = false
Expand Down