Skip to content

Instantly share code, notes, and snippets.

@bgando
Created September 27, 2017 00:58
Show Gist options
  • Save bgando/c5763d4ed54d728acd080b9ec69933da to your computer and use it in GitHub Desktop.
Save bgando/c5763d4ed54d728acd080b9ec69933da to your computer and use it in GitHub Desktop.
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;
};
@bgando
Copy link
Author

bgando commented Sep 27, 2017

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 the value event of this observable.

handlers.getNode(["value"]) return the handlers for the value event grouped by queue

enqueueByQueue will queue those handlers by task. It takes:

  • a context in which to call the handlers (obs);
  • the arguments to call those functions with ([value,old])
  • A function for meta information, including what to log.
  • A final argument for "why this was registered"

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 the handlers key tree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment