Skip to content

Commit daac21c

Browse files
authored
Merge pull request #2109 from ably/PUB-1197/liveobjects-rest-api
[AIT-27, PUB-1197] LiveObjects REST client
2 parents f8260f2 + 4235266 commit daac21c

File tree

20 files changed

+2304
-359
lines changed

20 files changed

+2304
-359
lines changed

liveobjects.d.ts

Lines changed: 528 additions & 8 deletions
Large diffs are not rendered by default.

scripts/moduleReport.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ async function checkLiveObjectsPluginFiles() {
341341
'src/plugins/liveobjects/pathobject.ts',
342342
'src/plugins/liveobjects/pathobjectsubscriptionregister.ts',
343343
'src/plugins/liveobjects/realtimeobject.ts',
344+
'src/plugins/liveobjects/restobject.ts',
344345
'src/plugins/liveobjects/rootbatchcontext.ts',
345346
'src/plugins/liveobjects/syncobjectspool.ts',
346347
]);

src/common/lib/client/baseclient.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { FilteredSubscriptions } from './filteredsubscriptions';
2020
import type { LocalDevice } from 'plugins/push/pushactivation';
2121
import EventEmitter from '../util/eventemitter';
2222
import { MessageEncoding } from '../types/basemessage';
23+
import type * as LiveObjectsPlugin from 'plugins/liveobjects';
2324

