kdf
Version:
104 lines (69 loc) • 2.49 kB
text/coffeescript
KD = require './kd.coffee'
KDEventEmitter = require './eventemitter.coffee'
module.exports = class KDObject extends KDEventEmitter
[NOTREADY, READY] = [0,1]
utils: KD.utils
constructor:(options = {}, data)->
or= options.id or KD.utils.getUniqueId()
options
data if data
options.delegate if options.delegate
super
if options.testPath
KD.registerInstanceForTesting this
'error', error
'ready', => = READY
define: (property, options) ->
options = { get: options } if 'function' is typeof options
Object.defineProperty this, property, options
bound: (method)->
unless 'function' is typeof @[method]
throw new Error "bound: unknown method! #{method}"
boundMethod = "__bound__#{method}"
boundMethod of this or Object.defineProperty(
this, boundMethod, value: @[method].bind this
)
return @[boundMethod]
lazyBound: (method, rest...)-> @[method].bind this, rest...
forwardEvent: (target, eventName, prefix="") ->
target.on eventName, 'emit', prefix + eventName
forwardEvents: (target, eventNames, prefix="") ->
target, eventName, prefix for eventName in eventNames
ready:(listener)->
if Promise?::nodeify
new Promise (resolve) =>
resolve() if is READY
'ready', resolve
.nodeify listener
else if is READY then .defer listener
else 'ready', listener
registerSingleton:KD.registerSingleton
getSingleton:KD.getSingleton
getInstance:(instanceId)->
KD.getAllKDInstances()[instanceId] ? null
registerKDObjectInstance: -> KD.registerInstance @
setData:( )->
getData:->
setOptions:( = {})->
setOption:(option, value)-> [option] = value
unsetOption:(option)-> delete [option] if [option]
getOptions:->
getOption:(key)-> [key] ? null
changeId:(id)->
KD.deleteInstance id
= id
KD.registerInstance @
getId:->
setDelegate:( )->
getDelegate:->
destroy:->
= yes
'KDObjectWillBeDestroyed'
KD.deleteInstance
# good idea but needs some refactoring
# @[prop] = null for own prop of this
chainNames:(options)->
options.chain
options.newLink
"#{options.chain}.#{options.newLink}"