Skip to content

Commit e545b2b

Browse files
committed
Implemented observeAll
1 parent 194b6d3 commit e545b2b

File tree

5 files changed

+56
-113
lines changed

5 files changed

+56
-113
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ package-lock.json
2020
yarn.lock
2121
examples/drag/package-lock.json
2222
examples/todomvc/package-lock.json
23+
.vscode/launch.json

.vscode/launch.json

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/core/constructors/observer.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dop.core.observer.prototype.observe = function(object, property) {
2626
type = 'observers_prop'
2727
}
2828

29-
return this._observe(path_id, type)
29+
return this.observe(path_id, type)
3030
}
3131

3232
dop.core.observer.prototype.observeAll = function(object) {
@@ -40,11 +40,10 @@ dop.core.observer.prototype.observeAll = function(object) {
4040
'observer.observeAll() The object you are passing is not allocated to a registered object'
4141
)
4242

43-
return this._observe(dop.core.getPathId(path), 'observers_all')
43+
return this.observe(dop.core.getPathId(path), 'observers_all')
4444
}
4545

46-
// private
47-
dop.core.observer.prototype._observe = function(path_id, type) {
46+
dop.core.observer.prototype.observe = function(path_id, type) {
4847
var data_path = dop.data.path
4948

5049
if (data_path[path_id] === undefined) data_path[path_id] = {}
@@ -60,27 +59,6 @@ dop.core.observer.prototype._observe = function(path_id, type) {
6059
}.bind(this)
6160
}
6261

63-
// dop.core.observer.prototype.unobserve = function(object, property) {
64-
// dop.util.invariant(dop.isRegistered(object), 'observer.unobserve() needs a registered object as first parameter');
65-
// var path = dop.getObjectPath(object);
66-
// dop.util.invariant(isArray(path), 'observer.unobserve() The object you are passing is not allocated to a registered object');
67-
68-
// var path_id = dop.core.getPathId(path);
69-
// data_path = dop.data.path,
70-
// type = 'observers';
71-
72-
// // is observeProperty
73-
// if (arguments.length === 2) {
74-
// type = 'observers_prop';
75-
// path_id += dop.core.pathSeparator(property);
76-
// }
77-
78-
// if (data_path[path_id] !== undefined && data_path[path_id][type] !== undefined) {
79-
// delete data_path[path_id][type][this.id];
80-
// delete this[type][path_id];
81-
// }
82-
// };
83-
8462
dop.core.observer.prototype.destroy = function() {
8563
var path_id,
8664
data_path = dop.data.path

src/core/objects/emitToObservers.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,9 @@ dop.core.emitToObservers = function(mutations) {
4242
data_path[path_id_all].observers_all !== undefined
4343
) {
4444
for (observer_id in data_path[path_id_all].observers_all) {
45-
console.log(
46-
{ path_id_all, observer_id },
47-
data_path[path_id_all].observers_all[observer_id]
48-
)
49-
// if (
50-
// mutations_by_observers[observer_id] ===
51-
// undefined
52-
// )
53-
// mutations_by_observers[observer_id] = []
54-
// mutations_by_observers[observer_id].push(
55-
// mutation
56-
// )
45+
if (mutations_by_observers[observer_id] === undefined)
46+
mutations_by_observers[observer_id] = []
47+
mutations_by_observers[observer_id].push(mutation)
5748
}
5849
}
5950
}

test/observeAll.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,62 @@ var test = require('tape')
22
// require('tabe').createStream( test );
33
var dop = require('./.proxy').create()
44

5-
test('observeAll', function(t) {
5+
test('observing root', function(t) {
66
var object = dop.register({
77
a: { a1: true },
88
b: { b1: { b11: true } }
99
})
1010
var observer = dop.createObserver(function(mutations) {
11-
// t.equal(Array.isArray(mutations[0].swaps), true)
12-
// t.end()
11+
t.equal(mutations.length, 1)
12+
t.end()
1313
})
14-
observer.observeAll(object.b)
14+
// observer.observeAll(object.b)
1515
observer.observeAll(object)
1616
object.b.b1.b11 = false
17+
})
18+
19+
test('mutation above', function(t) {
20+
var object = dop.register({
21+
a: true,
22+
b: { b1: { b11: true } }
23+
})
24+
var observer = dop.createObserver(function(mutations) {
25+
t.equal(true, false, 'This should not happen')
26+
})
27+
observer.observeAll(object.b)
28+
object.a = false
29+
t.equal(true, true, 'All good')
1730
t.end()
1831
})
32+
33+
test('mutating both levels observing root', function(t) {
34+
var object = dop.register({
35+
a: true,
36+
b: { b1: { b11: true } }
37+
})
38+
var observer = dop.createObserver(function(mutations) {
39+
t.equal(mutations.length, 2)
40+
t.end()
41+
})
42+
observer.observeAll(object)
43+
dop.action(function() {
44+
object.a = false
45+
object.b.b1.b11 = false
46+
})()
47+
})
48+
49+
test('mutating both levels observing deep', function(t) {
50+
var object = dop.register({
51+
a: true,
52+
b: { b1: { b11: true } }
53+
})
54+
var observer = dop.createObserver(function(mutations) {
55+
t.equal(mutations.length, 1)
56+
t.end()
57+
})
58+
observer.observeAll(object.b.b1)
59+
dop.action(function() {
60+
object.a = false
61+
object.b.b1.b11 = false
62+
})()
63+
})

0 commit comments

Comments
 (0)