Coder Social home page Coder Social logo

a-signal's Introduction

a-signal's People

Contributors

vovchisko avatar

Stargazers

 avatar

Watchers

 avatar  avatar

a-signal's Issues

Signal.all() - waiting for n-signals or one of signals

Let's say we have an entity with multiple signals: A, B, C. After init, we call connect and expect one of the signals fired. Once it fired - all other binds should be removed. (works only with .once())

The possible syntax could be:

const thing = {
   onA: new Sig(),
   onB: new Sig(),
   onC: new Sig(),
   // ...
}

const bindA = thing.onA.once(...)
const bindB = thing.onB.once(...)
const bindC = thing.onC.once(...)

bindA.onlyOne([bindB, bindC]) // now if any of this binds fires (A,B or C) all other binds are off.

Could be achieved with:

await Promise.all([
   thing.onA.wait(),
   thing.onB.wait(),
   thing.onC.wait(),
])

Or, It could be sugared with some helper:

import Signal from 'a-signal'

await Signal.all(thing.onA, thing.onB, thing.onC)
// or
await Signal.any(thing.onA, thing.onB, thing.onC)

Async "await once" sugar

More async sugar. 🍬

const sig = new Signal()

setTimeout(() => {
     sig.emit()
}, 1000)

// sweet way of .once()
await sig.wait()

await sig.wait(200) // will throw error
// or
await sig.wait(5000) // fits

Clear memory on memorable signals

Currently, there is no way to clear args/called from the last emit without accessing Signal.args and Signal.emited.

Let's add Signal.flush() to clear emited flag (and args).

More extracted methods

a.loaded = sig.getEmitter() 
a.whenLoaded = sig.getSubscriber() 
a.onceLoaded = sig.getSubscriberOnce() 

a.loaded(()=>console.log('loaded')) // the same as sig.on(<cb>)
a.emit() // the same as sig.emit()
e.onceLoaded()

TBD

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.