/
space-proxy.ts
487 lines (417 loc) · 13.9 KB
/
space-proxy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
//
// Copyright 2021 DXOS.org
//
import isEqualWith from 'lodash.isequalwith';
import { Event, MulticastObservable, scheduleMicroTask, synchronized, Trigger } from '@dxos/async';
import { Properties, type ClientServicesProvider, type Space, type SpaceInternal } from '@dxos/client-protocol';
import { Stream } from '@dxos/codec-protobuf';
import { cancelWithContext, Context } from '@dxos/context';
import { checkCredentialType } from '@dxos/credentials';
import { loadashEqualityFn, todo, warnAfterTimeout } from '@dxos/debug';
import {
EchoDatabaseImpl,
type AutomergeContext,
type EchoDatabase,
type Hypergraph,
type TypedObject,
} from '@dxos/echo-schema';
import { invariant } from '@dxos/invariant';
import { type PublicKey } from '@dxos/keys';
import { log } from '@dxos/log';
import { decodeError } from '@dxos/protocols';
import {
Invitation,
SpaceState,
type CreateEpochRequest,
type Space as SpaceData,
type SpaceMember,
} from '@dxos/protocols/proto/dxos/client/services';
import { type SpaceSnapshot } from '@dxos/protocols/proto/dxos/echo/snapshot';
import { type GossipMessage } from '@dxos/protocols/proto/dxos/mesh/teleport/gossip';
import { trace } from '@dxos/tracing';
import { InvitationsProxy } from '../invitations';
export type SpaceProxyOptions = {
useReactiveObjectApi?: boolean;
};
// TODO(burdon): This should not be used as part of the API (don't export).
@trace.resource()
export class SpaceProxy implements Space {
private readonly _ctx = new Context();
/**
* @internal
* To update the space query when a space changes.
*/
// TODO(wittjosiah): Remove this? Should be consistent w/ ECHO query.
public readonly _stateUpdate = new Event<SpaceState>();
private readonly _pipelineUpdate = new Event<SpaceData.PipelineState>();
// TODO(dmaretskyi): Reconcile initialization states.
/**
* @internal
* To unlock internal operations that should happen after the database is initialized but before initialize() completes.
*/
public readonly _databaseInitialized = new Trigger();
/**
* @internal
* Space proxy is fully initialized, database open, state is READY.
*/
public readonly _initializationComplete = new Trigger();
// TODO(burdon): Change to state property.
@trace.info()
private _initializing = false;
/**
* @internal
*/
@trace.info()
_initialized = false;
private readonly _db!: EchoDatabaseImpl;
private readonly _internal!: SpaceInternal;
private readonly _invitationsProxy: InvitationsProxy;
private readonly _state = MulticastObservable.from(this._stateUpdate, SpaceState.CLOSED);
private readonly _pipeline = MulticastObservable.from(this._pipelineUpdate, {});
private readonly _membersUpdate = new Event<SpaceMember[]>();
private readonly _members = MulticastObservable.from(this._membersUpdate, []);
private _databaseOpen = false;
private _error: Error | undefined = undefined;
private _properties?: TypedObject = undefined;
constructor(
private _clientServices: ClientServicesProvider,
private _data: SpaceData,
graph: Hypergraph,
automergeContext: AutomergeContext,
options: SpaceProxyOptions = {},
) {
log('construct', { key: _data.spaceKey, state: SpaceState[_data.state] });
invariant(this._clientServices.services.InvitationsService, 'InvitationsService not available');
this._invitationsProxy = new InvitationsProxy(
this._clientServices.services.InvitationsService,
this._clientServices.services.IdentityService,
() => ({
kind: Invitation.Kind.SPACE,
spaceKey: this.key,
}),
);
invariant(this._clientServices.services.DataService, 'DataService not available');
this._db = new EchoDatabaseImpl({
spaceKey: this.key,
graph,
automergeContext,
useReactiveObjectApi: options.useReactiveObjectApi,
});
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this._internal = {
get db(): never {
throw new Error('Use space.db instead');
},
get data() {
return self._data;
},
createEpoch: this._createEpoch.bind(this),
open: this._activate.bind(this),
close: this._deactivate.bind(this),
removeMember: this._removeMember.bind(this),
};
this._error = this._data.error ? decodeError(this._data.error) : undefined;
graph._register(this.key, this._db, this);
// Update observables.
this._stateUpdate.emit(this._currentState);
this._pipelineUpdate.emit(_data.pipeline ?? {});
this._membersUpdate.emit(_data.members ?? []);
}
@trace.info()
get key() {
return this._data.spaceKey;
}
get db(): EchoDatabase {
return this._db;
}
@trace.info()
get isOpen() {
return this._data.state === SpaceState.READY && this._initialized;
}
@trace.info({ depth: 2 })
get properties(): TypedObject {
if (!this._initialized) {
throw new Error('Space is not initialized');
}
invariant(this._properties, 'Properties not available');
return this._properties;
}
get state() {
return this._state;
}
/**
* @inheritdoc
*/
get pipeline() {
return this._pipeline;
}
/**
* @inheritdoc
*/
get invitations() {
return this._invitationsProxy.created;
}
/**
* @inheritdoc
*/
get members() {
return this._members;
}
/**
* @inheritdoc
*/
// TODO(burdon): Remove?
get internal(): SpaceInternal {
return this._internal;
}
get error(): Error | undefined {
return this._error;
}
/**
* Current state of the space.
* The database is ready to be used in `SpaceState.READY` state.
* Presence is available in `SpaceState.CONTROL_ONLY` state.
*/
@trace.info({ enum: SpaceState })
private get _currentState(): SpaceState {
if (this._data.state === SpaceState.READY && !this._initialized) {
return SpaceState.INITIALIZING;
} else {
return this._data.state;
}
}
/**
* Called by EchoProxy to update this space instance.
* Called once on initial creation.
* @internal Package-private.
*/
@synchronized
async _processSpaceUpdate(space: SpaceData) {
const emitEvent = shouldUpdate(this._data, space);
const emitPipelineEvent = shouldPipelineUpdate(this._data, space);
const emitMembersEvent = shouldMembersUpdate(this._data.members, space.members);
const isFirstTimeInitializing = space.state === SpaceState.READY && !(this._initialized || this._initializing);
const isReopening =
this._data.state !== SpaceState.READY && space.state === SpaceState.READY && !this._databaseOpen;
log('update', {
key: space.spaceKey,
prevState: SpaceState[this._data.state],
state: SpaceState[space.state],
emitEvent,
emitPipelineEvent,
emitMembersEvent,
isFirstTimeInitializing,
isReopening,
});
this._data = space;
if (isFirstTimeInitializing) {
await this._initialize();
} else if (isReopening) {
await this._initializeDb();
}
if (space.error) {
this._error = decodeError(space.error);
}
if (this._initialized) {
// Transition onto new automerge root.
const automergeRoot = space.pipeline?.currentEpoch?.subject.assertion.automergeRoot;
if (automergeRoot) {
// NOOP if the root is the same.
await this._db.automerge.update({ rootUrl: automergeRoot });
}
}
if (emitEvent) {
this._stateUpdate.emit(this._currentState);
}
if (emitPipelineEvent) {
this._pipelineUpdate.emit(space.pipeline ?? {});
}
if (emitMembersEvent) {
this._membersUpdate.emit(space.members!);
}
}
private async _initialize() {
if (this._initializing || this._initialized) {
return;
}
log('initializing...');
// TODO(burdon): Does this need to be set before method completes?
this._initializing = true;
await this._invitationsProxy.open();
await this._initializeDb();
this._initialized = true;
this._initializing = false;
this._initializationComplete.wake();
this._stateUpdate.emit(this._currentState);
this._data.members && this._membersUpdate.emit(this._data.members);
log('initialized');
}
private async _initializeDb() {
this._databaseOpen = true;
{
let automergeRoot;
if (this._data.pipeline?.currentEpoch) {
invariant(checkCredentialType(this._data.pipeline.currentEpoch, 'dxos.halo.credentials.Epoch'));
automergeRoot = this._data.pipeline.currentEpoch.subject.assertion.automergeRoot;
}
await this._db.automerge.open({
rootUrl: automergeRoot,
});
}
log('ready');
this._databaseInitialized.wake();
const propertiesAvailable = new Trigger();
// Set properties document when it's available.
// NOTE: Emits state update event when properties are first available.
// This is needed to ensure reactivity for newly created spaces.
// TODO(wittjosiah): Transfer subscriptions from cached properties to the new properties object.
{
const unsubscribe = this._db.query(Properties.filter()).subscribe((query) => {
if (query.objects.length === 1) {
this._properties = query.objects[0];
propertiesAvailable.wake();
this._stateUpdate.emit(this._currentState);
scheduleMicroTask(this._ctx, () => {
unsubscribe();
});
}
}, true);
}
await warnAfterTimeout(5_000, 'Finding properties for a space', () =>
cancelWithContext(this._ctx, propertiesAvailable.wait()),
);
}
/**
* Called by EchoProxy close.
* @internal Package-private.
*/
@synchronized
async _destroy() {
log('destroying...');
await this._ctx.dispose();
await this._invitationsProxy.close();
await this._db.automerge.close();
this._databaseOpen = false;
log('destroyed');
}
/**
* TODO
*/
async open() {
await this._setOpen(true);
}
/**
* TODO
*/
async close() {
await this._setOpen(false);
}
/**
* Waits until the space is in the ready state, with database initialized.
*/
async waitUntilReady() {
await cancelWithContext(this._ctx, this._initializationComplete.wait());
return this;
}
/**
* Post a message to the space.
*/
async postMessage(channel: string, message: any) {
invariant(this._clientServices.services.SpacesService, 'SpacesService not available');
await this._clientServices.services.SpacesService.postMessage({
spaceKey: this.key,
channel,
message: { ...message, '@type': message['@type'] || 'google.protobuf.Struct' },
});
}
/**
* Listen for messages posted to the space.
*/
listen(channel: string, callback: (message: GossipMessage) => void) {
invariant(this._clientServices.services.SpacesService, 'SpacesService not available');
const stream = this._clientServices.services.SpacesService.subscribeMessages({ spaceKey: this.key, channel });
stream.subscribe(callback);
return () => stream.close();
}
/**
* Creates an interactive invitation.
*/
share(options?: Partial<Invitation>) {
log('create invitation', options);
return this._invitationsProxy.share({ ...options, spaceKey: this.key });
}
/**
* Implementation method.
*/
createSnapshot(): Promise<SpaceSnapshot> {
return todo();
// return this._serviceProvider.services.SpaceService.createSnapshot({ space_key: this.key });
}
async _setOpen(open: boolean) {
return todo();
// invariant(this._clientServices.services.SpaceService, 'SpaceService not available');
// await this._clientServices.services.SpaceService.setSpaceState({
// spaceKey: this.key,
// open
// });
}
private async _createEpoch({ migration }: { migration?: CreateEpochRequest.Migration } = {}) {
await this._clientServices.services.SpacesService!.createEpoch({ spaceKey: this.key, migration });
}
private async _activate() {
await this._clientServices.services.SpacesService!.updateSpace({ spaceKey: this.key, state: SpaceState.ACTIVE });
}
private async _deactivate() {
await this._db.flush();
await this._clientServices.services.SpacesService!.updateSpace({ spaceKey: this.key, state: SpaceState.INACTIVE });
}
private async _removeMember(memberKey: PublicKey) {
if (!this._members.get().find((member) => member.identity.identityKey.equals(memberKey))) {
throw new Error(`Member ${memberKey} not found`);
}
const credentials = await Stream.consumeData(
this._clientServices.services.SpacesService!.queryCredentials({ spaceKey: this.key, noTail: true }),
);
const credential = credentials.find(
(credential) =>
checkCredentialType(credential, 'dxos.halo.credentials.SpaceMember') && credential.subject.id.equals(memberKey),
);
if (!credential) {
throw new Error(`Credential for ${memberKey} not found`);
}
if (!credential.id) {
throw new Error(`Credential for ${memberKey} does not have an id`);
}
const identityQuery = await Stream.first(this._clientServices.services.IdentityService!.queryIdentity());
const identityKey = identityQuery?.identity?.identityKey;
invariant(identityKey, 'Identity key not found');
await this._clientServices.services.SpacesService!.writeCredentials({
spaceKey: this.key,
credentials: [
{
issuer: identityKey,
issuanceDate: new Date(),
subject: {
id: credential.id,
assertion: {
'@type': 'dxos.halo.credentials.Revocation',
},
},
},
],
});
}
}
const shouldUpdate = (prev: SpaceData, next: SpaceData) => {
return prev.state !== next.state;
};
const shouldPipelineUpdate = (prev: SpaceData, next: SpaceData) => {
return !isEqualWith(prev.pipeline, next.pipeline, loadashEqualityFn);
};
const shouldMembersUpdate = (prev: SpaceMember[] | undefined, next: SpaceMember[] | undefined) => {
if (!next) {
return false;
}
return !isEqualWith(prev, next, loadashEqualityFn);
};