Created
September 27, 2017 00:58
-
-
Save bgando/c5763d4ed54d728acd080b9ec69933da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var simpleObservable = function(value){ | |
var handlers = new KeyTree([Object, Object, Array]); | |
var obs = { | |
get: function(){ | |
Observation.add(this, "value"); | |
return this.value; | |
}, | |
set: function(value){ | |
var old = this.value; | |
this.value = value; | |
queues.enqueueByQueue(handlers.getNode(["value"]), obs, [value, old], function(handler, context, args){ | |
return {log: ["simpleObservable changed to", args[0], args[1]]}; | |
}, ["simpleObservable changed to", value, old]); | |
}, | |
value: value | |
}; | |
CID(obs); | |
// keyName => queueName => handlers | |
canReflect.assignSymbols(obs, { | |
"can.onKeyValue": function(key, handler, queueName) { | |
handlers.add([key, queueName || "mutate", handler]); | |
}, | |
"can.offKeyValue": function(key, handler, queueName) { | |
handlers.delete([key, queueName || "mutate", handler]); | |
} | |
}); | |
return obs; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the
handlers
KeyTree houses handlers organized by property then queue.{value: {mutate: [handler1,handler2], notify: [handler3] } }
Observation.add
tells any computes that they depend on thevalue
event of this observable.handlers.getNode(["value"])
return the handlers for thevalue
event grouped by queueenqueueByQueue
will queue those handlers by task. It takes:obs
);[value,old]
)CID()
adds a unique id. I'd like to get rid of this.can.onKeyValue
is what is called by any Observation (compute) binding to this observable. We add the handler in thehandlers
key tree.