eager subscribable property (no bug)

node v12.22.12
version: 2.0.0
endpointsharetweet
const { EventEmitter } = require("events") const { Operation, spawn, timeout } = require("effection"); const { on } = require("@effection/events") const { Subscribable } = require('@effection/subscription'); const { main } = require("@effection/node") class Atom { constructor(state) { this.subscriptions = new EventEmitter(); this.state = state; // this.states = Subscribable.from(on(this.subscriptions, 'state')) // .map(([state]) => state); } get states() { return Subscribable.from(on(this.subscriptions, 'state')) .map(([state]) => state) } update(fn) { this.state = fn(this.state); this.subscriptions.emit("state", this.state); } once(predicate) { return this.states.filter(predicate).first(); } } main(function*() { let atom = new Atom(0); yield spawn(function*() { while (true) { yield timeout(1000); atom.update(count => count + 1) } }); yield spawn(function*() { yield atom.states.forEach(function*(count) { console.log('count is ' + count); }) }) yield atom.once(count => count >= 5); });"";
Loading…

no comments

    sign in to comment