|
| 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