-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
interfaces.ts
289 lines (259 loc) · 7.95 KB
/
interfaces.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { CommandRegistry } from '@lumino/commands';
import { ReadonlyJSONObject, ReadonlyJSONValue } from '@lumino/coreutils';
import { IDisposable, IObservableDisposable } from '@lumino/disposable';
import { ISignal } from '@lumino/signaling';
/**
* A generic interface for change emitter payloads.
*/
export interface IChangedArgs<T, U extends string = string> {
/**
* The name of the changed attribute.
*/
name: U;
/**
* The old value of the changed attribute.
*/
oldValue: T;
/**
* The new value of the changed attribute.
*/
newValue: T;
}
/**
* The description of a general purpose data connector.
*
* @typeparam T - The basic entity response type a service's connector.
*
* @typeparam U - The basic entity request type, which is conventionally the
* same as the response type but may be different if a service's implementation
* requires input data to be different from output responses. Defaults to `T`.
*
* @typeparam V - The basic token applied to a request, conventionally a string
* ID or filter, but may be set to a different type when an implementation
* requires it. Defaults to `string`.
*
* @typeparam W - The type of the optional `query` parameter of the `list`
* method. Defaults to `string`;
*/
export interface IDataConnector<T, U = T, V = string, W = string> {
/**
* Retrieve an item from the data connector.
*
* @param id - The identifier used to retrieve an item.
*
* @returns A promise that bears a data payload if available.
*
* #### Notes
* The promise returned by this method may be rejected if an error occurs in
* retrieving the data. Nonexistence of an `id` resolves with `undefined`.
*/
fetch(id: V): Promise<T | undefined>;
/**
* Retrieve the list of items available from the data connector.
*
* @param query - The optional query filter to apply to the connector request.
*
* @returns A promise that bears a list of `values` and an associated list of
* fetch `ids`.
*
* #### Notes
* The promise returned by this method may be rejected if an error occurs in
* retrieving the data. The two lists will always be the same size. If there
* is no data, this method will succeed with empty `ids` and `values`.
*/
list(query?: W): Promise<{ ids: V[]; values: T[] }>;
/**
* Remove a value using the data connector.
*
* @param id - The identifier for the data being removed.
*
* @returns A promise that is rejected if remove fails and succeeds otherwise.
*
* #### Notes
* This promise may resolve with a back-end response or `undefined`.
* Existence of resolved content in the promise is not prescribed and must be
* tested for. For example, some back-ends may return a copy of the item of
* type `T` being removed while others may return no content.
*/
remove(id: V): Promise<any>;
/**
* Save a value using the data connector.
*
* @param id - The identifier for the data being saved.
*
* @param value - The data being saved.
*
* @returns A promise that is rejected if saving fails and succeeds otherwise.
*
* #### Notes
* This promise may resolve with a back-end response or `undefined`.
* Existence of resolved content in the promise is not prescribed and must be
* tested for. For example, some back-ends may return a copy of the item of
* type `T` being saved while others may return no content.
*/
save(id: V, value: U): Promise<any>;
}
/**
* A pool of objects whose disposable lifecycle is tracked.
*
* @typeparam T - The type of object held in the pool.
*/
export interface IObjectPool<T extends IObservableDisposable>
extends IDisposable {
/**
* A signal emitted when an object is added.
*
* ####
* This signal does not emit if an object is added using `inject()`.
*/
readonly added: ISignal<this, T>;
/**
* The current object.
*/
readonly current: T | null;
/**
* A signal emitted when the current object changes.
*
* #### Notes
* If the last object being tracked is disposed, `null` will be emitted.
*/
readonly currentChanged: ISignal<this, T | null>;
/**
* The number of objects held by the pool.
*/
readonly size: number;
/**
* A signal emitted when an object is updated.
*/
readonly updated: ISignal<this, T>;
/**
* Find the first object in the pool that satisfies a filter function.
*
* @param - fn The filter function to call on each object.
*
* #### Notes
* If nothing is found, the value returned is `undefined`.
*/
find(fn: (obj: T) => boolean): T | undefined;
/**
* Iterate through each object in the pool.
*
* @param fn - The function to call on each object.
*/
forEach(fn: (obj: T) => void): void;
/**
* Filter the objects in the pool based on a predicate.
*
* @param fn - The function by which to filter.
*/
filter(fn: (obj: T) => boolean): T[];
/**
* Check if this pool has the specified object.
*
* @param obj - The object whose existence is being checked.
*/
has(obj: T): boolean;
}
/**
* An interface for a state restorer.
*
* @typeparam T - The restorable collection's type.
*
* @typeparam U - The type of object held by the restorable collection.
*
* @typeparam V - The `restored` promise resolution type. Defaults to `any`.
*/
export interface IRestorer<
T extends IRestorable<U> = IRestorable<IObservableDisposable>,
U extends IObservableDisposable = IObservableDisposable,
V = any
> {
/**
* Restore the objects in a given restorable collection.
*
* @param restorable - The restorable collection being restored.
*
* @param options - The configuration options that describe restoration.
*
* @returns A promise that settles when restored with `any` results.
*
*/
restore(restorable: T, options: IRestorable.IOptions<U>): Promise<V>;
/**
* A promise that settles when the collection has been restored.
*/
readonly restored: Promise<V>;
}
/**
* A namespace for `IRestorer` interface definitions.
*/
export namespace IRestorer {
/**
* The state restoration configuration options.
*
* @typeparam T - The type of object held by the restorable collection.
*/
export interface IOptions<T extends IObservableDisposable> {
/**
* The command to execute when restoring instances.
*/
command: string;
/**
* A function that returns the args needed to restore an instance.
*/
args?: (obj: T) => ReadonlyJSONObject;
/**
* A function that returns a unique persistent name for this instance.
*/
name: (obj: T) => string;
/**
* The point after which it is safe to restore state.
*/
when?: Promise<any> | Array<Promise<any>>;
}
}
/**
* An interface for objects that can be restored.
*
* @typeparam T - The type of object held by the restorable collection.
*
* @typeparam U - The `restored` promise resolution type. Defaults to `any`.
*/
export interface IRestorable<T extends IObservableDisposable, U = any> {
/**
* Restore the objects in this restorable collection.
*
* @param options - The configuration options that describe restoration.
*
* @returns A promise that settles when restored with `any` results.
*
*/
restore(options: IRestorable.IOptions<T>): Promise<U>;
/**
* A promise that settles when the collection has been restored.
*/
readonly restored: Promise<U>;
}
/**
* A namespace for `IRestorable` interface definitions.
*/
export namespace IRestorable {
/**
* The state restoration configuration options.
*
* @typeparam T - The type of object held by the restorable collection.
*/
export interface IOptions<T extends IObservableDisposable>
extends IRestorer.IOptions<T> {
/**
* The data connector to fetch restore data.
*/
connector: IDataConnector<ReadonlyJSONValue>;
/**
* The command registry which holds the restore command.
*/
registry: CommandRegistry;
}
}