Skip to content

Commit f224adb

Browse files
committed
Bug fixed: collect() and action() uses last index of collectors when creating rather than 0
1 parent 8fa6d3c commit f224adb

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
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.42.0",
3+
"version": "0.43.0",
44
"main": "./dist/dop.nodejs.js",
55
"browser": "./dist/dop.js",
66
"unpkg": "./dist/dop.min.js",

src/api/action.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ dop.action = function(f) {
44
'dop.action only accept one argument as function'
55
)
66
return function() {
7-
var collector = dop.core.createCollector(dop.data.collectors, 0)
7+
var collectors = dop.data.collectors
8+
var collector = dop.core.createCollector(collectors, collectors.length)
89
var output = f.apply(this, arguments)
910
collector.emit()
1011
return output

src/api/collect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ dop.collect = function(index_function) {
55
'dop.collect only accept one argument as function'
66
)
77
var collectors = dop.data.collectors
8-
var index = is_function ? index_function(collectors) : 0
8+
var index = is_function ? index_function(collectors) : collectors.length
99
return dop.core.createCollector(collectors, index)
1010
}

test/collectors_combining.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
var test = require('tape')
2+
// require('tabe').createStream( test );
3+
var dop = require('./.proxy').create()
4+
5+
var object = dop.register({
6+
collector1: true,
7+
collector2: true,
8+
action1: true,
9+
action2: true
10+
})
11+
12+
function collector1() {
13+
var collector = dop.collect()
14+
dop.set(object, 'collector1', !object.collector1)
15+
collector.emit()
16+
}
17+
18+
function collector2() {
19+
var collector = dop.collect()
20+
collector1()
21+
dop.set(object, 'collector2', !object.collector2)
22+
collector.emit()
23+
}
24+
25+
var action1 = dop.action(function() {
26+
dop.set(object, 'action1', !object.action1)
27+
})
28+
29+
var action2 = dop.action(function() {
30+
action1()
31+
dop.set(object, 'action2', !object.action2)
32+
})
33+
34+
var allInOne = dop.action(function() {
35+
action1()
36+
action2()
37+
collector1()
38+
collector2()
39+
})
40+
41+
function allInOneCollector() {
42+
var collector = dop.collect()
43+
action1()
44+
action2()
45+
collector1()
46+
collector2()
47+
collector.emit()
48+
}
49+
50+
test('Two collectors', function(t) {
51+
var times = 0
52+
var observer = dop.createObserver(function(m) {
53+
t.equal(m.length, 2)
54+
times += 1
55+
})
56+
observer.observeObject(object)
57+
collector2()
58+
t.equal(times, 1)
59+
observer.destroy()
60+
t.end()
61+
})
62+
63+
test('Two actions', function(t) {
64+
var times = 0
65+
var observer = dop.createObserver(function(m) {
66+
t.equal(m.length, 2)
67+
times += 1
68+
})
69+
observer.observeObject(object)
70+
action2()
71+
t.equal(times, 1)
72+
observer.destroy()
73+
t.end()
74+
})
75+
76+
test('allInOne as action', function(t) {
77+
var times = 0
78+
var observer = dop.createObserver(function(m) {
79+
t.equal(m.length, 6)
80+
times += 1
81+
})
82+
observer.observeObject(object)
83+
allInOne()
84+
t.equal(times, 1)
85+
observer.destroy()
86+
t.end()
87+
})
88+
89+
test('allInOne as collector', function(t) {
90+
var times = 0
91+
var observer = dop.createObserver(function(m) {
92+
t.equal(m.length, 6)
93+
times += 1
94+
})
95+
observer.observeObject(object)
96+
allInOneCollector()
97+
t.equal(times, 1)
98+
observer.destroy()
99+
t.end()
100+
})

0 commit comments

Comments
 (0)