@jupyterlab/observables 2.1.1-alpha.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/modeldb.js CHANGED
@@ -1,294 +1,294 @@
1
- "use strict";
2
- // Copyright (c) Jupyter Development Team.
3
- // Distributed under the terms of the Modified BSD License.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- const disposable_1 = require("@phosphor/disposable");
6
- const signaling_1 = require("@phosphor/signaling");
7
- const coreutils_1 = require("@phosphor/coreutils");
8
- const observablemap_1 = require("./observablemap");
9
- const observablejson_1 = require("./observablejson");
10
- const observablestring_1 = require("./observablestring");
11
- const undoablelist_1 = require("./undoablelist");
12
- /**
13
- * A concrete implementation of an `IObservableValue`.
14
- */
15
- class ObservableValue {
16
- /**
17
- * Constructor for the value.
18
- *
19
- * @param initialValue: the starting value for the `ObservableValue`.
20
- */
21
- constructor(initialValue = null) {
22
- this._value = null;
23
- this._changed = new signaling_1.Signal(this);
24
- this._isDisposed = false;
25
- this._value = initialValue;
26
- }
27
- /**
28
- * The observable type.
29
- */
30
- get type() {
31
- return 'Value';
32
- }
33
- /**
34
- * Whether the value has been disposed.
35
- */
36
- get isDisposed() {
37
- return this._isDisposed;
38
- }
39
- /**
40
- * The changed signal.
41
- */
42
- get changed() {
43
- return this._changed;
44
- }
45
- /**
46
- * Get the current value, or `undefined` if it has not been set.
47
- */
48
- get() {
49
- return this._value;
50
- }
51
- /**
52
- * Set the current value.
53
- */
54
- set(value) {
55
- let oldValue = this._value;
56
- if (coreutils_1.JSONExt.deepEqual(oldValue, value)) {
57
- return;
58
- }
59
- this._value = value;
60
- this._changed.emit({
61
- oldValue: oldValue,
62
- newValue: value
63
- });
64
- }
65
- /**
66
- * Dispose of the resources held by the value.
67
- */
68
- dispose() {
69
- if (this._isDisposed) {
70
- return;
71
- }
72
- this._isDisposed = true;
73
- signaling_1.Signal.clearData(this);
74
- this._value = null;
75
- }
76
- }
77
- exports.ObservableValue = ObservableValue;
78
- /**
79
- * The namespace for the `ObservableValue` class statics.
80
- */
81
- (function (ObservableValue) {
82
- /**
83
- * The changed args object emitted by the `IObservableValue`.
84
- */
85
- class IChangedArgs {
86
- }
87
- ObservableValue.IChangedArgs = IChangedArgs;
88
- })(ObservableValue = exports.ObservableValue || (exports.ObservableValue = {}));
89
- /**
90
- * A concrete implementation of an `IModelDB`.
91
- */
92
- class ModelDB {
93
- /**
94
- * Constructor for the `ModelDB`.
95
- */
96
- constructor(options = {}) {
97
- /**
98
- * Whether the model has been populated with
99
- * any model values.
100
- */
101
- this.isPrepopulated = false;
102
- /**
103
- * Whether the model is collaborative.
104
- */
105
- this.isCollaborative = false;
106
- /**
107
- * A promise resolved when the model is connected
108
- * to its backend. For the in-memory ModelDB it
109
- * is immediately resolved.
110
- */
111
- this.connected = Promise.resolve(void 0);
112
- this._toDispose = false;
113
- this._isDisposed = false;
114
- this._disposables = new disposable_1.DisposableSet();
115
- this._basePath = options.basePath || '';
116
- if (options.baseDB) {
117
- this._db = options.baseDB;
118
- }
119
- else {
120
- this._db = new observablemap_1.ObservableMap();
121
- this._toDispose = true;
122
- }
123
- }
124
- /**
125
- * The base path for the `ModelDB`. This is prepended
126
- * to all the paths that are passed in to the member
127
- * functions of the object.
128
- */
129
- get basePath() {
130
- return this._basePath;
131
- }
132
- /**
133
- * Whether the database is disposed.
134
- */
135
- get isDisposed() {
136
- return this._isDisposed;
137
- }
138
- /**
139
- * Get a value for a path.
140
- *
141
- * @param path: the path for the object.
142
- *
143
- * @returns an `IObservable`.
144
- */
145
- get(path) {
146
- return this._db.get(this._resolvePath(path));
147
- }
148
- /**
149
- * Whether the `IModelDB` has an object at this path.
150
- *
151
- * @param path: the path for the object.
152
- *
153
- * @returns a boolean for whether an object is at `path`.
154
- */
155
- has(path) {
156
- return this._db.has(this._resolvePath(path));
157
- }
158
- /**
159
- * Create a string and insert it in the database.
160
- *
161
- * @param path: the path for the string.
162
- *
163
- * @returns the string that was created.
164
- */
165
- createString(path) {
166
- let str = new observablestring_1.ObservableString();
167
- this._disposables.add(str);
168
- this.set(path, str);
169
- return str;
170
- }
171
- /**
172
- * Create an undoable list and insert it in the database.
173
- *
174
- * @param path: the path for the list.
175
- *
176
- * @returns the list that was created.
177
- *
178
- * #### Notes
179
- * The list can only store objects that are simple
180
- * JSON Objects and primitives.
181
- */
182
- createList(path) {
183
- let vec = new undoablelist_1.ObservableUndoableList(new undoablelist_1.ObservableUndoableList.IdentitySerializer());
184
- this._disposables.add(vec);
185
- this.set(path, vec);
186
- return vec;
187
- }
188
- /**
189
- * Create a map and insert it in the database.
190
- *
191
- * @param path: the path for the map.
192
- *
193
- * @returns the map that was created.
194
- *
195
- * #### Notes
196
- * The map can only store objects that are simple
197
- * JSON Objects and primitives.
198
- */
199
- createMap(path) {
200
- let map = new observablejson_1.ObservableJSON();
201
- this._disposables.add(map);
202
- this.set(path, map);
203
- return map;
204
- }
205
- /**
206
- * Create an opaque value and insert it in the database.
207
- *
208
- * @param path: the path for the value.
209
- *
210
- * @returns the value that was created.
211
- */
212
- createValue(path) {
213
- let val = new ObservableValue();
214
- this._disposables.add(val);
215
- this.set(path, val);
216
- return val;
217
- }
218
- /**
219
- * Get a value at a path, or `undefined if it has not been set
220
- * That value must already have been created using `createValue`.
221
- *
222
- * @param path: the path for the value.
223
- */
224
- getValue(path) {
225
- let val = this.get(path);
226
- if (!val || val.type !== 'Value') {
227
- throw Error('Can only call getValue for an ObservableValue');
228
- }
229
- return val.get();
230
- }
231
- /**
232
- * Set a value at a path. That value must already have
233
- * been created using `createValue`.
234
- *
235
- * @param path: the path for the value.
236
- *
237
- * @param value: the new value.
238
- */
239
- setValue(path, value) {
240
- let val = this.get(path);
241
- if (!val || val.type !== 'Value') {
242
- throw Error('Can only call setValue on an ObservableValue');
243
- }
244
- val.set(value);
245
- }
246
- /**
247
- * Create a view onto a subtree of the model database.
248
- *
249
- * @param basePath: the path for the root of the subtree.
250
- *
251
- * @returns an `IModelDB` with a view onto the original
252
- * `IModelDB`, with `basePath` prepended to all paths.
253
- */
254
- view(basePath) {
255
- let view = new ModelDB({ basePath, baseDB: this });
256
- this._disposables.add(view);
257
- return view;
258
- }
259
- /**
260
- * Set a value at a path. Not intended to
261
- * be called by user code, instead use the
262
- * `create*` factory methods.
263
- *
264
- * @param path: the path to set the value at.
265
- *
266
- * @param value: the value to set at the path.
267
- */
268
- set(path, value) {
269
- this._db.set(this._resolvePath(path), value);
270
- }
271
- /**
272
- * Dispose of the resources held by the database.
273
- */
274
- dispose() {
275
- if (this.isDisposed) {
276
- return;
277
- }
278
- this._isDisposed = true;
279
- if (this._toDispose) {
280
- this._db.dispose();
281
- }
282
- this._disposables.dispose();
283
- }
284
- /**
285
- * Compute the fully resolved path for a path argument.
286
- */
287
- _resolvePath(path) {
288
- if (this._basePath) {
289
- path = this._basePath + '.' + path;
290
- }
291
- return path;
292
- }
293
- }
294
- exports.ModelDB = ModelDB;
1
+ "use strict";
2
+ // Copyright (c) Jupyter Development Team.
3
+ // Distributed under the terms of the Modified BSD License.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ const disposable_1 = require("@phosphor/disposable");
6
+ const signaling_1 = require("@phosphor/signaling");
7
+ const coreutils_1 = require("@phosphor/coreutils");
8
+ const observablemap_1 = require("./observablemap");
9
+ const observablejson_1 = require("./observablejson");
10
+ const observablestring_1 = require("./observablestring");
11
+ const undoablelist_1 = require("./undoablelist");
12
+ /**
13
+ * A concrete implementation of an `IObservableValue`.
14
+ */
15
+ class ObservableValue {
16
+ /**
17
+ * Constructor for the value.
18
+ *
19
+ * @param initialValue: the starting value for the `ObservableValue`.
20
+ */
21
+ constructor(initialValue = null) {
22
+ this._value = null;
23
+ this._changed = new signaling_1.Signal(this);
24
+ this._isDisposed = false;
25
+ this._value = initialValue;
26
+ }
27
+ /**
28
+ * The observable type.
29
+ */
30
+ get type() {
31
+ return 'Value';
32
+ }
33
+ /**
34
+ * Whether the value has been disposed.
35
+ */
36
+ get isDisposed() {
37
+ return this._isDisposed;
38
+ }
39
+ /**
40
+ * The changed signal.
41
+ */
42
+ get changed() {
43
+ return this._changed;
44
+ }
45
+ /**
46
+ * Get the current value, or `undefined` if it has not been set.
47
+ */
48
+ get() {
49
+ return this._value;
50
+ }
51
+ /**
52
+ * Set the current value.
53
+ */
54
+ set(value) {
55
+ let oldValue = this._value;
56
+ if (coreutils_1.JSONExt.deepEqual(oldValue, value)) {
57
+ return;
58
+ }
59
+ this._value = value;
60
+ this._changed.emit({
61
+ oldValue: oldValue,
62
+ newValue: value
63
+ });
64
+ }
65
+ /**
66
+ * Dispose of the resources held by the value.
67
+ */
68
+ dispose() {
69
+ if (this._isDisposed) {
70
+ return;
71
+ }
72
+ this._isDisposed = true;
73
+ signaling_1.Signal.clearData(this);
74
+ this._value = null;
75
+ }
76
+ }
77
+ exports.ObservableValue = ObservableValue;
78
+ /**
79
+ * The namespace for the `ObservableValue` class statics.
80
+ */
81
+ (function (ObservableValue) {
82
+ /**
83
+ * The changed args object emitted by the `IObservableValue`.
84
+ */
85
+ class IChangedArgs {
86
+ }
87
+ ObservableValue.IChangedArgs = IChangedArgs;
88
+ })(ObservableValue = exports.ObservableValue || (exports.ObservableValue = {}));
89
+ /**
90
+ * A concrete implementation of an `IModelDB`.
91
+ */
92
+ class ModelDB {
93
+ /**
94
+ * Constructor for the `ModelDB`.
95
+ */
96
+ constructor(options = {}) {
97
+ /**
98
+ * Whether the model has been populated with
99
+ * any model values.
100
+ */
101
+ this.isPrepopulated = false;
102
+ /**
103
+ * Whether the model is collaborative.
104
+ */
105
+ this.isCollaborative = false;
106
+ /**
107
+ * A promise resolved when the model is connected
108
+ * to its backend. For the in-memory ModelDB it
109
+ * is immediately resolved.
110
+ */
111
+ this.connected = Promise.resolve(void 0);
112
+ this._toDispose = false;
113
+ this._isDisposed = false;
114
+ this._disposables = new disposable_1.DisposableSet();
115
+ this._basePath = options.basePath || '';
116
+ if (options.baseDB) {
117
+ this._db = options.baseDB;
118
+ }
119
+ else {
120
+ this._db = new observablemap_1.ObservableMap();
121
+ this._toDispose = true;
122
+ }
123
+ }
124
+ /**
125
+ * The base path for the `ModelDB`. This is prepended
126
+ * to all the paths that are passed in to the member
127
+ * functions of the object.
128
+ */
129
+ get basePath() {
130
+ return this._basePath;
131
+ }
132
+ /**
133
+ * Whether the database is disposed.
134
+ */
135
+ get isDisposed() {
136
+ return this._isDisposed;
137
+ }
138
+ /**
139
+ * Get a value for a path.
140
+ *
141
+ * @param path: the path for the object.
142
+ *
143
+ * @returns an `IObservable`.
144
+ */
145
+ get(path) {
146
+ return this._db.get(this._resolvePath(path));
147
+ }
148
+ /**
149
+ * Whether the `IModelDB` has an object at this path.
150
+ *
151
+ * @param path: the path for the object.
152
+ *
153
+ * @returns a boolean for whether an object is at `path`.
154
+ */
155
+ has(path) {
156
+ return this._db.has(this._resolvePath(path));
157
+ }
158
+ /**
159
+ * Create a string and insert it in the database.
160
+ *
161
+ * @param path: the path for the string.
162
+ *
163
+ * @returns the string that was created.
164
+ */
165
+ createString(path) {
166
+ let str = new observablestring_1.ObservableString();
167
+ this._disposables.add(str);
168
+ this.set(path, str);
169
+ return str;
170
+ }
171
+ /**
172
+ * Create an undoable list and insert it in the database.
173
+ *
174
+ * @param path: the path for the list.
175
+ *
176
+ * @returns the list that was created.
177
+ *
178
+ * #### Notes
179
+ * The list can only store objects that are simple
180
+ * JSON Objects and primitives.
181
+ */
182
+ createList(path) {
183
+ let vec = new undoablelist_1.ObservableUndoableList(new undoablelist_1.ObservableUndoableList.IdentitySerializer());
184
+ this._disposables.add(vec);
185
+ this.set(path, vec);
186
+ return vec;
187
+ }
188
+ /**
189
+ * Create a map and insert it in the database.
190
+ *
191
+ * @param path: the path for the map.
192
+ *
193
+ * @returns the map that was created.
194
+ *
195
+ * #### Notes
196
+ * The map can only store objects that are simple
197
+ * JSON Objects and primitives.
198
+ */
199
+ createMap(path) {
200
+ let map = new observablejson_1.ObservableJSON();
201
+ this._disposables.add(map);
202
+ this.set(path, map);
203
+ return map;
204
+ }
205
+ /**
206
+ * Create an opaque value and insert it in the database.
207
+ *
208
+ * @param path: the path for the value.
209
+ *
210
+ * @returns the value that was created.
211
+ */
212
+ createValue(path) {
213
+ let val = new ObservableValue();
214
+ this._disposables.add(val);
215
+ this.set(path, val);
216
+ return val;
217
+ }
218
+ /**
219
+ * Get a value at a path, or `undefined if it has not been set
220
+ * That value must already have been created using `createValue`.
221
+ *
222
+ * @param path: the path for the value.
223
+ */
224
+ getValue(path) {
225
+ let val = this.get(path);
226
+ if (!val || val.type !== 'Value') {
227
+ throw Error('Can only call getValue for an ObservableValue');
228
+ }
229
+ return val.get();
230
+ }
231
+ /**
232
+ * Set a value at a path. That value must already have
233
+ * been created using `createValue`.
234
+ *
235
+ * @param path: the path for the value.
236
+ *
237
+ * @param value: the new value.
238
+ */
239
+ setValue(path, value) {
240
+ let val = this.get(path);
241
+ if (!val || val.type !== 'Value') {
242
+ throw Error('Can only call setValue on an ObservableValue');
243
+ }
244
+ val.set(value);
245
+ }
246
+ /**
247
+ * Create a view onto a subtree of the model database.
248
+ *
249
+ * @param basePath: the path for the root of the subtree.
250
+ *
251
+ * @returns an `IModelDB` with a view onto the original
252
+ * `IModelDB`, with `basePath` prepended to all paths.
253
+ */
254
+ view(basePath) {
255
+ let view = new ModelDB({ basePath, baseDB: this });
256
+ this._disposables.add(view);
257
+ return view;
258
+ }
259
+ /**
260
+ * Set a value at a path. Not intended to
261
+ * be called by user code, instead use the
262
+ * `create*` factory methods.
263
+ *
264
+ * @param path: the path to set the value at.
265
+ *
266
+ * @param value: the value to set at the path.
267
+ */
268
+ set(path, value) {
269
+ this._db.set(this._resolvePath(path), value);
270
+ }
271
+ /**
272
+ * Dispose of the resources held by the database.
273
+ */
274
+ dispose() {
275
+ if (this.isDisposed) {
276
+ return;
277
+ }
278
+ this._isDisposed = true;
279
+ if (this._toDispose) {
280
+ this._db.dispose();
281
+ }
282
+ this._disposables.dispose();
283
+ }
284
+ /**
285
+ * Compute the fully resolved path for a path argument.
286
+ */
287
+ _resolvePath(path) {
288
+ if (this._basePath) {
289
+ path = this._basePath + '.' + path;
290
+ }
291
+ return path;
292
+ }
293
+ }
294
+ exports.ModelDB = ModelDB;