From 977e7a452eb06bccfe68f9b97a6f7b28fb14da11 Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 19 Feb 2026 18:47:57 +0900 Subject: [PATCH 1/2] Detailed type for `subscribe(eventNameLiteral, event => {})` --- src/core/ArSyncModel.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/core/ArSyncModel.ts b/src/core/ArSyncModel.ts index 4781d85..595ef5f 100644 --- a/src/core/ArSyncModel.ts +++ b/src/core/ArSyncModel.ts @@ -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

> = ((...args: P) => void) extends (first: infer First, ...other: any) => void ? First : never @@ -62,9 +67,9 @@ export default class ArSyncModel { 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(event: T, callback: SubscriptionCallbackMap[T]) { + const subscription = this.subscribe(event, (e?: any) => { + callback(e) subscription.unsubscribe() }) return subscription @@ -86,7 +91,7 @@ export default class ArSyncModel { } return dig(data, path) } - subscribe(event: SubscriptionType, callback: SubscriptionCallback): { unsubscribe: () => void } { + subscribe(event: T, callback: SubscriptionCallbackMap[T]): { unsubscribe: () => void } { const id = this._listenerSerial++ const subscription = this._ref.model.subscribe(event, callback) let unsubscribed = false From c6a97c486da63473b4e23836b6bef49d31cfa7fd Mon Sep 17 00:00:00 2001 From: tompng Date: Thu, 19 Feb 2026 18:56:54 +0900 Subject: [PATCH 2/2] build --- core/ArSyncModel.d.ts | 13 +++++++++---- core/ArSyncModel.js | 4 ++-- core/ArSyncStore.d.ts | 4 +++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/ArSyncModel.d.ts b/core/ArSyncModel.d.ts index 4ad3487..713a9f0 100644 --- a/core/ArSyncModel.d.ts +++ b/core/ArSyncModel.d.ts @@ -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; @@ -44,12 +49,12 @@ export default class ArSyncModel { immutable: boolean; }); onload(callback: LoadCallback): void; - subscribeOnce(event: SubscriptionType, callback: SubscriptionCallback): { + subscribeOnce(event: T, callback: SubscriptionCallbackMap[T]): { unsubscribe: () => void; }; dig

(path: P): DigResult | null; static digData(data: Data, path: P): DigResult; - subscribe(event: SubscriptionType, callback: SubscriptionCallback): { + subscribe(event: T, callback: SubscriptionCallbackMap[T]): { unsubscribe: () => void; }; release(): void; diff --git a/core/ArSyncModel.js b/core/ArSyncModel.js index 4fae12a..c803d87 100644 --- a/core/ArSyncModel.js +++ b/core/ArSyncModel.js @@ -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; diff --git a/core/ArSyncStore.d.ts b/core/ArSyncStore.d.ts index 5b780b5..fa203c5 100644 --- a/core/ArSyncStore.d.ts +++ b/core/ArSyncStore.d.ts @@ -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[]; @@ -34,3 +35,4 @@ export declare class ArSyncStore { freezeMarked(): void; release(): void; } +export {};