Skip to content

Commit 252d2d4

Browse files
author
Kamil Kisiela
committed
fix(reactive): empty collection on cursor change
1 parent 9f0df5f commit 252d2d4

File tree

4 files changed

+88
-51
lines changed

4 files changed

+88
-51
lines changed

dist/angular-meteor.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,25 +2151,47 @@
21512151
$$Reactive.$$setFnHelper = function (vm, k, fn) {
21522152
var _this3 = this;
21532153

2154-
this.autorun(function (computation) {
2154+
var activeObservation = null;
2155+
var lastModel = null;
2156+
var lastModelData = [];
2157+
2158+
this.autorun(function () /* computation */{
21552159
// Invokes the reactive functon
21562160
var model = fn.apply(vm);
21572161

21582162
// Ignore notifications made by the following handler
21592163
Tracker.nonreactive(function () {
21602164
// If a cursor, observe its changes and update acoordingly
21612165
if ($$utils.isCursor(model)) {
2162-
(function () {
2163-
var observation = _this3.$$handleCursor(vm, k, model);
2164-
2165-
computation.onInvalidate(function () {
2166-
observation.stop();
2167-
vm[k].splice(0);
2168-
});
2169-
})();
2166+
var modelData = void 0;
2167+
2168+
if (angular.isUndefined(vm[k])) {
2169+
_this3.$$setValHelper(vm, k, [], false);
2170+
}
2171+
2172+
if (activeObservation) {
2173+
lastModelData = lastModel.fetch();
2174+
activeObservation.stop();
2175+
activeObservation = null;
2176+
}
2177+
2178+
var handle = _this3.$$handleCursor(vm, k, model);
2179+
2180+
activeObservation = handle.observation;
2181+
modelData = handle.data;
2182+
2183+
var diff = jsondiffpatch.diff(lastModelData, modelData);
2184+
vm[k] = jsondiffpatch.patch(lastModelData, diff);
2185+
2186+
lastModel = model;
2187+
lastModelData = modelData;
2188+
2189+
/* computation.onInvalidate(() => {
2190+
activeObservation.stop();
2191+
});*/
21702192
} else {
2171-
_this3.$$handleNonCursor(vm, k, model);
2172-
}
2193+
_this3.$$handleNonCursor(vm, k, model);
2194+
}
21732195

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

2210-
// If not defined set it
2211-
if (angular.isUndefined(vm[k])) {
2212-
this.$$setValHelper(vm, k, cursor.fetch(), false);
2213-
}
2214-
// If defined update it
2215-
else {
2216-
var diff = jsondiffpatch.diff(vm[k], cursor.fetch());
2217-
jsondiffpatch.patch(vm[k], diff);
2218-
}
2219-
2232+
var data = [];
22202233
// Observe changes made in the result set
22212234
var observation = cursor.observe({
22222235
addedAt: function addedAt(doc, atIndex) {
2223-
if (!observation) return;
2236+
if (!observation) {
2237+
data.push(doc);
2238+
return;
2239+
}
22242240
vm[k].splice(atIndex, 0, doc);
22252241
_this5.$$changed(vm, k);
22262242
},
@@ -2240,7 +2256,10 @@
22402256
}
22412257
});
22422258

2243-
return observation;
2259+
return {
2260+
observation: observation,
2261+
data: data
2262+
};
22442263
};
22452264

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

dist/angular-meteor.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/angular-meteor.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/reactive.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ angular.module(name, [
3636
if (!_.isObject(props)) {
3737
throw Error('argument 2 must be an object');
3838
}
39-
}
40-
else {
39+
} else {
4140
props = vm;
4241
vm = $Mixer.caller;
4342

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

5554
_.each(props, (v, k) => {
5655
if (!vm.$$dependencies[k]) {
57-
// Registers a new dependency to the specified helper
56+
// Registers a new dependency to the specified helper
5857
vm.$$dependencies[k] = new Tracker.Dependency();
5958
}
6059

@@ -73,8 +72,7 @@ angular.module(name, [
7372
if (!_.isBoolean(isDeep)) {
7473
throw Error('argument 3 must be a boolean');
7574
}
76-
}
77-
else {
75+
} else {
7876
isDeep = angular.isDefined(k) ? k : false;
7977
k = vm;
8078
vm = $Mixer.caller;
@@ -96,8 +94,7 @@ angular.module(name, [
9694
if (!_.isString(k)) {
9795
throw Error('argument 2 must be a string');
9896
}
99-
}
100-
else {
97+
} else {
10198
k = vm;
10299
vm = $Mixer.caller;
103100

@@ -139,20 +136,44 @@ angular.module(name, [
139136

140137
// Invokes a function and sets the return value as a property
141138
$$Reactive.$$setFnHelper = function(vm, k, fn) {
142-
this.autorun((computation) => {
139+
let activeObservation = null;
140+
let lastModel = null;
141+
let lastModelData = [];
142+
143+
this.autorun((/* computation */) => {
143144
// Invokes the reactive functon
144145
const model = fn.apply(vm);
145146

146147
// Ignore notifications made by the following handler
147148
Tracker.nonreactive(() => {
148149
// If a cursor, observe its changes and update acoordingly
149150
if ($$utils.isCursor(model)) {
150-
const observation = this.$$handleCursor(vm, k, model);
151+
let modelData;
152+
153+
if (angular.isUndefined(vm[k])) {
154+
this.$$setValHelper(vm, k, [], false);
155+
}
156+
157+
if (activeObservation) {
158+
lastModelData = lastModel.fetch();
159+
activeObservation.stop();
160+
activeObservation = null;
161+
}
151162

152-
computation.onInvalidate(() => {
153-
observation.stop();
154-
vm[k].splice(0);
155-
});
163+
const handle = this.$$handleCursor(vm, k, model);
164+
165+
activeObservation = handle.observation;
166+
modelData = handle.data;
167+
168+
const diff = jsondiffpatch.diff(lastModelData, modelData);
169+
vm[k] = jsondiffpatch.patch(lastModelData, diff);
170+
171+
lastModel = model;
172+
lastModelData = modelData;
173+
174+
/* computation.onInvalidate(() => {
175+
activeObservation.stop();
176+
});*/
156177
} else {
157178
this.$$handleNonCursor(vm, k, model);
158179
}
@@ -187,20 +208,14 @@ angular.module(name, [
187208

188209
// Fetching a cursor and updates properties once the result set has been changed
189210
$$Reactive.$$handleCursor = function(vm, k, cursor) {
190-
// If not defined set it
191-
if (angular.isUndefined(vm[k])) {
192-
this.$$setValHelper(vm, k, cursor.fetch(), false);
193-
}
194-
// If defined update it
195-
else {
196-
const diff = jsondiffpatch.diff(vm[k], cursor.fetch());
197-
jsondiffpatch.patch(vm[k], diff);
198-
}
199-
211+
const data = [];
200212
// Observe changes made in the result set
201213
const observation = cursor.observe({
202214
addedAt: (doc, atIndex) => {
203-
if (!observation) return;
215+
if (!observation) {
216+
data.push(doc);
217+
return;
218+
}
204219
vm[k].splice(atIndex, 0, doc);
205220
this.$$changed(vm, k);
206221
},
@@ -220,7 +235,10 @@ angular.module(name, [
220235
}
221236
});
222237

223-
return observation;
238+
return {
239+
observation,
240+
data
241+
};
224242
};
225243

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

0 commit comments

Comments
 (0)