This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
modeldb.ts
463 lines (423 loc) · 12.4 KB
/
modeldb.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
DisposableSet
} from '@phosphor/disposable';
import {
JSONValue, PromiseDelegate, JSONExt
} from '@phosphor/coreutils';
import {
IModelDB, IObservableValue, IObservableVector, ObservableValue,
IObservableMap, IObservableString, IObservable, IObservableUndoableVector,
IObservableJSON
} from '@jupyterlab/coreutils';
import {
GoogleSynchronizable
} from './googlerealtime';
import {
GoogleString
} from './string';
import {
GoogleVector
} from './vector';
import {
GoogleUndoableVector
} from './undoablevector';
import {
GoogleMap
} from './map';
import {
GoogleJSON
} from './json';
import {
getResourceForPath, loadRealtimeDocument
} from '../drive/drive';
/**
* Google Drive-based Model database that implements `IModelDB.
*/
export
class GoogleModelDB implements IModelDB {
/**
* Constructor for the database.
*/
constructor(options: GoogleModelDB.ICreateOptions) {
this._basePath = options.basePath || '';
this._filePath = options.filePath;
if(options.baseDB) {
// Handle the case of a view on an already existing database.
this._baseDB = options.baseDB;
} else {
// More complicated if we have to load the database. The `IModelDB`
// needs to be able to create objects immediately, so we create a
// temporary in-memory document, and then transfer data as necessary
// when the Google-Drive-backed document becomes available.
this._doc = gapi.drive.realtime.newInMemoryDocument();
this._model = this._doc.getModel();
//Wrap the model root in a `GoogleMap`.
this._db = new GoogleMap(this._model.getRoot());
// Load the document from Google Drive.
getResourceForPath(options.filePath).then((resource: any) => {
loadRealtimeDocument(resource.id).then((doc: gapi.drive.realtime.Document) => {
// Update the references to the doc and model
this._doc = doc;
this._model = doc.getModel();
let oldDB = this._db;
this._db = new GoogleMap(this._model.getRoot());
if (this._model.getRoot().size !== 0) {
// If the model is not empty, it is coming prepopulated.
this._isPrepopulated = true;
for (let key of oldDB.keys()) {
let oldVal = this._localDB.get(key);
if (this._db.has(key)) {
let dbVal = this._db.get(key);
if (oldVal.googleObject) {
oldVal.googleObject = dbVal;
}
}
}
} else {
//Handle the case where we populate the model.
for(let key of oldDB.keys()) {
let val = this._localDB.get(key);
if(val.googleObject) {
let newVal: gapi.drive.realtime.CollaborativeObject;
if(val.googleObject.type === 'EditableString') {
// Create a string.
newVal = this._model.createString(val.text);
} else if (val.googleObject.type === 'List') {
// Create a list.
newVal = this._model.createList(val.googleObject.asArray());
} else if (val.googleObject.type === 'Map') {
// Create a map.
newVal = this._model.createMap();
for(let item of val.keys()) {
(newVal as gapi.drive.realtime.CollaborativeMap<JSONValue>)
.set(item, val.get(item));
}
}
val.googleObject = newVal;
this._db.set(key, newVal);
} else if (val instanceof ObservableValue) {
this.set(key, val);
}
}
}
//Resolve the connected Promise.
this._connected.resolve(void 0);
});
});
}
}
/**
* Get the underlying `gapi.drive.realtime.Model`.
*/
get model(): gapi.drive.realtime.Model {
if(this._baseDB) {
return this._baseDB.model;
} else {
return this._model;
}
}
/**
* Get the underlying `gapi.drive.realtime.Document`.
*/
get doc(): gapi.drive.realtime.Document {
return this._doc;
}
/**
* The base path for the `GoogleModelDB`. This is prepended
* to all the paths that are passed in to the member
* functions of the object.
*/
get basePath(): string {
return this._basePath;
}
/**
* Whether the model has been populated with
* any model values.
*/
get isPrepopulated(): boolean {
if (this._baseDB) {
return this._baseDB.isPrepopulated;
} else {
return this._isPrepopulated;
}
}
/**
* A promise resolved when the `GoogleModelDB` has
* connected to Google Drive.
*/
get connected(): Promise<void> {
if (this._baseDB ) {
return this._baseDB.connected;
} else {
return this._connected.promise;
}
}
/**
* Whether the database is disposed.
*/
get isDisposed(): boolean {
return this._doc === null;
}
/**
* Get a value for a path.
*
* @param path: the path for the object.
*
* @returns an `IObservable`.
*/
get(path: string): IObservable {
return this._localDB.get(path);
}
/**
* Get the object in the underlying `gapi.drive.realtime.CollaborativeMap`.
* Not intended to be called by user code.
*
* @param path: the path for the object.
*/
getGoogleObject(path: string): GoogleSynchronizable {
if(this._baseDB) {
return this._baseDB.getGoogleObject(this._basePath+'/'+path);
} else {
return this._db.get(path);
}
}
/**
* Whether the `GoogleModelDB` has an object at this path.
*
* @param path: the path for the object.
*
* @returns a boolean for whether an object is at `path`.
*/
has(path: string): boolean {
if(this._baseDB) {
return this._baseDB.has(this._basePath+'/'+path);
} else {
return this._db.has(path);
}
}
/**
* Set a value at a path. Not intended to
* be called by user code, instead use the
* `create*` factory methods.
*
* @param path: the path to set the value at.
*
* @param value: the value to set at the path.
*/
set(path: string, value: IObservable): void {
this._localDB.set(path, value);
if(this._baseDB) {
this._baseDB.set(this._basePath+'/'+path, value);
} else {
let toSet: any;
if(value && (value as any).googleObject) {
toSet = (value as any).googleObject;
} else if (value instanceof ObservableValue) {
toSet = (value as any).get();
value.changed.connect((obs, args) => {
if(!JSONExt.deepEqual(args.newValue, this._db.get(path) as JSONValue)) {
this._db.set(path, args.newValue);
}
});
this._db.changed.connect((db, args) => {
if(args.key === path &&
!JSONExt.deepEqual(args.newValue as JSONValue, value.get())) {
value.set(args.newValue as JSONValue);
}
});
} else {
toSet = value;
}
this._db.set(path, toSet);
}
}
/**
* Create a string and insert it in the database.
*
* @param path: the path for the string.
*
* @returns the string that was created.
*/
createString(path: string): IObservableString {
let str: gapi.drive.realtime.CollaborativeString;
if(this.has(path)) {
str = this.getGoogleObject(path) as gapi.drive.realtime.CollaborativeString;
} else {
str = this.model.createString();
}
let newStr = new GoogleString(str);
this._disposables.add(newStr);
this.set(path, newStr);
return newStr;
}
/**
* Create a vector and insert it in the database.
*
* @param path: the path for the vector.
*
* @returns the vector that was created.
*
* #### Notes
* The vector can only store objects that are simple
* JSON Objects and primitives.
*/
createVector(path: string): IObservableVector<JSONValue> {
let vec: gapi.drive.realtime.CollaborativeList<JSONValue>;
if(this.has(path)) {
vec = this.getGoogleObject(path) as gapi.drive.realtime.CollaborativeList<JSONValue>;
} else {
vec = this.model.createList<JSONValue>();
}
let newVec = new GoogleVector<JSONValue>(vec);
this._disposables.add(newVec);
this.set(path, newVec);
return newVec;
}
/**
* Create an undoable vector and insert it in the database.
*
* @param path: the path for the vector.
*
* @returns the vector that was created.
*
* #### Notes
* The vector can only store objects that are simple
* JSON Objects and primitives.
*/
createUndoableVector(path: string): IObservableUndoableVector<JSONValue> {
let vec: gapi.drive.realtime.CollaborativeList<JSONValue>;
if(this.has(path)) {
vec = this.getGoogleObject(path) as gapi.drive.realtime.CollaborativeList<JSONValue>;
} else {
vec = this.model.createList<JSONValue>();
}
let newVec = new GoogleUndoableVector(vec);
this._disposables.add(newVec);
this.set(path, newVec);
return newVec;
}
/**
* Create a map and insert it in the database.
*
* @param path: the path for the map.
*
* @returns the map that was created.
*
* #### Notes
* The map can only store objects that are simple
* JSON Objects and primitives.
*/
createMap(path: string): IObservableMap<JSONValue> {
let map: gapi.drive.realtime.CollaborativeMap<JSONValue>;
if(this.has(path)) {
map = this.getGoogleObject(path) as gapi.drive.realtime.CollaborativeMap<JSONValue>;
} else {
map = this.model.createMap<JSONValue>();
}
let newMap = new GoogleMap<JSONValue>(map);
this._disposables.add(newMap);
this.set(path, newMap);
return newMap;
}
/**
* Create an `IObservableJSON` and insert it in the database.
*
* @param path: the path for the object.
*
* @returns the object that wsa created.
*/
createJSON(path: string): IObservableJSON {
let json: gapi.drive.realtime.CollaborativeMap<JSONValue>;
if(this.has(path)) {
json = this.getGoogleObject(path) as gapi.drive.realtime.CollaborativeMap<JSONValue>;
} else {
json = this.model.createMap<JSONValue>();
}
let newJSON = new GoogleJSON(json);
this._disposables.add(newJSON);
this.set(path, newJSON);
return newJSON;
}
/**
* Create an opaque value and insert it in the database.
*
* @param path: the path for the value.
*
* @returns the value that was created.
*/
createValue(path: string): IObservableValue {
let val: JSONValue = '';
if(this.has(path)) {
val = this.getGoogleObject(path) as JSONValue;
}
let newVal = new ObservableValue(val);
this._disposables.add(newVal);
this.set(path, newVal);
return newVal;
}
/**
* Create a view onto a subtree of the model database.
*
* @param basePath: the path for the root of the subtree.
*
* @returns a `GoogleModelDB` with a view onto the original
* `GoogleModelDB`, with `basePath` prepended to all paths.
*/
view(basePath: string): GoogleModelDB {
return new GoogleModelDB({filePath: this._filePath, basePath, baseDB: this});
}
/**
* Dispose of the resources held by the database.
*/
dispose(): void {
if (this.isDisposed) {
return;
}
let db = this._db;
this._db = null;
this._doc = null;
this._model = null;
if (db) {
db.dispose();
}
this._disposables.dispose();
}
private _filePath: string;
private _db: GoogleMap<GoogleSynchronizable>;
private _localDB = new Map<string, any>();
private _disposables = new DisposableSet();
private _model: gapi.drive.realtime.Model = null;
private _doc: gapi.drive.realtime.Document = null;
private _basePath: string;
private _baseDB: GoogleModelDB = null;
private _connected = new PromiseDelegate<void>()
private _isPrepopulated = false;
}
/**
* A namespace for the `GoogleModelDB` class statics.
*/
export
namespace GoogleModelDB {
/**
* Options for creating a `ModelDB` object.
*/
export
interface ICreateOptions {
/**
* The path for the location on Google Drive
* to store the model.
*/
filePath: string;
/**
* The base path to prepend to all the path arguments.
*/
basePath?: string;
/**
* A `GoogleModelDB` to use as the store for this
* `GoogleModelDB`. If none is given, it uses its own store.
*/
baseDB?: GoogleModelDB;
}
}