-
Notifications
You must be signed in to change notification settings - Fork 4
/
var.coffee
237 lines (192 loc) · 7.58 KB
/
var.coffee
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
# Copyright 2015, Quixey Inc.
# All rights reserved.
#
# Licensed under the Modified BSD License found in the
# LICENSE file in the root directory of this source tree.
class J.VALUE_NOT_READY extends Error
constructor: ->
@name = "J.VALUE_NOT_READY"
@message = "Value not ready."
J.makeValueNotReadyObject = ->
# The commented-out lines are kinda helpful
# for debugging but slow as hell.
# e = Error()
obj = new J.VALUE_NOT_READY
obj.isServer = Meteor.isServer
# obj.stack = e.stack
obj
J.tryGet = (func, defaultValue) ->
# If value is ready then return it, otherwise
# return undefined (rather than throwing)
try
func()
catch e
throw e unless e instanceof J.VALUE_NOT_READY
defaultValue
class J.Var
# TODO: Fancy granular deps
# general:
# equals
# numbers:
# lessThan, greaterThan, lessThanOrEq, greaterThanOrEq
# arrays:
# contains (can keep an object-set for this)
constructor: (value, options) ->
# Options:
# tag: A toString-able object for debugging
# onChange: function(oldValue, newValue) or null
# creator: The computation that "created" this var,
# which makes this var not active when it
# invalidates.
# wrap: If true, wrap the argument to @set in a List
# or Dict if it's an array or plain object.
if arguments.length is 0
value = J.makeValueNotReadyObject()
unless @ instanceof J.Var and not @_id?
return new J.Var value, options
@_id = J.getNextId()
if J.debugGraph then J.graph[@_id] = @
if options?.creator is undefined
@creator = Tracker.currentComputation ? null
else if options.creator instanceof Tracker.Computation or not options.creator?
@creator = options.creator
else
throw new Error "Invalid Var creator: #{options.creator}"
@creator?.onInvalidate => delete @creator
@equalsFunc = options?.equalsFunc ? null
@tag = if J.debugTags then J.util.stringifyTag(options?.tag) else null
if _.isFunction options?.onChange
@onChange = options.onChange
else if options?.onChange?
throw new Error "Invalid Var onChange: #{options.onChange}"
else
@onChange = null
@wrap = options?.wrap ? true
@_getters = [] # computations
# @_value is never undefined. It can be J.VALUE_NOT_READY
# which causes get() to either throw VALUE_NOT_READY or return
# undefined when there is no active computation.
@_previousReadyValue = undefined
initValue = @_value = @maybeWrap value
debug: ->
console.log @toString()
get: ->
getter = Tracker.currentComputation
if not @isActive()
throw new Error "Can't get value of inactive Var: #{@}"
if getter?
if getter not in @_getters
@_getters.push getter
getter.onInvalidate =>
@_getters.splice @_getters.indexOf(getter), 1
if (
(@_value instanceof J.List or @_value instanceof J.Dict) and
@_value.creator?
)
# Normally Lists and Dicts control their own reactivity when methods
# are called on them. The exception is when they get stopped.
@_value.creator._addSideGetter getter
if @_value instanceof J.VALUE_NOT_READY
throw @_value
else
@_value
isActive: ->
not @creator?.invalidated
set: (value) ->
if not @isActive()
throw new Error "Can't set value of inactive Var: #{@}"
setter = Tracker.currentComputation
previousValue = @_value
newValue = @_value = @maybeWrap value
if @onChange?
if previousValue not instanceof J.VALUE_NOT_READY
@_previousReadyValue = previousValue
if not (@equalsFunc ? J.util.equals) newValue, previousValue
for getter in _.clone @_getters
unless getter is setter is @creator
getter.invalidate()
if (
@onChange? and
@_value not instanceof J.VALUE_NOT_READY and
not (@equalsFunc ? J.util.equals) @_previousReadyValue, @_value
)
# Need lexically scoped oldValue and newValue because the
# current behavior is to save a series of changes.
# E.g. @set(4), @set(7), @set(3) can all happen synchronously
# and cause @onChange(undefined, 4), @onChange(4, 7) and
# @onChange(7, 3) to both be called after flush, in the correct
# order of course.
previousReadyValue = @_previousReadyValue
@_previousReadyValue = undefined # Free the memory
Tracker.afterFlush =>
# Only call onChange if we're still active, even though
# there may be multiple onChange calls queued up from when
# we were still active.
if @isActive()
@onChange.call @, previousReadyValue, newValue
@_value
toString: ->
s = "Var[#{@_id}](#{J.util.stringifyTag @tag ? ''}=#{J.util.stringify @_value})"
if not @isActive() then s += " (inactive)"
s
tryGet: (defaultValue) ->
J.tryGet(
=> @get()
defaultValue
)
maybeWrap: (value, withFieldFuncs = true) ->
if value is undefined
throw new Error "Can't set #{@toString()} value to undefined.
Use null or new J.VALUE_NOT_READY instead."
else if value instanceof J.AutoVar
throw new Error "Can't put an AutoVar inside #{@toString()}:
#{value}. Get its value with .get() first."
if not @wrap and (_.isArray(value) or J.util.isPlainObject(value))
return value
J.Var.wrap value, withFieldFuncs
@deepUnwrap: (value) ->
if value instanceof J.List
value.toArr()
else if _.isArray(value)
J.List(value).toArr()
else if value instanceof J.Dict
value.toObj()
else if J.util.isPlainObject value
J.Dict(value).toObj()
else
value
@unwrap: (value) ->
if value instanceof J.List or _.isArray value
J.List.unwrap value
else if value instanceof J.Dict or J.util.isPlainObject value
J.Dict.unwrap value
else
value
@wrap: (value, withFieldFuncs = true) ->
if value is undefined
if Tracker.active
throw new Error "Undefined is an invalid value.
Use null or new J.VALUE_NOT_READY instead."
else
J.makeValueNotReadyObject()
else if value instanceof J.AutoVar
throw new Error "A whole AutoVar is not a valid:
value: #{value}. Get its value with .get() first."
if value instanceof J.VALUE_NOT_READY
value
else if J.util.isPlainObject value
J.Dict value,
fineGrained: false
withFieldFuncs: withFieldFuncs
else if _.isArray(value)
J.List value,
fineGrained: false
else if value instanceof J.Var
# Prevent any nested J.Var situation
try
value.get()
catch e
throw e unless e instanceof J.VALUE_NOT_READY
e
else
value