GithubHelp home page GithubHelp logo

yuriyyakym / awai Goto Github PK

View Code? Open in Web Editor NEW
26.0 1.0 1.0 1016 KB

Promised state management library with a unique events system, where every event is thennable, which allows to write logic in a saga-like way using async functions

Home Page: https://awai.js.org

License: MIT License

JavaScript 0.95% TypeScript 99.05%
atomic management react state state-management effects race-conditions

awai's People

Contributors

yuriyyakym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

awai's Issues

Improve scenario `repeatUntil` configuration

Get rid of repeatUntil config option and add another scenario override instead:

scenario(trigger, repeatUntil, callback, config);

So that instead of

scenario(
  draw.events.invoked,
  ({ arguments: [endPoint] }) => {
    currentLayerState.set((layer) => ({ ...layer, endPoint }));
  },
  { repeatUntil: stopDrawing.events.invoked },
);

Developer can write

scenario(
  draw.events.invoked,
  stopDrawing.events.invoked,
  ({ arguments: [endPoint] }) => {
    currentLayerState.set((layer) => ({ ...layer, endPoint }));
  },
);

repeat config option should remain as is.

Make Scenario thennable

In order to improve code readability in quite common cases, it was decided to make scenario thennable, so that instead of assigning scenario to a variable and then awaiting a final event, like this:

  const drawingScenario = scenario(
    draw.events.invoked,
    stopDrawing.events.invoked,
    ({ arguments: [endPoint] }) => {
      currentLineLayerState.set((layer) => ({ ...layer, endPoint }));
    },
  );

  await drawingScenario.events.expired;

it should be possible to do it like this:

  await scenario(
    draw.events.invoked,
    stopDrawing.events.invoked,
    ({ arguments: [endPoint] }) => {
      currentLineLayerState.set((layer) => ({ ...layer, endPoint }));
    },
  );

It should also warn a user if he is trying to await and infinite scenario.

Update async events

Introduce fulfilled event, which is only emitted on success.
changed event should be emitted always, right after fullfilled and rejected events.

Enhance scenario interface

It may handle action and readable states.

Current approach:

const items = state();
const createItem = action();

scenario(items.events.changed, () => { /* ... */ });
scenario(createItem.events.invoked, () => { /* ... */ });

Suggested syntax sugar, which would by default take changed event from states, and invoked event from action.

scenario(items, () => { /* ... */ });
scenario(createItem, () => { /* ... */ });

Enhance scenario trigger interface

Right now the only way to trigger a scenario with multiple AwaiEvents's/promises is to have a trigger callback and use Promise.race, like so:

scenario(
  () => Promise.race([
    action1.events.invoked,
    delay(1000),
  ]),
  () => { /* ... */ }
);

It should also be possible to pass an array or AwaiEvents:

scenario(
  [action1.events.invoked, state1.events.changed],
  () => { /* ... */ }
);

Create registry

Registry should store references to all nodes.
Registry should have events.registered event.

Scenario should be stoppable

It will allow to nicely compose scenarios, like in this example for rect drawing scenario:

scenario(startDraw.events.invoked, async ({ arguments: [event]}) => {
  const tool = toolState.get();

  if (tool !== 'rectangle') {
    return;
  }

  currentLayerState.set({
    type: 'rectangle',
    start: getEventPoint(event),
    end: getEventPoint(event)
  });

  const drawScenario = scenario(draw.events.invoked, ({ arguments: [event]}) => {
    currentLayerState.set(layer => ({
      ...layer!,
      end: getEventPoint(event)
    }));
  });

  await endDraw.events.invoked;

  // drawScenario.stop();
});

Notice that after inner scenario is disposed, we can safely handle next drawings.

Add `ignored` event to AsyncState

AsyncState should emit ignored event when outdated promise is resolved. Event should include ignored version number and resolved value.

Enhance scenario

At the moment scenario is just a syntax sugar for an infinite loop. Also there are variations: scenarioOnce and scenarioOnEvery.

There should be only one scenario with configuration support.

Refactor `then` methods in every Awai node

When no callback is passed to then method, it should behave like native Promise - unmodified value.

eg. state.events.changed.then() should return the same promise as state.events.changed.

Marketing

  • DevHunt
  • HN
  • LinkedIn blog post
  • StackOverflow article
  • Dev.to post
  • GitHub Discussions

Support `repeat` and `repeatUntil` scenario config attributes

Extend scenario config with the following attributes:

{
  repeat?: number;
  repeatUntil?: PromiseLike | AwaiEvent | PredicateCallback;
}
  • repeat - number of times scenario should run before expired event is emited.
  • repeatUntil
    • If PromiseLike - repeat unitl resolved
    • If AwaiEvent - repeat until emited
    • If predicate - repeat until returns truthy

When both repeat and repeatUntil are specified, scenario should be expired as soon as any of them are supposed to expire the scenario.

expired event should only be emitted when last scenario callback is finished evaluating.


Get rid of current stop functionality.

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.