This repository has been archived by the owner on Jul 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Store.ts
201 lines (180 loc) · 5.79 KB
/
Store.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
import { Evented } from '@dojo/core/Evented';
import { Patch, PatchOperation } from './state/Patch';
import { Pointer } from './state/Pointer';
import Map from '@dojo/shim/Map';
/**
* The "path" to a value of type T on and object of type M. The path string is a JSON Pointer to the location of
* `value` within `state`.
*
*/
export interface Path<M, T> {
path: string;
state: M;
value: T;
}
/**
* An interface that enables typed traversal of an arbitrary type M. `path` and `at` can be used to generate
* `Path`s that allow access to properties within M via the `get` method. The returned `Path`s can also be passed to the
* utility methods `add`, `replace`, and `delete` in order to generate typed operations for modifying the state of a store.
*/
export interface State<M> {
get<S>(path: Path<M, S>): S;
path<T, P0 extends keyof T>(path: Path<M, T>, a: P0): Path<M, T[P0]>;
path<T, P0 extends keyof T, P1 extends keyof T[P0]>(path: Path<M, T>, a: P0, b: P1): Path<M, T[P0][P1]>;
path<
T,
P0 extends keyof T,
P1 extends keyof T[P0],
P2 extends keyof T[P0][P1]
>(path: Path<M, T>, a: P0, b: P1, c: P2): Path<M, T[P0][P1][P2]>;
path<
T,
P0 extends keyof T,
P1 extends keyof T[P0],
P2 extends keyof T[P0][P1],
P3 extends keyof T[P0][P1][P2]
>(path: Path<M, T>, a: P0, b: P1, c: P2, d: P3): Path<M, T[P0][P1][P2][P3]>;
path<
T,
P0 extends keyof T,
P1 extends keyof T[P0],
P2 extends keyof T[P0][P1],
P3 extends keyof T[P0][P1][P2],
P4 extends keyof T[P0][P1][P2][P3]
>(path: Path<M, T>, a: P0, b: P1, c: P2, d: P3, e: P4): Path<M, T[P0][P1][P2][P3][P4]>;
path<P0 extends keyof M>(a: P0): Path<M, M[P0]>;
path<P0 extends keyof M, P1 extends keyof M[P0]>(a: P0, b: P1): Path<M, M[P0][P1]>;
path<P0 extends keyof M, P1 extends keyof M[P0], P2 extends keyof M[P0][P1]>(a: P0, b: P1, c: P2): Path<M, M[P0][P1][P2]>;
path<
P0 extends keyof M,
P1 extends keyof M[P0],
P2 extends keyof M[P0][P1],
P3 extends keyof M[P0][P1][P2]
>(a: P0, b: P1, c: P2, d: P3): Path<M, M[P0][P1][P2][P3]>;
path<
P0 extends keyof M,
P1 extends keyof M[P0],
P2 extends keyof M[P0][P1],
P3 extends keyof M[P0][P1][P2],
P4 extends keyof M[P0][P1][P2][P3]
>(a: P0, b: P1, c: P2, d: P3, e: P4): Path<M, M[P0][P1][P2][P3][P4]>;
at<S extends Path<M, Array<any>>>(path: S, index: number): Path<M, S['value'][0]>;
}
interface OnChangeCallback {
callbackId: number;
callback: () => void;
}
interface OnChangeValue {
callbacks: OnChangeCallback[];
previousValue: any;
}
function isString(segment?: string): segment is string {
return typeof segment === 'string';
}
/**
* Application state store
*/
export class Store<T = any> extends Evented implements State<T> {
/**
* The private state object
*/
private _state = {} as T;
private _changePaths = new Map<string, OnChangeValue>();
private _callbackId = 0;
/**
* Returns the state at a specific pointer path location.
*/
public get = <U = any>(path: Path<T, U>): U => {
return path.value;
}
/**
* Applies store operations to state and returns the undo operations
*/
public apply = (operations: PatchOperation<T>[], invalidate: boolean = false): PatchOperation<T>[] => {
const patch = new Patch(operations);
const patchResult = patch.apply(this._state);
this._state = patchResult.object;
if (invalidate) {
this.invalidate();
}
return patchResult.undoOperations;
}
public at = <U = any>(path: Path<T, Array<U>>, index: number): Path<T, U> => {
const array = this.get(path);
const value = array && array[index];
return {
path: `${path.path}/${index}`,
state: path.state,
value
};
}
public onChange = <U = any>(paths: Path<T, U> | Path<T, U>[], callback: () => void) => {
const callbackId = this._callbackId;
if (!Array.isArray(paths)) {
paths = [ paths ];
}
paths.forEach((path) => this._addOnChange(path, callback, callbackId));
this._callbackId += 1;
return {
remove: () => {
(paths as Path<T, U>[]).forEach((path) => {
const onChange = this._changePaths.get(path.path);
if (onChange) {
onChange.callbacks = onChange.callbacks.filter((callback) => {
return callback.callbackId !== callbackId;
});
}
});
}
};
}
private _addOnChange = <U = any>(path: Path<T, U>, callback: () => void, callbackId: number): void => {
let changePaths = this._changePaths.get(path.path);
if (!changePaths) {
changePaths = { callbacks: [], previousValue: this.get(path) };
}
changePaths.callbacks.push({ callbackId, callback });
this._changePaths.set(path.path, changePaths);
}
private _runOnChanges() {
const callbackIdsCalled: number[] = [];
this._changePaths.forEach((value: OnChangeValue, path: string) => {
const { previousValue, callbacks } = value;
const newValue = new Pointer(path).get(this._state);
if (previousValue !== newValue) {
this._changePaths.set(path, { callbacks, previousValue: newValue });
callbacks.forEach((callbackItem) => {
const { callback, callbackId } = callbackItem;
if (callbackIdsCalled.indexOf(callbackId) === -1) {
callbackIdsCalled.push(callbackId);
callback();
}
});
}
});
}
/**
* Emits an invalidation event
*/
public invalidate(): any {
this._runOnChanges();
this.emit({ type: 'invalidate' });
}
public path: State<T>['path'] = (path: string | Path<T, any>, ...segments: (string | undefined)[]) => {
if (typeof path === 'string') {
segments = [ path, ...segments ];
}
else {
segments = [ ...new Pointer(path.path).segments, ...segments ];
}
const stringSegments = segments.filter<string>(isString);
const hasMultipleSegments = stringSegments.length > 1;
const pointer = new Pointer(hasMultipleSegments ? stringSegments : (stringSegments[0] || ''));
return {
path: pointer.path,
state: this._state,
value: pointer.get(this._state)
};
}
}
export default Store;