This repository has been archived by the owner on Dec 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Casing.coffee
430 lines (338 loc) · 16.8 KB
/
Casing.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Copyright (c) 2018 Natalie Marleny
# Casing - UI framework for Framer
# License: MIT
# URL: https://github.com/nataliemarleny/Casing
# -----------------------------------------------------------------------------
# Utility functions
exports.getScreenFramePoint = (layer) -> (_.pick layer.screenFrame, ['x', 'y'])
exports.sizePositionApply = (sourceComp, targetComp) ->
targetComp.parent = sourceComp.parent
_.assign targetComp, sourceComp.frame
exports.autoPosition = (parentComp, referenceComp, comps) ->
referenceFramePoint = _.cloneDeep(exports.getScreenFramePoint(referenceComp))
for compName, comp of comps
_.assign comp, (_.mergeWith exports.getScreenFramePoint(comp), referenceFramePoint, _.subtract)
_.assign(comp, {parent: parentComp}) for compName, comp of comps
exports.invokeOnce = (func) ->
return {
'invokeOnce': true
'func': func
}
exports.constructModule = (moduleName) ->
->
module = require moduleName
new module[moduleName]
# -----------------------------------------------------------------------------
# Purpose of data-bundles: changes propagation, history of values, custom properties
class DataBundle
constructor: (@_componentName, @_dataName, @_dataValue, @_app) ->
Object.defineProperty DataBundle.prototype, "value",
configurable: true
get: () -> (@_dataValue)
set: (newValue) ->
# If the new value differs from registered
if @_dataValue != newValue
@_dataValue = newValue
@_app._updateData(@_componentName, @_dataName, newValue)
Object.defineProperty DataBundle.prototype, "_data",
configurable: true
get: () -> (@_app._data)
Object.defineProperty DataBundle.prototype, "_history",
configurable: true
get: () -> (_.map @_app._dataIsolatedHistory, "#{@_componentName}.#{@_dataName}")
Object.defineProperty DataBundle.prototype, "_historyChanges",
configurable: true
get: () ->
trackTransitions = (acc, next_item) ->
if acc == [] or (not _.isEqual(acc[acc.length - 1], next_item))
acc.push(next_item)
return acc
return _.reduce @_history, trackTransitions, []
# -----------------------------------------------------------------------------
# Retrieves the names of the function parameters (for dependency-injection)
# Adapted from: https://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically
STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg
ARGUMENT_NAMES = /([^\s,]+)/g
getParamNames = (func) ->
fnStr = func.toString().replace(STRIP_COMMENTS, '')
result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES)
if result == null
result = []
return result
# -----------------------------------------------------------------------------
# Application implementation: manages the screens and data
# entrypoint method
class exports.App extends Layer
constructor: (options) ->
[_d, _t] = @_log_performance_prep(true)
_.defaults options,
backgroundColor: "#EFEFEF"
frame: Screen.frame
# Pattern to use when detecting wiring functions
wireComponentMethodPrefix: "wiring_"
showErrors: true
showWarnings: true
showPerformance: false
lowPerformanceWidth: 145
dataInit: {}
dataProperties: {}
super options
_.assign @, _.pick options, [
'wireComponentMethodPrefix'
'showErrors'
'showWarnings'
'showPerformance'
'lowPerformanceWidth'
]
_.assign @,
screen_switch_happened: false
# ... componentName == "_" for global-data
# {componentName: {localDataName: DataBundle(dataValue)}}
@_dataIsolated = {}
# ... global-data mapped into component-local-context (with appropriate rename)
# {componentName: {localDataName: DataBundle(dataValue)}}
@data = @_data = {}
# ... componentName == "_" for global-data-history
# [{componentName: {localDataName: dataIntialValue}}, {componentName: {localDataName: dataValue2Snapshot}}]
@dataHistory = @_dataIsolatedHistory = [{}]
# {componentName, {localDataName: "targetComponentName.targetComponentValue"}}
@_dataLink = {}
# Set(propertyName)
@_dataProperties = new Set()
# {componentName: componentSpec}
@_componentSpecs = {}
# {screenName: [componentName]}
@_screenSpecs = {}
# ... values that change with the screen of the app
# {componentName: component}
@_activeComponents = {}
# {sourceComponentName: {sourceDataName: ["componentName.methodName"]}}
@_activeUpdateLists = {}
# ... initialize the data and the properties
@_setupDataDict "_", options.dataInit
@_setupDataPropertiesDict options.dataProperties
# Screen state-machine transitions defined
@on Events.StateSwitchEnd, @_screenTransition
@_log_performance _d, _t, "App.constructor"
# entrypoint method
defineComponent: (componentSpec) ->
[_d, _t] = @_log_performance_prep(true)
@_assert(
componentSpec.name not of @_componentSpecs
"component \"#{componentSpec.name}\" defined multiple times"
)
componentName = componentSpec.name
@_componentSpecs[componentName] = componentSpec
# ... initialize data, data-links, data-properties
@_setupDataDict componentName, componentSpec.dataInit
@_setupDataLinkDict componentName, componentSpec.dataLink
@_setupDataPropertiesDict componentSpec.dataProperties
@_log_performance _d, _t, "App.defineComponent(#{componentName})"
# entrypoint method
defineScreen: (screenName, componentList) ->
[_d, _t] = @_log_performance_prep(true)
@_assert(
screenName not of @states
"screen \"#{screenName}\" defined multiple times"
)
@_screenSpecs[screenName] = componentList
@states[screenName] = {}
@_log_performance _d, _t, "App.defineScreen(#{screenName})"
# entrypoint method
_screenTransition: (oldScreen, newScreen) ->
[_d, _t] = @_log_performance_prep(true)
# Mark when the first transition has happened
@screen_switch_happened = true
# Ignore the transitions to the same screen
if oldScreen == newScreen
return
# Remove the components from the previous screen
for componentName, component of @_activeComponents
component.destroy()
# Reset screen dependent objects
@_activeComponents = {}
@_activeUpdateLists = {}
# Create currently active components
for componentName in @_screenSpecs[newScreen]
# ... create active components for this screen
componentSpec = @_componentSpecs[componentName]
component = componentSpec.construct()
component.parent = @
@_activeComponents[componentName] = component
# Warn if any dataName is defined in local-data and linked-data
for dataName of (componentSpec?.dataInit or {})
@_assert(
not componentSpec.dataLink?[dataName]?
"local-data \"#{dataName}\" present in both local-data and linked-data"
)
# ... create active update lists for this screen
for methodName in Object.keys(Object.getPrototypeOf(component))
if methodName.startsWith(@wireComponentMethodPrefix)
# Unwrap the method if necessary
method = component[methodName]
if not _.isFunction method
method = method['func']
for methodParam in getParamNames(method)
@_assert(
@_data[componentName]?[methodParam]?
"parameter \"#{methodParam}\" present in \"#{componentName}:#{methodName}\" but not found as either local-data or linked-data"
)
# ... resolve where to subscribe the method
targetComponentDataName = "#{componentName}.#{methodParam}"
if componentSpec.dataLink?[methodParam]?
targetComponentDataName = componentSpec.dataLink[methodParam]
[targetComponentName, targetDataName] = targetComponentDataName.split(".")
@_activeUpdateLists[targetComponentName] ?= {}
@_activeUpdateLists[targetComponentName][targetDataName] ?= []
@_activeUpdateLists[targetComponentName][targetDataName].push "#{componentName}.#{methodName}"
@_updateComponentsForAllData()
@_log_performance _d, _t, "App._screenTransition(#{oldScreen}, #{newScreen})"
# entrypoint method
_updateData: (componentName, dataName, newValue) ->
[_d, _t] = @_log_performance_prep(true)
@_updateDataHistory(componentName, dataName, newValue)
@_updateComponentsForData(componentName, dataName)
@_log_performance _d, _t, "App._updateData(#{componentName}:#{dataName}, #{newValue})"
_invokeComponentMethod: (componentName, methodName, componentJustCreated = false) ->
[_d, _t] = @_log_performance_prep(false)
component = @_activeComponents[componentName]
method = component[methodName]
if not _.isFunction method
# Don't invoke multiple times if requested
if (
"invokeOnce" of method and
not componentJustCreated
)
return
# Unwrap the method
method = method["func"]
funcArguments = _.at @_data[componentName], getParamNames(method)
method.apply(component, funcArguments)
@_log_performance _d, _t, "App._invokeComponentMethod(#{componentName}, #{methodName})"
_updateComponentsForAllData: ->
[_d, _t] = @_log_performance_prep(false)
# Call methods of all active components only once
for componentName, component of (@_activeComponents or {})
for methodName in Object.keys(Object.getPrototypeOf(component))
if methodName.startsWith(@wireComponentMethodPrefix)
@_invokeComponentMethod(componentName, methodName, true)
@_log_performance _d, _t, "App._updateComponentsForAllData"
_updateComponentsForData: (componentName, dataName) ->
[_d, _t] = @_log_performance_prep(false)
for componentMethodName in (@_activeUpdateLists?[componentName]?[dataName] or [])
[compName, methodName] = componentMethodName.split(".")
@_invokeComponentMethod(compName, methodName, false)
@_log_performance _d, _t, "App._updateComponentsForData(#{componentName}:#{dataName})"
_updateDataHistory: (componentName, dataName, newValue) ->
[_d, _t] = @_log_performance_prep(false)
@_assert(
@_data[componentName]?[dataName]?
"CRITICAL INTERNAL - data not registered in history before changing, contact maintainer"
)
_lastHistoryEntry = @_dataIsolatedHistory[@_dataIsolatedHistory.length - 1]
_newHistoryEntry = _.cloneDeep _lastHistoryEntry
_newHistoryEntry[componentName][dataName] = _.cloneDeep newValue
@_dataIsolatedHistory.push(_newHistoryEntry)
# Verify consitency in data-storage
_lastFromHistory = @_dataIsolatedHistory[@_dataIsolatedHistory.length - 1]
_lastFromData = {}
for _componentName in Object.keys(@_dataIsolated)
_lastFromData[_componentName] ?= {}
for _dataName in Object.keys(@_dataIsolated[_componentName])
_lastFromData[_componentName][_dataName] = @_dataIsolated[_componentName][_dataName].value
@_assert(
_.isEqual _lastFromHistory, _lastFromData
"CRITICAL INTERNAL - inconsistency between history and current data, contact maintainer"
)
@_log_performance _d, _t, "App._updateDataHistory(#{componentName}:#{dataName}, #{newValue})"
_setupDataDict: (componentName, dataDict = {}) ->
[_d, _t] = @_log_performance_prep(false)
for dataName, dataValue of dataDict
@_setupData componentName, dataName, dataValue
@_log_performance _d, _t, "App._setupDataDict(#{componentName}, <dataDict>)"
_setupData: (componentName, dataName, dataValue) ->
[_d, _t] = @_log_performance_prep(false)
@_assert(
not @screen_switch_happened
"data \"#{componentName}:#{dataName}\" initialized to \"#{dataValue}\" after first screen-switch happened"
)
@_assert(
not @_data[componentName]?[dataName]?
"data \"#{componentName}:#{dataName}\" initialized multiple times"
)
@_dataIsolated[componentName] ?= {}
@_data[componentName] ?= {}
# ... don't copy functions
dataValueCopy = if _.isFunction dataValue then dataValue else _.cloneDeep dataValue
dataBundle = new DataBundle(componentName, dataName, dataValueCopy, @)
@_dataIsolated[componentName][dataName] = @_data[componentName][dataName] = dataBundle
@_dataIsolatedHistory[0][componentName] ?= {}
dataValueCopy = if _.isFunction dataValue then dataValue else _.cloneDeep dataValue
@_dataIsolatedHistory[0][componentName][dataName] = dataValueCopy
@_log_performance _d, _t, "App._setupData(#{componentName}:#{dataName}, #{dataValue})"
_setupDataLinkDict: (componentName, dataLinkDict = {}) ->
[_d, _t] = @_log_performance_prep(false)
for dataLinkName, dataLinkValue of dataLinkDict
@_setupDataLink componentName, dataLinkName, dataLinkValue
@_log_performance _d, _t, "App._setupDataLinkDict(#{componentName}, <dataLinks>)"
_setupDataLink: (componentName, dataLinkName, dataLinkValue) ->
[_d, _t] = @_log_performance_prep(false)
@_assert(
not @screen_switch_happened
"data-link \"#{componentName}:#{dataLinkName}\" initialized to \"#{dataLinkValue}\" after first screen-switch happened"
)
@_assert(
not @_data[componentName]?[dataLinkName]?
"data-link \"#{componentName}:#{dataLinkName}\" initialized multiple times"
)
@_dataLink[componentName] ?= {}
@_dataLink[componentName][dataLinkName] = dataLinkValue
@_data[componentName] ?= {}
@_data[componentName][dataLinkName] = _.get @_dataIsolated, dataLinkValue
@_log_performance _d, _t, "App._setupDataLink(#{componentName}:#{dataLinkName}, #{dataLinkValue})"
_setupDataPropertiesDict: (dataPropertiesDict = {}) ->
[_d, _t] = @_log_performance_prep(false)
for dataPropertyName, dataPropertyValue of dataPropertiesDict
@_setupDataProperty dataPropertyName, dataPropertyValue
@_log_performance _d, _t, "App._setupDataPropertiesDict(<dataPropertiesDict>)"
_setupDataProperty: (dataPropertyName, dataPropertyValue) ->
[_d, _t] = @_log_performance_prep(false)
@_assert(
not @screen_switch_happened
"data-property \"#{dataPropertyName}\" initialized after first screen-switch happened"
)
@_assert(
not @_dataProperties.has(dataPropertyName)
"data-property \"#{dataPropertyName}\" initialized multiple times"
)
@_dataProperties.add(dataPropertyName)
Object.defineProperty DataBundle.prototype, dataPropertyName,
configurable: true
get: dataPropertyValue
@_log_performance _d, _t, "App._setupDataProperty(#{dataPropertyName}, <dataPropertyValue>)"
_error: (message) ->
if @showErrors
throw "Casing: ERROR #{message}"
_assert: (cond, message) ->
if not cond
@_error message
_warn: (message) ->
if @showErrors or @showWarnings
print "Casing: WARN #{message}"
_assert_warn: (cond, message) ->
if not cond
@_warn message
_log_performance_prep: (isEntryPointMethod) ->
if isEntryPointMethod
@_methodCallStackDepth = 0
@_methodCallStackDepth += 1
return [@_methodCallStackDepth, performance.now()]
_log_performance: (_methodCallStackDepth, startTime, message) ->
if _methodCallStackDepth == 1
graphLines = '└'
else
graphLines = "├#{_.repeat "─", _methodCallStackDepth - 1}"
if @showPerformance
print "#{_.padStart (performance.now() - startTime).toFixed(2), 7, '_'} ms #{graphLines} #{message}".slice(0, @lowPerformanceWidth)
@_methodCallStackDepth -= 1
exports.App.prototype.switchScreen = exports.App.prototype.stateSwitch