From 02281b36ed96c1346cf2e6f078c3d85e1b955bda Mon Sep 17 00:00:00 2001 From: Andrew Bulat Date: Tue, 19 Nov 2024 04:07:41 +0000 Subject: [PATCH] Move types required for user-provided LiveObjects typings to ably.d.ts --- ably.d.ts | 25 +++++++++++++++++++--- src/plugins/liveobjects/livemap.ts | 6 +++--- src/plugins/liveobjects/liveobjects.ts | 3 +-- src/plugins/liveobjects/liveobjectspool.ts | 4 ++-- src/plugins/liveobjects/typings.ts | 22 ------------------- 5 files changed, 28 insertions(+), 32 deletions(-) delete mode 100644 src/plugins/liveobjects/typings.ts diff --git a/ably.d.ts b/ably.d.ts index 9154ef81c..35254837f 100644 --- a/ably.d.ts +++ b/ably.d.ts @@ -2029,13 +2029,32 @@ export declare interface PushChannel { * Enables the LiveObjects state to be subscribed to for a channel. */ export declare interface LiveObjects { - getRoot(): Promise; + getRoot(): Promise>; } // TODO: document public API for LiveObjects -export declare interface LiveMap extends LiveObject { - get(key: string): LiveObject | StateValue | undefined; +declare global { + // a global interface which can be used by users to define their own types for LiveObjects. + export interface LiveObjectsTypes { + [key: string]: unknown; + } +} + +// LiveMap type representation of how it looks to the user. A mapping of string keys to scalar values (StateValue) or other Live Objects. +export type LiveMapType = { [key: string]: StateValue | LiveMap | LiveCounter | undefined }; + +export type DefaultRoot = + // we need a way to know when no types were provided by the user. + // we expect a "root" property to be set on LiveObjectsTypes interface, e.g. it won't be "unknown" anymore + unknown extends LiveObjectsTypes['root'] + ? LiveMapType // no types provided by the user, use the default map type for the root + : LiveObjectsTypes['root'] extends LiveMapType + ? LiveObjectsTypes['root'] // "root" was provided by the user, and it is of an expected type, we can use this interface for the root object in LiveObjects. + : `Provided type definition for the "root" object in LiveObjectsTypes is not of an expected LiveMapType type`; + +export declare interface LiveMap extends LiveObject { + get(key: TKey): T[TKey]; size(): number; } diff --git a/src/plugins/liveobjects/livemap.ts b/src/plugins/liveobjects/livemap.ts index 900a0ab90..518acf834 100644 --- a/src/plugins/liveobjects/livemap.ts +++ b/src/plugins/liveobjects/livemap.ts @@ -1,6 +1,7 @@ import deepEqual from 'deep-equal'; import type BaseClient from 'common/lib/client/baseclient'; +import type * as API from '../../../ably'; import { LiveObject, LiveObjectData, LiveObjectUpdate, LiveObjectUpdateNoop } from './liveobject'; import { LiveObjects } from './liveobjects'; import { @@ -14,7 +15,6 @@ import { StateValue, } from './statemessage'; import { DefaultTimeserial, Timeserial } from './timeserial'; -import { LiveMapType } from './typings'; export interface ObjectIdStateData { /** A reference to another state object, used to support composable state objects. */ @@ -47,7 +47,7 @@ export interface LiveMapUpdate extends LiveObjectUpdate { update: { [keyName: string]: 'updated' | 'removed' }; } -export class LiveMap extends LiveObject { +export class LiveMap extends LiveObject { constructor( liveObjects: LiveObjects, private _semantics: MapSemantics, @@ -63,7 +63,7 @@ export class LiveMap extends LiveObject( + static zeroValue( liveobjects: LiveObjects, objectId?: string, regionalTimeserial?: Timeserial, diff --git a/src/plugins/liveobjects/liveobjects.ts b/src/plugins/liveobjects/liveobjects.ts index 11566c46c..e4ab07c6e 100644 --- a/src/plugins/liveobjects/liveobjects.ts +++ b/src/plugins/liveobjects/liveobjects.ts @@ -9,7 +9,6 @@ import { LiveObjectsPool, ROOT_OBJECT_ID } from './liveobjectspool'; import { StateMessage } from './statemessage'; import { LiveCounterDataEntry, SyncLiveObjectsDataPool } from './syncliveobjectsdatapool'; import { DefaultTimeserial, Timeserial } from './timeserial'; -import { DefaultRoot, LiveMapType } from './typings'; enum LiveObjectsEvents { SyncCompleted = 'SyncCompleted', @@ -48,7 +47,7 @@ export class LiveObjects { * A user can provide an explicit type for the getRoot method to explicitly set the LiveObjects type structure on this particular channel. * This is useful when working with LiveObjects on multiple channels with different underlying data. */ - async getRoot(): Promise> { + async getRoot(): Promise> { // SYNC is currently in progress, wait for SYNC sequence to finish if (this._syncInProgress) { await this._eventEmitter.once(LiveObjectsEvents.SyncCompleted); diff --git a/src/plugins/liveobjects/liveobjectspool.ts b/src/plugins/liveobjects/liveobjectspool.ts index 273e74197..9b0a524d7 100644 --- a/src/plugins/liveobjects/liveobjectspool.ts +++ b/src/plugins/liveobjects/liveobjectspool.ts @@ -1,5 +1,6 @@ import type BaseClient from 'common/lib/client/baseclient'; import type RealtimeChannel from 'common/lib/client/realtimechannel'; +import type * as API from '../../../ably'; import { LiveCounter } from './livecounter'; import { LiveMap } from './livemap'; import { LiveObject } from './liveobject'; @@ -7,7 +8,6 @@ import { BufferedStateMessage, LiveObjects } from './liveobjects'; import { ObjectId } from './objectid'; import { MapSemantics, StateMessage, StateOperation, StateOperationAction } from './statemessage'; import { DefaultTimeserial, Timeserial } from './timeserial'; -import { LiveMapType } from './typings'; export const ROOT_OBJECT_ID = 'root'; @@ -194,7 +194,7 @@ export class LiveObjectsPool { } private _handleMapCreate(stateOperation: StateOperation, opRegionalTimeserial: Timeserial): void { - let map: LiveMap; + let map: LiveMap; if (this._client.Utils.isNil(stateOperation.map)) { // if a map object is missing for the MAP_CREATE op, the initial value is implicitly a zero-value map. map = LiveMap.zeroValue(this._liveObjects, stateOperation.objectId, opRegionalTimeserial); diff --git a/src/plugins/liveobjects/typings.ts b/src/plugins/liveobjects/typings.ts deleted file mode 100644 index 879b6a29c..000000000 --- a/src/plugins/liveobjects/typings.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { LiveCounter } from './livecounter'; -import { LiveMap } from './livemap'; -import { StateValue } from './statemessage'; - -declare global { - // define a global interface which can be used by users to define their own types for LiveObjects. - export interface LiveObjectsTypes { - [key: string]: unknown; - } -} - -// LiveMap type representation of how it looks to the end-user. A mapping of string keys to the scalar values (StateValue) or other Live Objects. -export type LiveMapType = { [key: string]: StateValue | LiveMap | LiveCounter | undefined }; - -export type DefaultRoot = - // we need a way to understand when no types were provided by the user. - // we expect a "root" property to be set on LiveObjectsTypes interface, e.g. it won't be "unknown" anymore - unknown extends LiveObjectsTypes['root'] - ? LiveMapType // no types provided by the user, use the default map type for the root - : LiveObjectsTypes['root'] extends LiveMapType - ? LiveObjectsTypes['root'] // "root" was provided by the user, and it is of an expected type, we can use this interface for the root object in LiveObjects. - : `Provided type definition for the "root" object in LiveObjectsTypes is not of an expected LiveMapType type`;