Skip to content

Commit 3501ff2

Browse files
committed
Add publisher.clear() to clear the subscribers. Remove some duplicated codes
1 parent 6ebae7f commit 3501ff2

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

monadio.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,16 @@ class MonadIODef {
99
return fn(self.effect());
1010
});
1111
}
12-
bind(fn) {
13-
return this.then(fn);
14-
}
15-
map(fn) {
16-
return this.then(fn);
17-
}
1812
flatMap(fn) {
1913
return this.then((result)=>fn(result).effect());
2014
}
2115
of(ref) {
2216
var m = new MonadIODef(()=>ref);
2317
return m;
2418
}
25-
just(ref) {
26-
return this.of(ref);
27-
}
2819
ap(fnM) {
2920
return fnM.chain(f => this.map(f));
3021
}
31-
chain(fn) {
32-
return this.flatMap(fn);
33-
}
3422

3523
subscribe(fn, asynchronized) {
3624
if (asynchronized) {
@@ -41,6 +29,10 @@ class MonadIODef {
4129
return fn;
4230
}
4331
}
32+
MonadIODef.prototype.just = MonadIODef.prototype.of
33+
MonadIODef.prototype.chain = MonadIODef.prototype.flatMap
34+
MonadIODef.prototype.bind = MonadIODef.prototype.then
35+
MonadIODef.prototype.map = MonadIODef.prototype.then
4436

4537
var MonadIO = new MonadIODef({});
4638
MonadIO.fromPromise = function (p) {

publisher.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class Publisher {
2323
unsubscribe(fn) {
2424
this.subscribers = this.subscribers.filter((item)=>item!==fn);
2525
}
26+
clear() {
27+
this.subscribers = []
28+
}
2629
publish(result,asynchronized) {
2730
this.subscribers.forEach((fn)=>asynchronized ? Promise.resolve(result).then(fn) : fn(result));
2831
}

test/publisher.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ describe('Publisher', function () {
77

88
v = 0;
99
p.subscribe((i)=>v=i);
10-
p.publish(1);
1110

11+
p.publish(1);
12+
v.should.equal(1)
13+
// Clear
14+
p.clear()
15+
p.publish(2);
1216
v.should.equal(1)
1317
});
1418
it('async', function (done) {

0 commit comments

Comments
 (0)