/
reactive-membrane.ts
178 lines (157 loc) · 6.65 KB
/
reactive-membrane.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
import {
ObjectDefineProperty,
unwrap,
isArray,
isObject,
isUndefined,
getPrototypeOf,
isFunction,
hasOwnProperty,
registerProxy,
} from './shared';
import { ReactiveProxyHandler } from './reactive-handler';
import { ReadOnlyHandler } from './read-only-handler';
import { init as initDevFormatter } from './reactive-dev-formatter';
if (process.env.NODE_ENV !== 'production') {
initDevFormatter();
}
interface ReactiveState {
readOnly: any;
reactive: any;
shadowTarget: ReactiveMembraneShadowTarget;
}
export type ReactiveMembraneShadowTarget = object | any[];
export type ReactiveMembraneAccessCallback = (obj: any, key: PropertyKey) => void;
export type ReactiveMembraneMutationCallback = (obj: any, key: PropertyKey) => void;
export type ReactiveMembraneDistortionCallback = (value: any) => any;
export type ReactiveMembraneObservableCallback = (value: any) => boolean;
export interface ObservableMembraneInit {
valueMutated?: ReactiveMembraneMutationCallback;
valueObserved?: ReactiveMembraneAccessCallback;
valueDistortion?: ReactiveMembraneDistortionCallback;
valueIsObservable?: ReactiveMembraneObservableCallback;
}
function createShadowTarget(value: any): ReactiveMembraneShadowTarget {
let shadowTarget: ReactiveMembraneShadowTarget | undefined = undefined;
if (isArray(value)) {
shadowTarget = [];
} else if (isObject(value)) {
shadowTarget = {};
}
return shadowTarget as ReactiveMembraneShadowTarget;
}
const ObjectDotPrototype = Object.prototype;
function defaultValueIsObservable(value: any): boolean {
// intentionally checking for null
if (value === null) {
return false;
}
// treat all non-object types, including undefined, as non-observable values
if (typeof value !== 'object') {
return false;
}
if (isArray(value)) {
return true;
}
const proto = getPrototypeOf(value);
return (proto === ObjectDotPrototype || proto === null || getPrototypeOf(proto) === null);
}
const defaultValueObserved: ReactiveMembraneAccessCallback = (obj: any, key: PropertyKey) => {
/* do nothing */
};
const defaultValueMutated: ReactiveMembraneMutationCallback = (obj: any, key: PropertyKey) => {
/* do nothing */
};
const defaultValueDistortion: ReactiveMembraneDistortionCallback = (value: any) => value;
export function wrapDescriptor(membrane: ReactiveMembrane, descriptor: PropertyDescriptor, getValue: (membrane: ReactiveMembrane, originalValue: any) => any): PropertyDescriptor {
const { set, get } = descriptor;
if (hasOwnProperty.call(descriptor, 'value')) {
descriptor.value = getValue(membrane, descriptor.value);
} else {
if (!isUndefined(get)) {
descriptor.get = function() {
// invoking the original getter with the original target
return getValue(membrane, get.call(unwrap(this)));
};
}
if (!isUndefined(set)) {
descriptor.set = function(value) {
// At this point we don't have a clear indication of whether
// or not a valid mutation will occur, we don't have the key,
// and we are not sure why and how they are invoking this setter.
// Nevertheless we preserve the original semantics by invoking the
// original setter with the original target and the unwrapped value
set.call(unwrap(this), membrane.unwrapProxy(value));
};
}
}
return descriptor;
}
export class ReactiveMembrane {
valueDistortion: ReactiveMembraneDistortionCallback = defaultValueDistortion;
valueMutated: ReactiveMembraneMutationCallback = defaultValueMutated;
valueObserved: ReactiveMembraneAccessCallback = defaultValueObserved;
valueIsObservable: ReactiveMembraneObservableCallback = defaultValueIsObservable;
private objectGraph: WeakMap<any, ReactiveState> = new WeakMap();
constructor(options?: ObservableMembraneInit) {
if (!isUndefined(options)) {
const { valueDistortion, valueMutated, valueObserved, valueIsObservable } = options;
this.valueDistortion = isFunction(valueDistortion) ? valueDistortion : defaultValueDistortion;
this.valueMutated = isFunction(valueMutated) ? valueMutated : defaultValueMutated;
this.valueObserved = isFunction(valueObserved) ? valueObserved : defaultValueObserved;
this.valueIsObservable = isFunction(valueIsObservable) ? valueIsObservable : defaultValueIsObservable;
}
}
getProxy(value: any) {
const unwrappedValue = unwrap(value);
const distorted = this.valueDistortion(unwrappedValue);
if (this.valueIsObservable(distorted)) {
const o = this.getReactiveState(unwrappedValue, distorted);
// when trying to extract the writable version of a readonly
// we return the readonly.
return o.readOnly === value ? value : o.reactive;
}
return distorted;
}
getReadOnlyProxy(value: any) {
value = unwrap(value);
const distorted = this.valueDistortion(value);
if (this.valueIsObservable(distorted)) {
return this.getReactiveState(value, distorted).readOnly;
}
return distorted;
}
unwrapProxy(p: any) {
return unwrap(p);
}
private getReactiveState(value: any, distortedValue: any): ReactiveState {
const {
objectGraph,
} = this;
let reactiveState = objectGraph.get(distortedValue);
if (reactiveState) {
return reactiveState;
}
const membrane = this;
reactiveState = {
get reactive() {
const reactiveHandler = new ReactiveProxyHandler(membrane, distortedValue);
// caching the reactive proxy after the first time it is accessed
const proxy = new Proxy(createShadowTarget(distortedValue), reactiveHandler);
registerProxy(proxy, value);
ObjectDefineProperty(this, 'reactive', { value: proxy });
return proxy;
},
get readOnly() {
const readOnlyHandler = new ReadOnlyHandler(membrane, distortedValue);
// caching the readOnly proxy after the first time it is accessed
const proxy = new Proxy(createShadowTarget(distortedValue), readOnlyHandler);
registerProxy(proxy, value);
ObjectDefineProperty(this, 'readOnly', { value: proxy });
return proxy;
}
} as ReactiveState;
objectGraph.set(distortedValue, reactiveState);
return reactiveState;
}
}