This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.coffee
More file actions
executable file
·321 lines (294 loc) · 10 KB
/
Copy pathindex.coffee
File metadata and controls
executable file
·321 lines (294 loc) · 10 KB
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
###
Bongo.js
Unfancy models for MongoDB
@class: Base
@author: Christopher Thorn
###
{EventEmitter2} = require 'koding-eventemitter2'
nodePath = require 'path'
module.exports = class Base extends EventEmitter2
# native
{defineProperty} = Object
# contrib
createId = require('hat').rack()
Inflector = require 'inflector'
###
Static events
@description: see ./staticemitterdelegate.coffee
###
{@on, @off, @addListener, @removeListener, @removeAllListeners, @once
@listeners, @setMaxListeners, @emit, @getEmitter} = require './staticemitterdelegate'
# oldEmit = @emit
# @emit =(rest...)->
# if @::mq?
# [event, data] = rest
# broadcastable = @getBroadcastable()
# if broadcastable
# if Array.isArray event
# event = event.join '.'
# @::mq?.trigger @name, event, data
# else
# oldEmit.apply @, rest
###
@method: property of the constructor
@signature: Base.mixin(stuff)
@description: copy all the static and prototype members.
###
mixin: @mixin = (source)->
@[key] = val for own key,val of source when key isnt 'constructor'
@trait =(root, trait)->
if 'function' is typeof root
@mixin root
@::mixin(root::) if root::?
else
[trait, root] = [root, trait] unless trait
root ?= ''
err = null
traitPath = nodePath.join(root, trait)
traitModule = try
require traitPath
catch e
err = e
if traitModule?
@trait traitModule
else
throw new Error "Couldn't load the trait #{traitPath} #{err}"
@addGlobalListener = (event, listener) ->
@emit "global-listener-added", event, listener
getInterfaceName: @getInterfaceName = (verb, noun, isPlural)->
noun = Inflector.pluralize noun if isPlural
name = Inflector.camelize "#{verb}_#{noun}", yes, yes
invoke: @invoke = (verb, noun, rest...)->
(@[@getInterfaceName(verb, noun)]?(rest...) and true) or false
###
@method.
@signatature: initializeSharedInstance()
###
initializeSharedInstance:(options)->
model = @
if model.constructor.isShared
if options.bongo_?
{instanceId} = options.bongo_
model.bongo_ = options.bongo_
else
instanceId = createId()
model.bongo_ = {
constructorName : model.constructor.name
instanceId
}
@wildcard = yes
@maxListeners = 0
constructor:(options={})->
@initializeSharedInstance options
defineProperty @, '_events', value: {}, writeable: yes
defineProperty @, 'wildcard', value: yes, writeable: yes
defineProperty @, 'maxListeners', value: 0, writeable: yes
defineProperty @, 'listenerTree', value: {}, writeable: yes
super options
@on '*', (rest...)=>
@constructor.bongos.forEach (bongo)=>
bongo.handleEvent 'instance', @, @event, rest
addGlobalListener: (event, listener) ->
@emit "global-listener-added", event, listener
###
@property.
@signature: Base.constructors
@description - an array of all constructors (that have schema).
###
defineProperty(@, 'constructors', value: {})
###
@property.
@signature: Base.sharedConstructors
@description - an array of shared constructors.
###
defineProperty(@, 'sharedConstructors', value: {})
defineProperty(@, 'globalSharedConstructors', value: {})
###
@method: property of the constructor.
@signature: Base.extend(constructor, proto)
@param: statik - members to be mixed into the constructor.
@param: proto - an optional object (or a function that returns
an object) that provides the prototype properties for the
model. You can use this for even more sugar to assign
instance methods to your models. It should contain a "constructor"
property:
@property - constructor - a named function that will be used as
the constructor for the new klass. If you don't name your
function some features may break. There is no way to change
the name of a function runtime, since the Function::name
property is read-only. So, do give it name. :)
@description: This is a sugar method intended to simplify
inheritance for those who are using Bongo with JS. Writing
your application in CoffeeScript is recommended, but in that
case you shouldn't use Model.extend—use classes instead.
###
@extend = (statik, proto)->
props = [statik, proto]
for prop, index in props
if 'function' is typeof prop
props[index] = new prop
[proto, statik] = props unless proto
parent = @
child = proto.constructor
for own prop of parent
child[prop] = parent[prop]
ctor =->
@constructor = child
return
ctor:: = parent::
child:: = new ctor
for own prop of proto
child::[prop] = proto[prop]
child.__super__ = parent::
child.set statik
child
@addBongo =(bongo)->
defineProperty(@, 'bongos', value: []) unless @bongos?
@bongos.push bongo
@on '*', (rest...)=>
@bongos.forEach (bongo)=>
bongo.handleEvent 'static', @, @getEmitter().event, rest
###
@method: property of the constructor.
@stub.
@description: this provides a a getter for the constructor
which can then be overridden by a subklass, which will in
any case return a constructor—but maybe not this exact one.
###
@getExactConstructor =(data)-> @
###
@method.
@signature: Base::Uber(arguments...)
@description: this is sugar to simplify inheritance for
those who are using Bongo and writing their application
with JS. Avoid Uber() when you are using CoffeeScript—
prefer super().
###
Uber:->
@constructor.__super__.constructor.apply @, arguments
###
@method: property of the constructor.
@signature: Base.uber(methodName, arguments...)
@description: Call a method on the superclass.
@description: this is sugar to simplify inheritance for
those who are using Bongo and writing their application
with JS. Avoid uber() when you are using CoffeeScript—
prefer super().
###
@uber=(methodName, args...)->
@__super__.constructor[methodName].apply @, args
###
@method.
@signature: Base::uber(methodName, arguments...)
@description: Call a method on the superclass.
@description: this is sugar to simplify inheritance for
those who are using Bongo and writing their application
with JS. Avoid uber() when you are using CoffeeScript—
prefer super().
###
uber:(methodName, args...)->
@constructor.__super__[methodName].apply @, args
@registerConstructor =->
unless Base.constructors[@name]?
Base.constructors[@name] = @
###
@method: property of the constructor
@signature: Model.set(options)
@param: component - e.g.:
- "client" will be passed into setClient()
- "schema" will be passed into setSchema()
- "validators" will be passed into setValidators()
@description: overrides Base.set() and passes items
to their appropriate setter method. Ignores items
that do not have setters.
###
@set =(options)->
@registerConstructor()
for own optionName, option of options
methodName =
new Inflector("set_#{Inflector.underscore optionName}")
.decapitalize()
.camelize(yes)
.value
if 'function' is typeof @[methodName]
@[methodName] option
else
@[optionName] = option
@
###
@method.
@signature: Model.setSharedMethods(methods)
@param: methods - an object containing the following
properties:
@property: static - an array of method names to share
with the client that should be attached to the
constructor.
@property: instance - an array of method names to share
with the client that should be attached to the
prototype.
@description: methods may be negated if they were
shared by parent constructors by prefixing the method
name with one or more "!"s (so don't do double
negation, because it doesn't seem to make any sense
in this context.)
###
@setSharedMethods =(methods)->
for own methodContext, methodNames of methods
unique = {}
for methodName in methodNames
unique[methodName] = yes
defineProperty @, "#{methodContext}Methods_",
value: Object.keys unique
defineProperty @, 'sharedMethods', value: methods, enumerable: yes
@getSharedMethods =->
{
statik : @staticMethods_ or []
instance : @instanceMethods_ or []
}
@hasSharedMethod =(method)->
{statik} = @getSharedMethods()
method in statik
hasSharedMethod:(method)->
{instance} = @constructor.getSharedMethods()
method in instance
@share =(global)->
konstructor = @
{name} = konstructor
unless name
throw new Error "Can't share an unnamed constructor"
defineProperty konstructor, 'isShared', value: yes
# TODO: outsource this for scalability reasons.
# defineProperty constructor, 'sharedInstances', value: {}
Base.sharedConstructors[name] = konstructor
Base.globalSharedConstructors[name] = konstructor
@
@inheritanceChain =(Klass=@, glue)->
unless chain = @inheritanceChain_
proto = Klass.prototype
chain = [Klass]
while proto = proto.__proto__
chain.push proto.constructor
# memoize the inheritance chain because it ought not to change
# and it's a bit expensive to have to travese the inheritance
# chain every time.
defineProperty @, 'inheritanceChain_', value: chain
if glue
(constructor.name for constructor in chain).join glue
else
chain
inheritanceChain:(glue)->
Base.inheritanceChain @constructor, glue
# emit:(event, data)->
# super
# id = @getId?() or @bongo_.instanceId
# if id? and event isnt 'newListener'
# if Array.isArray event
# event = event.join('.')
# wrappedData = {}
# wrappedData[event] = data
# @mq?.trigger "updateInstance", "object-#{id}", wrappedData
@setBroadcastable =(@broadcastable_=yes)->
@getBroadcastable =->
if @broadcastable_? then @broadcastable_ else yes
toObjectRef:->
ref = new ObjectRef @