Skip to content

Commit

Permalink
Making it possible to order observation updates by their depth automa…
Browse files Browse the repository at this point in the history
…tically (#155)

* maybe using can.setElement

* include element

* use domQueue

* updating can-queues
  • Loading branch information
phillipskevin authored Aug 5, 2019
1 parent ca92394 commit 46c62ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
20 changes: 14 additions & 6 deletions can-observation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var getValueDependenciesSymbol = canSymbol.for("can.getValueDependencies");

// ## Observation constructor
function Observation(func, context, options){
this.deriveQueue = queues.deriveQueue;

this.func = func;
this.context = context;
this.options = options || {priority: 0, isObservable: true};
Expand Down Expand Up @@ -99,7 +101,8 @@ canReflect.assign(Observation.prototype, {
this,
[],
{
priority: this.options.priority
priority: this.options.priority,
element: this.options.element
}
];
//!steal-remove-start
Expand All @@ -109,7 +112,8 @@ canReflect.assign(Observation.prototype, {
this,
[],
{
priority: this.options.priority
priority: this.options.priority,
element: this.options.element
/* jshint laxcomma: true */
, log: [ canReflect.getName(this.update) ]
/* jshint laxcomma: false */
Expand All @@ -121,7 +125,7 @@ canReflect.assign(Observation.prototype, {
}
//!steal-remove-end
// Update this observation after all `notify` tasks have been run.
queues.deriveQueue.enqueue.apply(queues.deriveQueue, queuesArgs);
this.deriveQueue.enqueue.apply(this.deriveQueue, queuesArgs);
}
},
// Called to update its value as part of the `derive` queue.
Expand Down Expand Up @@ -168,7 +172,7 @@ canReflect.assign(Observation.prototype, {
// It's possible that a child dependency of this observable might be queued
// to change. Check all child dependencies and make sure they are up-to-date by
// possibly running what they have registered in the derive queue.
if(queues.deriveQueue.tasksRemainingCount() > 0) {
if(this.deriveQueue.tasksRemainingCount() > 0) {
Observation.updateChildrenAndSelf(this);
}

Expand Down Expand Up @@ -239,6 +243,10 @@ var observationProto = {
},
"can.setPriority": function(priority){
this.options.priority = priority;
},
"can.setElement": function(element) {
this.options.element = element;
this.deriveQueue = queues.domQueue || queues.deriveQueue;
}
};

Expand All @@ -261,10 +269,10 @@ Observation.updateChildrenAndSelf = function(observation){
// the value is right.
// > NOTE: This only works for `Observation` right now. We need a way of knowing how
// > to find what an observable might have in the `deriveQueue`.
if(observation.update !== undefined && queues.deriveQueue.isEnqueued( observation.update ) === true) {
if(observation.update !== undefined && observation.deriveQueue.isEnqueued( observation.update ) === true) {
// TODO: In the future, we should be able to send log information
// to explain why this needed to be updated.
queues.deriveQueue.flushQueuedTask(observation.update);
observation.deriveQueue.flushQueuedTask(observation.update);
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@
"can-log": "^1.0.0",
"can-namespace": "1.0.0",
"can-observation-recorder": "^1.0.0",
"can-queues": "^1.0.0",
"can-queues": "^1.3.0",
"can-reflect": "^1.7.0",
"can-symbol": "^1.4.2"
},
"devDependencies": {
"bit-docs": "^0.0.7",
"can-cid": "^1.0.0",
"detect-cyclic-packages": "^1.1.0",
"docco": "^0.7.0",
Expand Down

0 comments on commit 46c62ec

Please sign in to comment.