Skip to content

Commit

Permalink
Add missing properties to LiveMap
Browse files Browse the repository at this point in the history
  • Loading branch information
VeskeR committed Oct 16, 2024
1 parent 3a34601 commit e770822
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
24 changes: 18 additions & 6 deletions src/plugins/liveobjects/livemap.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { LiveObject, LiveObjectData } from './liveobject';
import { StateValue } from './statemessage';
import { LiveObjects } from './liveobjects';
import { MapSemantics, StateValue } from './statemessage';

export interface ObjectIdStateData {
/**
* A reference to another state object, used to support composable state objects.
*/
/** A reference to another state object, used to support composable state objects. */
objectId: string;
}

export interface ValueStateData {
/**
* A concrete leaf value in the state object graph.
* The encoding the client should use to interpret the value.
* Analogous to the `encoding` field on the `Message` and `PresenceMessage` types.
*/
encoding?: string;
/** A concrete leaf value in the state object graph. */
value: StateValue;
}

export type StateData = ObjectIdStateData | ValueStateData;

export interface MapEntry {
// TODO: add tombstone, timeserial
tombstone: boolean;
timeserial: string;
data: StateData;
}

Expand All @@ -27,6 +30,15 @@ export interface LiveMapData extends LiveObjectData {
}

export class LiveMap extends LiveObject<LiveMapData> {
constructor(
liveObjects: LiveObjects,
private _semantics: MapSemantics,
initialData?: LiveMapData | null,
objectId?: string,
) {
super(liveObjects, initialData, objectId);
}

/**
* Returns the value associated with the specified key in the underlying Map object.
* If no element is associated with the specified key, undefined is returned.
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/liveobjects/liveobjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,19 @@ export class LiveObjects {
}

let newObject: LiveObject;
switch (entry.objectType) {
// assign to a variable so TS doesn't complain about 'never' type in the default case
const objectType = entry.objectType;
switch (objectType) {
case 'LiveCounter':
newObject = new LiveCounter(this, entry.objectData, objectId);
break;

case 'LiveMap':
newObject = new LiveMap(this, entry.objectData, objectId);
newObject = new LiveMap(this, entry.semantics, entry.objectData, objectId);
break;

default:
throw new this._client.ErrorInfo(`Unknown live object type: ${entry.objectType}`, 40000, 400);
throw new this._client.ErrorInfo(`Unknown live object type: ${objectType}`, 40000, 400);
}
newObject.setRegionalTimeserial(entry.regionalTimeserial);

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/liveobjects/liveobjectspool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type BaseClient from 'common/lib/client/baseclient';
import { LiveMap } from './livemap';
import { LiveObject } from './liveobject';
import { LiveObjects } from './liveobjects';
import { MapSemantics } from './statemessage';

export const ROOT_OBJECT_ID = 'root';

Expand Down Expand Up @@ -41,7 +42,7 @@ export class LiveObjectsPool {

private _getInitialPool(): Map<string, LiveObject> {
const pool = new Map<string, LiveObject>();
const root = new LiveMap(this._liveObjects, null, ROOT_OBJECT_ID);
const root = new LiveMap(this._liveObjects, MapSemantics.LWW, null, ROOT_OBJECT_ID);
pool.set(root.getObjectId(), root);
return pool;
}
Expand Down
19 changes: 16 additions & 3 deletions src/plugins/liveobjects/syncliveobjectsdatapool.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import { LiveObjectData } from './liveobject';
import { LiveObjects } from './liveobjects';
import { MapSemantics } from './statemessage';

export interface LiveObjectDataEntry {
objectData: LiveObjectData;
regionalTimeserial: string;
objectType: 'LiveMap' | 'LiveCounter';
}

export interface LiveCounterDataEntry extends LiveObjectDataEntry {
created: boolean;
objectType: 'LiveCounter';
}

export interface LiveMapDataEntry extends LiveObjectDataEntry {
objectType: 'LiveMap';
semantics: MapSemantics;
}

export type AnyDataEntry = LiveCounterDataEntry | LiveMapDataEntry;

/**
* @internal
*/
export class SyncLiveObjectsDataPool {
private _pool: Map<string, LiveObjectDataEntry>;
private _pool: Map<string, AnyDataEntry>;

constructor(private _liveObjects: LiveObjects) {
this._pool = new Map<string, LiveObjectDataEntry>();
this._pool = new Map<string, AnyDataEntry>();
}

entries() {
Expand All @@ -30,6 +43,6 @@ export class SyncLiveObjectsDataPool {
}

reset(): void {
this._pool = new Map<string, LiveObjectDataEntry>();
this._pool = new Map<string, AnyDataEntry>();
}
}

0 comments on commit e770822

Please sign in to comment.