2425
type BatchResult<T> = API.BatchResult<T>;
2526
type BatchPublishSpec = API.BatchPublishSpec;
@@ -50,6 +51,7 @@ class BaseClient {
5051
readonly _additionalHTTPRequestImplementations: HTTPRequestImplementations | null;
5152
private readonly __FilteredSubscriptions: typeof FilteredSubscriptions | null;
5253
readonly _Annotations: AnnotationsPlugin | null;
54+
readonly _liveObjectsPlugin: typeof LiveObjectsPlugin | null;
5355
readonly logger: Logger;
5456
_device?: LocalDevice;
5557

@@ -103,6 +105,7 @@ class BaseClient {
103105
this._Crypto = options.plugins?.Crypto ?? null;
104106
this.__FilteredSubscriptions = options.plugins?.MessageInteractions ?? null;
105107
this._Annotations = options.plugins?.Annotations ?? null;
108+
this._liveObjectsPlugin = options.plugins?.LiveObjects ?? null;
106109
}
107110

108111
get rest(): Rest {

src/common/lib/client/baserealtime.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import { ModularPlugins, RealtimePresencePlugin } from './modularplugins';
1313
import { TransportNames } from 'common/constants/TransportName';
1414
import { TransportImplementations } from 'common/platform';
1515
import Defaults from '../util/defaults';
16-
import type * as LiveObjectsPlugin from 'plugins/liveobjects';
1716

1817
/**
1918
`BaseRealtime` is an export of the tree-shakable version of the SDK, and acts as the base class for the `DefaultRealtime` class exported by the non tree-shakable version.
2019
*/
2120
class BaseRealtime extends BaseClient {
2221
readonly _RealtimePresence: RealtimePresencePlugin | null;
23-
readonly _liveObjectsPlugin: typeof LiveObjectsPlugin | null;
2422
// Extra transport implementations available to this client, in addition to those in Platform.Transports.bundledImplementations
2523
readonly _additionalTransportImplementations: TransportImplementations;
2624
_channels: any;
@@ -60,7 +58,6 @@ class BaseRealtime extends BaseClient {
6058

6159
this._additionalTransportImplementations = BaseRealtime.transportImplementationsFromPlugins(this.options.plugins);
6260
this._RealtimePresence = this.options.plugins?.RealtimePresence ?? null;
63-
this._liveObjectsPlugin = this.options.plugins?.LiveObjects ?? null;
6461
this.connection = new Connection(this, this.options);
6562
this._channels = new Channels(this);
6663
if (this.options.autoConnect !== false) this.connect();

src/common/lib/client/realtimechannel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ class RealtimeChannel extends EventEmitter {
146146
this._push = new client.options.plugins.Push.PushChannel(this);
147147
}
148148

149-
if (client.options.plugins?.LiveObjects) {
150-
this._object = new client.options.plugins.LiveObjects.RealtimeObject(this);
149+
if (client._liveObjectsPlugin) {
150+
this._object = new client._liveObjectsPlugin.RealtimeObject(this);
151151
}
152152
}
153153

src/common/lib/client/restchannel.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { RestHistoryParams } from './restchannelmixin';
1818
import { RequestBody } from 'common/types/http';
1919
import type { PushChannel } from 'plugins/push';
2020
import type RestAnnotations from './restannotations';
21+
import type { RestObject } from 'plugins/liveobjects';
2122

2223
const MSG_ID_ENTROPY_BYTES = 9;
2324

@@ -42,6 +43,7 @@ class RestChannel {
4243
}
4344
return this._annotations;
4445
}
46+
private _object?: RestObject;
4547

4648
constructor(client: BaseRest, name: string, channelOptions?: ChannelOptions) {
4749
Logger.logAction(client.logger, Logger.LOG_MINOR, 'RestChannel()', 'started; name = ' + name);
@@ -55,6 +57,9 @@ class RestChannel {
5557
if (client._Annotations) {
5658
this._annotations = new client._Annotations.RestAnnotations(this);
5759
}
60+
if (client._liveObjectsPlugin) {
61+
this._object = new client._liveObjectsPlugin.RestObject(this);
62+
}
5863
}
5964

6065
get push() {
@@ -64,6 +69,13 @@ class RestChannel {
6469
return this._push;
6570
}
6671

72+
get object(): RestObject {
73+
if (!this._object) {
74+
Utils.throwMissingPluginError('LiveObjects');
75+
}
76+
return this._object;
77+
}
78+
6779
get logger(): Logger {
6880
return this.client.logger;
6981
}

src/common/types/utils.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@ export type PaginatedResultCallback<T> = StandardCallback<PaginatedResult<T>>;
55
* Use this to override specific property typings on an existing object type
66
*/
77
export type Modify<T, R> = Omit<T, keyof R> & R;
8+
/**
9+
* Collects all keys from every member of a union into a single partial object type.
10+
* Allows uniform access to any field across a discriminated union.
11+
*/
12+
export type FlattenUnion<T> = {
13+
[K in T extends unknown ? keyof T : never]?: T extends unknown ? (K extends keyof T ? T[K] : never) : never;
14+
};

src/plugins/liveobjects/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { LiveCounterValueType } from './livecountervaluetype';
22
import { LiveMapValueType } from './livemapvaluetype';
3+
import { ObjectId } from './objectid';
34
import { ObjectMessage, WireObjectMessage } from './objectmessage';
45
import { RealtimeObject } from './realtimeobject';
6+
import { RestObject } from './restobject';
57

68
export {
79
LiveCounterValueType as LiveCounter,
810
LiveMapValueType as LiveMap,
11+
ObjectId,
912
ObjectMessage,
1013
RealtimeObject,
14+
RestObject,
1115
WireObjectMessage,
1216
};
1317

@@ -17,7 +21,9 @@ export {
1721
export const LiveObjects = {
1822
LiveCounter: LiveCounterValueType,
1923
LiveMap: LiveMapValueType,
24+
ObjectId,
2025
ObjectMessage,
2126
RealtimeObject,
27+
RestObject,
2228
WireObjectMessage,
2329
};

src/plugins/liveobjects/livecountervaluetype.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export class LiveCounterValueType implements LiveCounter {
6060
}
6161

6262
const counterCreate = LiveCounterValueType._getCounterCreate(count); // RTO12f12
63-
const { counterCreate: encodedCounterCreate } = encodePartialObjectOperationForWire({ counterCreate }, client);
63+
const { counterCreate: encodedCounterCreate } = encodePartialObjectOperationForWire(
64+
{ counterCreate },
65+
client,
66+
client.Utils.Format.json,
67+
);
6468
const initialValueJSONString = JSON.stringify(encodedCounterCreate); // RTO12f13
6569
const nonce = client.Utils.cheapRandStr(); // RTO12f4
6670
const msTimestamp = await client.getTimestamp(true); // RTO12f5

src/plugins/liveobjects/livemapvaluetype.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ export class LiveMapValueType<T extends Record<string, Value> = Record<string, V
7878
Object.entries(entries ?? {}).forEach(([key, value]) => LiveMap.validateKeyValue(realtimeObject, key, value));
7979

8080
const { mapCreate, nestedObjectsCreateMsgs } = await LiveMapValueType._getMapCreate(realtimeObject, entries); // RTO11f14
81-
const { mapCreate: encodedMapCreate } = encodePartialObjectOperationForWire({ mapCreate }, client); // RTO11f15a
81+
const { mapCreate: encodedMapCreate } = encodePartialObjectOperationForWire(
82+
{ mapCreate },
83+
client,
84+
client.Utils.Format.json,
85+
); // RTO11f15a
8286
const initialValueJSONString = JSON.stringify(encodedMapCreate); // RTO11f15b
8387
const nonce = client.Utils.cheapRandStr(); // RTO11f6
8488
const msTimestamp = await client.getTimestamp(true); // RTO11f7

0 commit comments

Comments
 (0)