-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
observablejson.ts
101 lines (89 loc) · 2.12 KB
/
observablejson.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
JSONExt,
JSONObject,
PartialJSONObject,
ReadonlyPartialJSONValue
} from '@lumino/coreutils';
import { Message } from '@lumino/messaging';
import { IObservableMap, ObservableMap } from './observablemap';
/**
* An observable JSON value.
*/
export interface IObservableJSON
extends IObservableMap<ReadonlyPartialJSONValue | undefined> {
/**
* Serialize the model to JSON.
*/
toJSON(): PartialJSONObject;
}
/**
* The namespace for IObservableJSON related interfaces.
*/
export namespace IObservableJSON {
/**
* A type alias for observable JSON changed args.
*/
export type IChangedArgs = IObservableMap.IChangedArgs<
ReadonlyPartialJSONValue
>;
}
/**
* A concrete Observable map for JSON data.
*/
export class ObservableJSON extends ObservableMap<ReadonlyPartialJSONValue> {
/**
* Construct a new observable JSON object.
*/
constructor(options: ObservableJSON.IOptions = {}) {
super({
itemCmp: JSONExt.deepEqual,
values: options.values
});
}
/**
* Serialize the model to JSON.
*/
toJSON(): PartialJSONObject {
const out: PartialJSONObject = Object.create(null);
const keys = this.keys();
for (let key of keys) {
const value = this.get(key);
if (value !== undefined) {
out[key] = JSONExt.deepCopy(value) as PartialJSONObject;
}
}
return out;
}
}
/**
* The namespace for ObservableJSON static data.
*/
export namespace ObservableJSON {
/**
* The options use to initialize an observable JSON object.
*/
export interface IOptions {
/**
* The optional initial value for the object.
*/
values?: JSONObject;
}
/**
* An observable JSON change message.
*/
export class ChangeMessage extends Message {
/**
* Create a new metadata changed message.
*/
constructor(type: string, args: IObservableJSON.IChangedArgs) {
super(type);
this.args = args;
}
/**
* The arguments of the change.
*/
readonly args: IObservableJSON.IChangedArgs;
}
}