Created
June 12, 2020 18:25
-
-
Save cowboyd/33a43bb5c7a0c2ce16276611b914cfc5 to your computer and use it in GitHub Desktop.
Demonstration of `@effection/subscription` bug wherein subscribables stored as instance properties don't work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter } from "events"; | |
import { Operation } from "effection"; | |
import { on } from '@effection/events'; | |
import { Subscribable } from '@effection/subscription'; | |
export class Atom<S> { | |
subscriptions = new EventEmitter(); | |
// get states() { | |
// return Subscribable.from(on(this.subscriptions, 'state')) | |
// .map(([state]) => state as S) | |
// } | |
states = Subscribable.from(on(this.subscriptions, 'state')) | |
.map(([state]) => state as S); | |
constructor(public state: S) {} | |
update(fn: (state: S) => S) { | |
this.state = fn(this.state); | |
this.subscriptions.emit("state", this.state); | |
} | |
once(predicate: (state: S) => boolean): Operation<S | undefined> { | |
return this.states.filter(predicate).first(); | |
} | |
} | |
import { spawn, timeout } from 'effection'; | |
import { main } from '@effection/node'; // | |
main(function*(): Operation<void> { | |
let atom = new Atom(0); | |
yield spawn(function*(): Operation<void> { | |
while (true) { | |
yield timeout(1000); | |
atom.update(count => count + 1) | |
} | |
}); | |
yield spawn(function*(): Operation<void> { | |
yield atom.states.forEach(function*(count) { | |
console.log('count is', count); | |
}) | |
}) | |
yield atom.once(count => count >= 5); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment