From 3e95eb13573487b1408f0c7f8eae8ca4f38f7cfc Mon Sep 17 00:00:00 2001 From: Andrew Bulat Date: Wed, 2 Oct 2024 04:07:50 +0100 Subject: [PATCH] Add LiveObjectsPool to LiveObjects and naive implementation of `getRoot` method --- scripts/moduleReport.ts | 1 + src/plugins/liveobjects/liveobjects.ts | 9 +++++ test/common/modules/private_api_recorder.js | 1 + test/realtime/live_objects.test.js | 39 +++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/scripts/moduleReport.ts b/scripts/moduleReport.ts index 7183619c6..dce162b9c 100644 --- a/scripts/moduleReport.ts +++ b/scripts/moduleReport.ts @@ -311,6 +311,7 @@ async function checkLiveObjectsPluginFiles() { const allowedFiles = new Set([ 'src/plugins/liveobjects/index.ts', 'src/plugins/liveobjects/liveobject.ts', + 'src/plugins/liveobjects/liveobjects.ts', 'src/plugins/liveobjects/liveobjectspool.ts', ]); diff --git a/src/plugins/liveobjects/liveobjects.ts b/src/plugins/liveobjects/liveobjects.ts index 6dfed511f..6ba94384f 100644 --- a/src/plugins/liveobjects/liveobjects.ts +++ b/src/plugins/liveobjects/liveobjects.ts @@ -1,12 +1,21 @@ import type BaseClient from 'common/lib/client/baseclient'; import type RealtimeChannel from 'common/lib/client/realtimechannel'; +import { LiveMap } from './livemap'; +import { LiveObjectsPool, ROOT_OBJECT_ID } from './liveobjectspool'; export class LiveObjects { private _client: BaseClient; private _channel: RealtimeChannel; + private _liveObjectsPool: LiveObjectsPool; constructor(channel: RealtimeChannel) { this._channel = channel; this._client = channel.client; + this._liveObjectsPool = new LiveObjectsPool(this); + } + + async getRoot(): Promise { + // TODO: wait for SYNC sequence to finish to return root + return this._liveObjectsPool.get(ROOT_OBJECT_ID) as LiveMap; } } diff --git a/test/common/modules/private_api_recorder.js b/test/common/modules/private_api_recorder.js index 57cc6c55d..848004242 100644 --- a/test/common/modules/private_api_recorder.js +++ b/test/common/modules/private_api_recorder.js @@ -44,6 +44,7 @@ define(['test/support/output_directory_paths'], function (outputDirectoryPaths) 'call.http._getHosts', 'call.http.checkConnectivity', 'call.http.doUri', + 'call.LiveObject.getObjectId', 'call.msgpack.decode', 'call.msgpack.encode', 'call.presence._myMembers.put', diff --git a/test/realtime/live_objects.test.js b/test/realtime/live_objects.test.js index cda9b88c7..58f891d0d 100644 --- a/test/realtime/live_objects.test.js +++ b/test/realtime/live_objects.test.js @@ -55,6 +55,45 @@ define(['ably', 'shared_helper', 'async', 'chai', 'live_objects'], function ( const channel = client.channels.get('channel'); expect(channel.liveObjects.constructor.name).to.equal('LiveObjects'); }); + + describe('LiveObjects instance', () => { + /** @nospec */ + it('getRoot() returns LiveMap instance', async function () { + const helper = this.test.helper; + const client = LiveObjectsRealtime(helper); + + await monitorConnectionThenCloseAndFinish( + helper, + async () => { + const channel = client.channels.get('channel'); + const liveObjects = channel.liveObjects; + const root = await liveObjects.getRoot(); + + expect(root.constructor.name).to.equal('LiveMap'); + }, + client, + ); + }); + + /** @nospec */ + it('getRoot() returns live object with id "root"', async function () { + const helper = this.test.helper; + const client = LiveObjectsRealtime(helper); + + await monitorConnectionThenCloseAndFinish( + helper, + async () => { + const channel = client.channels.get('channel'); + const liveObjects = channel.liveObjects; + const root = await liveObjects.getRoot(); + + helper.recordPrivateApi('call.LiveObject.getObjectId'); + expect(root.getObjectId()).to.equal('root'); + }, + client, + ); + }); + }); }); }); });