Skip to content

Commit

Permalink
Implement LiveObjects.getRoot() method
Browse files Browse the repository at this point in the history
Resolves DTP-951
  • Loading branch information
VeskeR committed Oct 16, 2024
1 parent 2f3bcf8 commit 0cdc7c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/plugins/liveobjects/liveobjects.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import type BaseClient from 'common/lib/client/baseclient';
import type RealtimeChannel from 'common/lib/client/realtimechannel';
import type * as API from '../../../ably';
import type EventEmitter from 'common/lib/util/eventemitter';
import { LiveCounter } from './livecounter';
import { LiveMap } from './livemap';
import { LiveObject } from './liveobject';
import { LiveObjectsPool, ROOT_OBJECT_ID } from './liveobjectspool';
import { StateMessage } from './statemessage';
import { SyncLiveObjectsDataPool } from './syncliveobjectsdatapool';

enum LiveObjectsEvents {
SyncCompleted = 'SyncCompleted',
}

export class LiveObjects {
private _client: BaseClient;
private _channel: RealtimeChannel;
// composition over inheritance since we cannot import class directly into plugin code.
// instead we obtain a class type from the client
private _eventEmitter: EventEmitter;
private _liveObjectsPool: LiveObjectsPool;
private _syncLiveObjectsDataPool: SyncLiveObjectsDataPool;
private _syncInProgress: boolean;
Expand All @@ -20,14 +28,24 @@ export class LiveObjects {
constructor(channel: RealtimeChannel) {
this._channel = channel;
this._client = channel.client;
this._eventEmitter = new this._client.EventEmitter(this._client.logger);
this._liveObjectsPool = new LiveObjectsPool(this);
this._syncLiveObjectsDataPool = new SyncLiveObjectsDataPool(this);
this._syncInProgress = true;
}

async getRoot(): Promise<LiveMap> {
// TODO: wait for SYNC sequence to finish to return root
return this._liveObjectsPool.get(ROOT_OBJECT_ID) as LiveMap;
if (!this._syncInProgress) {
// SYNC is finished, can return immediately root object from pool
return this._liveObjectsPool.get(ROOT_OBJECT_ID) as LiveMap;
}

// otherwise wait for SYNC sequence to finish
return new Promise((res) => {
this._eventEmitter.once(LiveObjectsEvents.SyncCompleted, () => {
res(this._liveObjectsPool.get(ROOT_OBJECT_ID) as LiveMap);
});
});
}

/**
Expand Down Expand Up @@ -123,6 +141,7 @@ export class LiveObjects {
this._currentSyncId = undefined;
this._currentSyncCursor = undefined;
this._syncInProgress = false;
this._eventEmitter.emit(LiveObjectsEvents.SyncCompleted);
}

private _parseSyncChannelSerial(syncChannelSerial: string | null | undefined): {
Expand Down
2 changes: 2 additions & 0 deletions test/realtime/live_objects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ define(['ably', 'shared_helper', 'async', 'chai', 'live_objects'], function (
await helper.monitorConnectionThenCloseAndFinish(async () => {
const channel = client.channels.get('channel');
const liveObjects = channel.liveObjects;
await channel.attach();
const root = await liveObjects.getRoot();

expect(root.constructor.name).to.equal('LiveMap');
Expand All @@ -71,6 +72,7 @@ define(['ably', 'shared_helper', 'async', 'chai', 'live_objects'], function (
await helper.monitorConnectionThenCloseAndFinish(async () => {
const channel = client.channels.get('channel');
const liveObjects = channel.liveObjects;
await channel.attach();
const root = await liveObjects.getRoot();

helper.recordPrivateApi('call.LiveObject.getObjectId');
Expand Down

0 comments on commit 0cdc7c7

Please sign in to comment.