Created
September 27, 2017 00:58
-
-
Save bgando/c5763d4ed54d728acd080b9ec69933da to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the
handlersKeyTree houses handlers organized by property then queue.{value: {mutate: [handler1,handler2], notify: [handler3] } }Observation.addtells any computes that they depend on thevalueevent of this observable.handlers.getNode(["value"])return the handlers for thevalueevent grouped by queueenqueueByQueuewill queue those handlers by task. It takes:obs);[value,old])CID()adds a unique id. I'd like to get rid of this.can.onKeyValueis what is called by any Observation (compute) binding to this observable. We add the handler in thehandlerskey tree.