Skip to content

Commit

Permalink
fix(reactive): empty collection on cursor change
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Kisiela committed Mar 19, 2016
1 parent 9f0df5f commit 252d2d4
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 51 deletions.
65 changes: 42 additions & 23 deletions dist/angular-meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2151,25 +2151,47 @@
$$Reactive.$$setFnHelper = function (vm, k, fn) {
var _this3 = this;

this.autorun(function (computation) {
var activeObservation = null;
var lastModel = null;
var lastModelData = [];

this.autorun(function () /* computation */{
// Invokes the reactive functon
var model = fn.apply(vm);

// Ignore notifications made by the following handler
Tracker.nonreactive(function () {
// If a cursor, observe its changes and update acoordingly
if ($$utils.isCursor(model)) {
(function () {
var observation = _this3.$$handleCursor(vm, k, model);

computation.onInvalidate(function () {
observation.stop();
vm[k].splice(0);
});
})();
var modelData = void 0;

if (angular.isUndefined(vm[k])) {
_this3.$$setValHelper(vm, k, [], false);
}

if (activeObservation) {
lastModelData = lastModel.fetch();
activeObservation.stop();
activeObservation = null;
}

var handle = _this3.$$handleCursor(vm, k, model);

activeObservation = handle.observation;
modelData = handle.data;

var diff = jsondiffpatch.diff(lastModelData, modelData);
vm[k] = jsondiffpatch.patch(lastModelData, diff);

lastModel = model;
lastModelData = modelData;

/* computation.onInvalidate(() => {
activeObservation.stop();
});*/
} else {
_this3.$$handleNonCursor(vm, k, model);
}
_this3.$$handleNonCursor(vm, k, model);
}

// Notify change and update the view model
_this3.$$changed(vm, k);
Expand Down Expand Up @@ -2207,20 +2229,14 @@
$$Reactive.$$handleCursor = function (vm, k, cursor) {
var _this5 = this;

// If not defined set it
if (angular.isUndefined(vm[k])) {
this.$$setValHelper(vm, k, cursor.fetch(), false);
}
// If defined update it
else {
var diff = jsondiffpatch.diff(vm[k], cursor.fetch());
jsondiffpatch.patch(vm[k], diff);
}

var data = [];
// Observe changes made in the result set
var observation = cursor.observe({
addedAt: function addedAt(doc, atIndex) {
if (!observation) return;
if (!observation) {
data.push(doc);
return;
}
vm[k].splice(atIndex, 0, doc);
_this5.$$changed(vm, k);
},
Expand All @@ -2240,7 +2256,10 @@
}
});

return observation;
return {
observation: observation,
data: data
};
};

$$Reactive.$$handleNonCursor = function (vm, k, data) {
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-meteor.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/angular-meteor.min.js.map

Large diffs are not rendered by default.

68 changes: 43 additions & 25 deletions src/modules/reactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ angular.module(name, [
if (!_.isObject(props)) {
throw Error('argument 2 must be an object');
}
}
else {
} else {
props = vm;
vm = $Mixer.caller;

Expand All @@ -54,7 +53,7 @@ angular.module(name, [

_.each(props, (v, k) => {
if (!vm.$$dependencies[k]) {
// Registers a new dependency to the specified helper
// Registers a new dependency to the specified helper
vm.$$dependencies[k] = new Tracker.Dependency();
}

Expand All @@ -73,8 +72,7 @@ angular.module(name, [
if (!_.isBoolean(isDeep)) {
throw Error('argument 3 must be a boolean');
}
}
else {
} else {
isDeep = angular.isDefined(k) ? k : false;
k = vm;
vm = $Mixer.caller;
Expand All @@ -96,8 +94,7 @@ angular.module(name, [
if (!_.isString(k)) {
throw Error('argument 2 must be a string');
}
}
else {
} else {
k = vm;
vm = $Mixer.caller;

Expand Down Expand Up @@ -139,20 +136,44 @@ angular.module(name, [

// Invokes a function and sets the return value as a property
$$Reactive.$$setFnHelper = function(vm, k, fn) {
this.autorun((computation) => {
let activeObservation = null;
let lastModel = null;
let lastModelData = [];

this.autorun((/* computation */) => {
// Invokes the reactive functon
const model = fn.apply(vm);

// Ignore notifications made by the following handler
Tracker.nonreactive(() => {
// If a cursor, observe its changes and update acoordingly
if ($$utils.isCursor(model)) {
const observation = this.$$handleCursor(vm, k, model);
let modelData;

if (angular.isUndefined(vm[k])) {
this.$$setValHelper(vm, k, [], false);
}

if (activeObservation) {
lastModelData = lastModel.fetch();
activeObservation.stop();
activeObservation = null;
}

computation.onInvalidate(() => {
observation.stop();
vm[k].splice(0);
});
const handle = this.$$handleCursor(vm, k, model);

activeObservation = handle.observation;
modelData = handle.data;

const diff = jsondiffpatch.diff(lastModelData, modelData);
vm[k] = jsondiffpatch.patch(lastModelData, diff);

lastModel = model;
lastModelData = modelData;

/* computation.onInvalidate(() => {
activeObservation.stop();
});*/
} else {
this.$$handleNonCursor(vm, k, model);
}
Expand Down Expand Up @@ -187,20 +208,14 @@ angular.module(name, [

// Fetching a cursor and updates properties once the result set has been changed
$$Reactive.$$handleCursor = function(vm, k, cursor) {
// If not defined set it
if (angular.isUndefined(vm[k])) {
this.$$setValHelper(vm, k, cursor.fetch(), false);
}
// If defined update it
else {
const diff = jsondiffpatch.diff(vm[k], cursor.fetch());
jsondiffpatch.patch(vm[k], diff);
}

const data = [];
// Observe changes made in the result set
const observation = cursor.observe({
addedAt: (doc, atIndex) => {
if (!observation) return;
if (!observation) {
data.push(doc);
return;
}
vm[k].splice(atIndex, 0, doc);
this.$$changed(vm, k);
},
Expand All @@ -220,7 +235,10 @@ angular.module(name, [
}
});

return observation;
return {
observation,
data
};
};

$$Reactive.$$handleNonCursor = function(vm, k, data) {
Expand Down

0 comments on commit 252d2d4

Please sign in to comment.