-
Notifications
You must be signed in to change notification settings - Fork 7
/
reactive.js
241 lines (231 loc) · 7.3 KB
/
reactive.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
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
(function (root, factory) { if (typeof define === 'function' && define.amd) {
define(['./util/lang', './operators', './Variable'], factory) } else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('./util/lang'), require('./operators'), require('./Variable')) // Node
}}(this, function (lang, operators, VariableExports) {
var Transform = VariableExports.Transform
var Variable = VariableExports.Variable
var changeValue = VariableExports._changeValue
var VArray = VariableExports.VArray
var isGenerator = lang.isGenerator
var ObjectTransform = lang.compose(Transform, function ObjectTransform(source, transform, sources) {
this.sources = sources
Transform.apply(this, arguments)
}, {
_getAsObject: function() {
return this.transform.apply(this, preserveObjects(this.sources))
}
})
function preserveObjects(sources) {
for (var i = 0, l = sources.length; i < l; i++) {
var source = sources[i]
if (source && source._getAsObject) {
sources[i] = source._getAsObject()
}
}
return sources
}
var typeMappings = new Map()
typeMappings.set('string', VariableExports.VString)
typeMappings.set('number', VariableExports.VNumber)
typeMappings.set('boolean', VariableExports.VBoolean)
typeMappings.set('undefined', Variable)
typeMappings.set(null, Variable)
typeMappings.set(Array, VArray)
typeMappings.set(Map, VariableExports.VMap)
typeMappings.set(Set, VariableExports.VSet)
function reactive(value) {
return fromValue(value)
}
VariableExports.reactive = reactive
function fromValue(value, isProperty, deferObject) {
// get the type for primitives or known constructors (or null)
var Type = typeMappings.get(typeof value) || typeMappings.get(value && value.constructor) || value instanceof Variable && !(value instanceof Transform) && value.constructor
if (Type) {
return new Type(isProperty ? undefined : value)
}
if (deferObject) {
return
}
// an object
var objectVar
if (isProperty) {
objectVar = new Variable()
} else {
objectVar = new Variable(value)
if (value && typeof value == 'object') {
// by default, if given an object (or variable or array), treat it as fixed
objectVar.fixed = true
}
}
if (value.then) {
// incoming promise or variable, just as a proxy
return objectVar
}
for (var key in value) {
if (value.hasOwnProperty(key)) {
var propertyValue = value[key]
var propertyVariable = fromValue(propertyValue, true, true)
if (propertyVariable) {
propertyVariable.key = key
propertyVariable.parent = objectVar
if (objectVar[key] === undefined) {
objectVar[key] = propertyVariable
} else {
(objectVar._properties || (objectVar._properties = {}))[key] = propertyVariable
}
} else {
// deferred, use getter/setter
defineValueProperty(objectVar, key, value)
}
}
}
return objectVar
}
function defineValueProperty(target, key, object) {
var Type
Object.defineProperty(target, key, {
get: function() {
return reactive.get(this, key, function() { return fromValue(object[key], true) })
},
set: function(value) {
reactive.set(this, key, value)
},
enumerable: true
})
}
lang.copy(reactive, {
from: function(value, options) {
if (value && value.property) {
return value
}
if (typeof value === 'function' && isGenerator(value)) {
return VariableExports.react(value, options)
}
return Variable.from(value)
},
getp: function(object, property) {
if (object) {
// TODO: Use a static set of public methods/properties that can be accessed
if (object.property) {
// it is a variable already, but check to see if we are using a method/property directly on the variable
var directPropertyValue = object[property]
return directPropertyValue !== undefined ? directPropertyValue : object.property(property)
}
return object[property]
}
// not even truthy, return undefined
},
get: function(target, key, Type) { // for TS/Babel decorators
var property = (target._properties || (target._properties = {}))[key]
if (!property) {
target._properties[key] = property = new (getType(Type))()
if (target.getValue) {
property.key = key
property.parent = target
if (property.listeners) {
// if it already has listeners, need to reinit it with the parent
property.init()
}
}
}
return property
},
set: function(target, key, value) {
var property = target[key]
property.parent ? changeValue(property, 3, value) : property.put(value)
},
prop: function(Type) {
return function(prototype, key) {
defineProperty(prototype, key, Type)
}
},
cond: function(test, consequent, alternate) {
return operators.if(test, operators.choose(consequent, alternate))
},
fcall: function(target, args) {
if (target.property && typeof target === 'function') {
return target.apply(null, preserveObjects(args))
}
return new Transform(args[0], target, args)
},
mcall: function(target, key, args) {
var method = target[key]
if (typeof method === 'function' && method.property || key === 'bind') {
// for now we check to see if looks like it could handle a variable, or is a bind call
return method.apply(target, preserveObjects(args))
}
return new Transform(args[0], target[key].bind(target), args)
},
ncall: function(target, args) {
if (target.property && typeof target === 'function') {
return new (target.bind.apply(target, [null].concat(preserveObjects(args))))()
}
return new Transform(args[0], function() {
return new (target.bind.apply(target, [null].concat(arguments)))()
}, args)
},
obj: function(sources) {
return sources
//return new ObjectTransform(sources[0], transform, sources)
},
val: function(value) {
return value && value.valueOf()
},
cls: function(definitions) {
reactive = this // TODO: clean this up
return function(Class) {
var prototype = Class.prototype
if (!(prototype instanceof Variable)) {
if (Object.getPrototypeOf(prototype) == Object.prototype) {
Object.setPrototypeOf(Class, Variable)
Object.setPrototypeOf(prototype, Variable.prototype)
} else {
console.warn('Unable to make class a reactive variable')
}
}
for (var key in definitions) {
defineProperty(prototype, key, definitions[key])
}
}
}
})
lang.copy(reactive, operators)
function getType(Type) {
if (typeMappings.has(Type)) {
return typeMappings.get(Type)
} else if (typeof Type === 'object') {
if (Type instanceof Array) {
if (Type[0]) {
return VArray.of(getType(Type[0]))
} else {
return VArray
}
}
var typedObject = {}
var hasKeys
for (var key in Type) {
typedObject[key] = getType(Type[key])
hasKeys = true
}
return hasKeys ? Variable.with(typedObject) : Variable
}
return Type
}
function defineProperty(target, key, Type) {
if (!Type) {
console.warn('Invalid type specified for', target && target.constructor.name, 'property', key, '(ensure you are using a concrete type, not an interface)')
} else if (!Type.notifies) {
Type = getType(Type) || Variable
}
Object.defineProperty(target, key, {
get: function() {
return reactive.get(this, key, Type)
},
set: function(value) {
reactive.set(this, key, value)
},
enumerable: true
})
}
return reactive
}))