This repository has been archived by the owner on May 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
node.coffee
283 lines (215 loc) · 6.9 KB
/
node.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
'use strict'
log = (require 'debug') 'scent:node'
_ = require 'lodash'
fast = require 'fast.js'
Lill = require 'lill'
{Symbol, Map} = require 'es6'
Component = require './component'
{bType} = symbols = require './symbols'
bData = Symbol 'internal data for the nodelist'
# Constructor function to create node type with given set
# of component types. Used internally.
NodeType = (componentTypes) ->
unless this instanceof NodeType
return new NodeType componentTypes
componentTypes = NodeType.validateComponentTypes componentTypes
unless componentTypes?.length
throw new TypeError 'node type requires at least one component type'
this[ bData ] = {
list: componentTypes
item: createNodeItem(this, componentTypes)
pool: fast.bind poolNodeItem, null, []
ref: Symbol 'node('+componentTypes.map(mapComponentName).join(',')+')'
added: false
removed: false
}
return Lill.attach this
# Method checks if entity fulfills component types constraints
# defined for node type.
NodeType::entityFits = (entity) ->
return false if entity[ symbols.bDisposing ]
for componentType in this[ bData ].list
return false unless entity.has componentType
return true
# Method is used to add new entity to the list. It rejects
# entity that is already on the list or if required components
# are missing.
NodeType::addEntity = ->
data = this[ bData ]
entity = validateEntity arguments[0]
# entity already watched by this node type or
# it doesn't fit in here
if entity[ data.ref ] or not @entityFits entity
return this
# grab node item from pool or create new one
unless nodeItem = data.pool()
nodeItem = new data.item
# mutual references
nodeItem[ symbols.bEntity ] = entity
entity[ data.ref ] = nodeItem
# store node item to the node type
Lill.add this, nodeItem
# if there are handlers for onAdded, remember node item
Lill.add added, nodeItem if added = data.added
return this
# Method to remove entity from the node type if it no longer
# fits in the node type constrains.
NodeType::removeEntity = ->
data = this[ bData ]
entity = validateEntity arguments[0]
return this unless nodeItem = entity[ data.ref ]
return this if @entityFits entity
Lill.remove this, nodeItem
delete entity[ data.ref ]
# if anything watches for removed nodes the actual pooling is postponed
if removed = data.removed
Lill.add removed, nodeItem
else
data.pool(nodeItem)
return this
# For entity that is not part of the node type, it will be
# checked against component type constrains and added if valid.
# Otherwise entity is removed from node type forcefully.
NodeType::updateEntity = ->
data = this[ bData ]
entity = validateEntity arguments[0]
unless entity[ data.ref ]
return this.addEntity entity
else
return this.removeEntity entity
return this
# Wrapper method for looping over node items.
NodeType::each = (fn) ->
if arguments.length <= 1
Lill.each this, fn
return this
args = Array.prototype.slice.call arguments, 1
Lill.each this, (node) ->
fn(node, args...)
return this
# Allow to register callback function that will be called
# whenever new entity is added to the node type. Callbacks
# will be executed when finish() method is invoked.
NodeType::onAdded = (callback) ->
unless _.isFunction callback
throw new TypeError 'expected callback function for onNodeAdded call'
{added} = data = this[ bData ]
unless added
data.added = added = []
Lill.attach added
added.push callback
return this
# Similar to onAdded, but invokes callbacks for each removed
# entity when finish() method is invoked.
NodeType::onRemoved = (callback) ->
unless _.isFunction callback
throw new TypeError 'expected callback function for onNodeRemoved call'
{removed} = data = this[ bData ]
unless removed
data.removed = removed = []
Lill.attach removed
removed.push callback
return this
# Used to invoke registered onAdded and onRemoved callbacks.
NodeType::finish = ->
data = this[ bData ]
if (added = data.added) and Lill.getSize(added)
for addedCb in added
Lill.each added, addedCb
Lill.clear added
if (removed = data.removed) and Lill.getSize(removed)
for removedCb in removed
Lill.each removed, removedCb
# return removed nodes to pool
Lill.each removed, data.pool
Lill.clear removed
return this
# Some convenient properties from Lill.
Object.defineProperties NodeType.prototype,
'head':
enumerable: yes
get: -> Lill.getHead this
'tail':
enumerable: yes
get: -> Lill.getTail this
'size':
enumerable: yes
get: -> Lill.getSize this
## NODEITEM
# Creates node item constructor function having properties
# for component types attached to prototype.
createNodeItem = (nodeType, componentTypes) ->
NodeItem = ->
NodeItem.prototype = new BaseNodeItem nodeType
for componentType in componentTypes
defineComponentProperty NodeItem, componentType
return NodeItem
# Define property with name of component type and getter that
# returns current component instance from entity.
defineComponentProperty = (nodeItemConstructor, componentType) ->
Object.defineProperty(
nodeItemConstructor.prototype
componentType.typeName
{
enumerable: yes
get: -> this[ symbols.bEntity ].get(componentType, true)
}
)
## BASENODEITEM
BaseNodeItem = (nodeType) ->
this[ symbols.bType ] = nodeType
return this
BaseNodeItem::[ symbols.bType ] = null
BaseNodeItem::[ symbols.bEntity ] = null
Object.defineProperty BaseNodeItem.prototype, 'entityRef', {
enumerable: yes
get: -> this[ symbols.bEntity ]
}
## INSPECTION
BaseNodeItem::inspect = ->
result = {
"--nodeType": this[ symbols.bType ].inspect(yes)
"--entity": this[ symbols.bEntity ].inspect()
}
for componentType in this[ symbols.bType ][ bData ].list
result[componentType.typeName] = this[componentType.typeName]?.inspect()
return result
NodeType::inspect = (metaOnly) ->
data = this[ bData ]
result = {
"--nodeSpec": data.list.map(mapComponentName).join(',')
"--listSize": this.size
}
return result if metaOnly is yes
toResult = (label, source) ->
return unless source and Lill.getSize(source)
target = result[label] = []
Lill.each source, (item) ->
target.push item.inspect()
toResult 'all', this
toResult 'added', data.added
toResult 'removed', data.removed
return result
## UTILITY FUNCTIONS
mapComponentName = (componentType) ->
return componentType.typeName
poolNodeItem = (pool, nodeItem) ->
unless nodeItem and pool.length
return pool.pop()
nodeItem[ symbols.bEntity ] = null
pool.push nodeItem
validateEntity = (entity) ->
unless entity and _.isFunction(entity.get)
throw new TypeError 'invalid entity for node type'
return entity
validateComponentType = (componentType) ->
return false unless componentType
return componentType instanceof Component
NodeType.validateComponentTypes = (types) ->
unless _.isArray types
_types = _([types])
else
_types = _(types)
# filter out duplicates and invalid component types
return _types.uniq().filter(validateComponentType).value()
module.exports = Object.freeze NodeType