-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathvariable.js
194 lines (170 loc) · 7.59 KB
/
variable.js
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
import {map} from "./array";
import constant from "./constant";
import {RuntimeError} from "./errors";
import identity from "./identity";
import noop from "./noop";
export var TYPE_NORMAL = 1; // a normal variable
export var TYPE_IMPLICIT = 2; // created on reference
export var TYPE_DUPLICATE = 3; // created on duplicate definition
export var no_observer = {};
export default function Variable(type, module, observer) {
if (!observer) observer = no_observer;
Object.defineProperties(this, {
_observer: {value: observer, writable: true},
_definition: {value: variable_undefined, writable: true},
_duplicate: {value: undefined, writable: true},
_duplicates: {value: undefined, writable: true},
_indegree: {value: NaN, writable: true}, // The number of computing inputs.
_inputs: {value: [], writable: true},
_invalidate: {value: noop, writable: true},
_module: {value: module},
_name: {value: null, writable: true},
_outputs: {value: new Set, writable: true},
_promise: {value: Promise.resolve(undefined), writable: true},
_reachable: {value: observer !== no_observer, writable: true}, // Is this variable transitively visible?
_rejector: {value: variable_rejector(this)},
_type: {value: type},
_value: {value: undefined, writable: true},
_version: {value: 0, writable: true}
});
}
Object.defineProperties(Variable.prototype, {
_pending: {value: variable_pending, writable: true, configurable: true},
_fulfilled: {value: variable_fulfilled, writable: true, configurable: true},
_rejected: {value: variable_rejected, writable: true, configurable: true},
define: {value: variable_define, writable: true, configurable: true},
delete: {value: variable_delete, writable: true, configurable: true},
import: {value: variable_import, writable: true, configurable: true}
});
function variable_attach(variable) {
variable._module._runtime._dirty.add(variable);
variable._outputs.add(this);
}
function variable_detach(variable) {
variable._module._runtime._dirty.add(variable);
variable._outputs.delete(this);
}
function variable_undefined() {
throw variable_undefined;
}
function variable_rejector(variable) {
return function(error) {
if (error === variable_undefined) throw new RuntimeError(variable._name + " is not defined", variable._name);
if (error instanceof Error && error.message) throw new RuntimeError(error.message, variable._name, error);
throw new RuntimeError(variable._name + " could not be resolved", variable._name);
};
}
function variable_duplicate(name) {
return function() {
throw new RuntimeError(name + " is defined more than once");
};
}
function variable_define(name, inputs, definition) {
switch (arguments.length) {
case 1: {
definition = name, name = inputs = null;
break;
}
case 2: {
definition = inputs;
if (typeof name === "string") inputs = null;
else inputs = name, name = null;
break;
}
}
return variable_defineImpl.call(this,
name == null ? null : name + "",
inputs == null ? [] : map.call(inputs, this._module._resolve, this._module),
typeof definition === "function" ? definition : constant(definition)
);
}
function variable_defineImpl(name, inputs, definition) {
var scope = this._module._scope, runtime = this._module._runtime;
this._inputs.forEach(variable_detach, this);
inputs.forEach(variable_attach, this);
this._inputs = inputs;
this._definition = definition;
this._value = undefined;
// Is this an active variable (that may require disposal)?
if (definition === noop) runtime._variables.delete(this);
else runtime._variables.add(this);
// Did the variable’s name change? Time to patch references!
if (name !== this._name || scope.get(name) !== this) {
var error, found;
if (this._name) { // Did this variable previously have a name?
if (this._outputs.size) { // And did other variables reference this variable?
scope.delete(this._name);
found = this._module._resolve(this._name);
found._outputs = this._outputs, this._outputs = new Set;
found._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(this)] = found; }, this);
found._outputs.forEach(runtime._updates.add, runtime._updates);
runtime._dirty.add(found).add(this);
scope.set(this._name, found);
} else if ((found = scope.get(this._name)) === this) { // Do no other variables reference this variable?
scope.delete(this._name); // It’s safe to delete!
} else if (found._type === TYPE_DUPLICATE) { // Do other variables assign this name?
found._duplicates.delete(this); // This variable no longer assigns this name.
this._duplicate = undefined;
if (found._duplicates.size === 1) { // Is there now only one variable assigning this name?
found = found._duplicates.keys().next().value; // Any references are now fixed!
error = scope.get(this._name);
found._outputs = error._outputs, error._outputs = new Set;
found._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(error)] = found; });
found._definition = found._duplicate, found._duplicate = undefined;
runtime._dirty.add(error).add(found);
runtime._updates.add(found);
scope.set(this._name, found);
}
} else {
throw new Error;
}
}
if (this._outputs.size) throw new Error;
if (name) { // Does this variable have a new name?
if (found = scope.get(name)) { // Do other variables reference or assign this name?
if (found._type === TYPE_DUPLICATE) { // Do multiple other variables already define this name?
this._definition = variable_duplicate(name), this._duplicate = definition;
found._duplicates.add(this);
} else if (found._type === TYPE_IMPLICIT) { // Are the variable references broken?
this._outputs = found._outputs, found._outputs = new Set; // Now they’re fixed!
this._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(found)] = this; }, this);
runtime._dirty.add(found).add(this);
scope.set(name, this);
} else { // Does another variable define this name?
found._duplicate = found._definition, this._duplicate = definition; // Now they’re duplicates.
error = new Variable(TYPE_DUPLICATE, this._module);
error._name = name;
error._definition = this._definition = found._definition = variable_duplicate(name);
error._outputs = found._outputs, found._outputs = new Set;
error._outputs.forEach(function(output) { output._inputs[output._inputs.indexOf(found)] = error; });
error._duplicates = new Set([this, found]);
runtime._dirty.add(found).add(error);
runtime._updates.add(found).add(error);
scope.set(name, error);
}
} else {
scope.set(name, this);
}
}
this._name = name;
}
runtime._updates.add(this);
runtime._compute();
return this;
}
function variable_import(remote, name, module) {
if (arguments.length < 3) module = name, name = remote;
return variable_defineImpl.call(this, name + "", [module._resolve(remote + "")], identity);
}
function variable_delete() {
return variable_defineImpl.call(this, null, [], noop);
}
function variable_pending() {
if (this._observer.pending) this._observer.pending();
}
function variable_fulfilled(value) {
if (this._observer.fulfilled) this._observer.fulfilled(value, this._name);
}
function variable_rejected(error) {
if (this._observer.rejected) this._observer.rejected(error, this._name);
}