Skip to content

Commit 0fcd6ea

Browse files
committed
We need to make sure that the observer still exists when emitting because maybe has been removed after calling previous observers
1 parent 9c4107a commit 0fcd6ea

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dop",
3-
"version": "0.24.2",
3+
"version": "0.24.3",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",

src/core/objects/emitToObservers.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dop.core.emitToObservers = function(mutations) {
44
var mutation,
55
path_id,
66
observer_id,
7-
observers = {},
7+
mutationsToEmitByIdObserver = {},
88
mutationsWithSubscribers = false,
99
data_path = dop.data.path,
1010
index = 0,
@@ -17,34 +17,37 @@ dop.core.emitToObservers = function(mutations) {
1717
if (!mutationsWithSubscribers && isObject(dop.data.object[dop.getObjectId(mutation.object)]))
1818
mutationsWithSubscribers = true;
1919

20-
20+
// .observers
2121
if (data_path[path_id] !== undefined && data_path[path_id].observers !== undefined) {
2222
for (observer_id in data_path[path_id].observers) {
23-
if (observers[observer_id] === undefined)
24-
observers[observer_id] = [];
25-
observers[observer_id].push(mutation);
23+
if (mutationsToEmitByIdObserver[observer_id] === undefined)
24+
mutationsToEmitByIdObserver[observer_id] = [];
25+
mutationsToEmitByIdObserver[observer_id].push(mutation);
2626
}
2727
}
2828

29-
// If mutation is swap type we should skip
30-
if (mutation.swaps === undefined) {
29+
// .observers_prop
30+
if (mutation.swaps === undefined) { // If mutation is swaps type we should skip because does not have observers_prop and also the length never changes
3131
path_id += dop.core.pathSeparator(mutation.splice===undefined ? mutation.prop : 'length');
3232
if (data_path[path_id] !== undefined && data_path[path_id].observers_prop !== undefined) {
3333
for (observer_id in data_path[path_id].observers_prop) {
34-
if (observers[observer_id] === undefined)
35-
observers[observer_id] = [];
34+
if (mutationsToEmitByIdObserver[observer_id] === undefined)
35+
mutationsToEmitByIdObserver[observer_id] = [];
3636
// We have to check this because we dont want to duplicate
37-
if (observers[observer_id].indexOf(mutation) == -1)
38-
observers[observer_id].push(mutation);
37+
if (mutationsToEmitByIdObserver[observer_id].indexOf(mutation) == -1)
38+
mutationsToEmitByIdObserver[observer_id].push(mutation);
3939
}
4040
}
4141
}
4242

4343
}
4444

4545
// Emiting
46-
for (observer_id in observers)
47-
dop.data.observers[observer_id].callback(observers[observer_id]);
46+
for (observer_id in mutationsToEmitByIdObserver) {
47+
var observer = dop.data.observers[observer_id];
48+
if (observer !== undefined) // We need to make sure that the observer still exists, because maybe has been removed after calling previous observers
49+
observer.callback(mutationsToEmitByIdObserver[observer_id]);
50+
}
4851

4952
return mutationsWithSubscribers;
5053
};

src/core/objects/storeMutation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dop.core.storeMutation = function(mutation) {
4141
dop.core.runInterceptors = function(interceptors, type, mutation) {
4242
if (interceptors && (interceptors=interceptors[type]) && interceptors.length>0)
4343
for (var index=0,total=interceptors.length; index<total; ++index)
44-
if (interceptors[index](mutation) !== true)
44+
if (interceptors[index](mutation, dop.getObjectTarget(mutation.object)) !== true)
4545
return false;
4646

4747
return true;

src/util/get.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ dop.util.get = function(object, path) {
44
if (path.length === 0)
55
return object;
66

7-
for (var index=0, total=path.length; index<total; index++) {
7+
for (var index=0, total=path.length, tmpobject; index<total; index++) {
88

9-
if (index+1<total && isObject(object[ path[index] ]))
10-
object = object[ path[index] ];
9+
tmpobject = object[ path[index] ];
10+
11+
if (index+1<total && isObject(tmpobject))
12+
object = tmpobject;
1113

1214
else if (object.hasOwnProperty(path[index]))
13-
return object[ path[index] ];
15+
return tmpobject;
1416

1517
else
1618
return undefined;

test/observers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ test('mutations with createObserver', function(t) {
5757
})
5858

5959

60+
61+
62+
test('check swap mutation with createObserver', function(t) {
63+
64+
var object = dop.register({array:[1,2,3]});
65+
var observer = dop.createObserver(function first(mutations) {
66+
t.equal(Array.isArray(mutations[0].swaps), true)
67+
t.end()
68+
});
69+
observer.observe(object.array);
70+
object.array.reverse()
71+
})

0 commit comments

Comments
 (0)