Code Monkey home page Code Monkey logo

yieldmachine's People

Contributors

royalicing 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yieldmachine's Issues

Add transform to xstate

Any machine can be imported into xstate. Which allows access to their visualisation, React hooks, and other tooling.

Compile Library away to imperative code

Before:

import { start } from "yieldmachine";

function TrafficLights() {
  function* Red() {
    yield on("timer", Green);
  }
  function* Green() {
    yield on("timer", Yellow);
  }
  function* Yellow() {
    yield on("timer", Red);
  }

  return Red;
}

const m = start(TrafficLights);
m.current; // { state: "Red", count: 0 }
m.next("timer");
m.current; // { state: "Green", count: 1 }
m.next("timer");
m.current; // { state: "Yellow", count: 2 }

After compiles to:

function* startTrafficLights() {
  let count = 0;
  let state = "Red";

  return {
    get current() {
      return Object.freeze({ count, state });
    }
    next(event) {
      if (event === "timer") {
        count++;
        if (state === "Red") {
          state = "Green";
        } else if (state === "Green") {
          state = "Yellow";
        } else if (state === "Yellow") {
          state = "Red";
        }
      }
      return { value: this.current, done: false };
    },
  };
}

const m = startTrafficLights();
m.current; // { state: "Red", count: 0 }
m.next("timer");
m.current; // { state: "Green", count: 1 }
m.next("timer");
m.current; // { state: "Yellow", count: 2 }

Support being reducer function passed to React’s `useReducer` accepting an event as action

function reducer(previousState: State, event: React.FormEvent<HTMLFormElement> | React.MouseEvent<HTMLButtonElement>): State {
  return previousState.machine.next(event);
}

eventsReducer(machine)

It wouldn’t support side effects and listening/emitting to events (e.g. with listenTo()).

import { eventsReducer } from 'yieldmachine';

function useMachine(machine) {
  const [state, dispatch] = useReducer(eventsReducer(machine));
  return [state, dispatch];
}

New description

Declare state machines using components.

YieldMachine is an opinionated library for creating state machines and actors. JavaScript Generator functions allow rich behaviour to be declared and composed together. And its modular design lets plugins add new behaviour.

Compile to WebAssembly .wasm

  • Parse mini modules format
  • Produce Wasm binary
  • Benefit: Able to be run anywhere: browsers, cloud, locally
  • Benefit: Sandboxed

Add online cloud service

Visualizer

Cloud rendered

  • Collected Press but for JavaScript modules.
  • Takes a .js file with a Generator Component from a Gist or GitHub repo, and allows running it online.
  • Allows passing the current state.

  • /viz/1/github/gist/RoyalIcing/e7d97cdf38a2907924ea12e4ebdf3c85 — See a visualizer for this Gist.
  • /execute/1/github/gist/RoyalIcing/e7d97cdf38a2907924ea12e4ebdf3c85 — Run the machine in this Gist.
  • /execute/1/github/gist/RoyalIcing/e7d97cdf38a2907924ea12e4ebdf3c85?current=loaded — Run the machine in this Gist from the loaded state.
  • /execute/1/github/gist/RoyalIcing/e7d97cdf38a2907924ea12e4ebdf3c85?current=loaded&events[]=click&events[]=click — Run the machine in this Gist from the loaded state and receive two click events.

jsDelivr has a beta service to bundle a ESM JS file from GitHub: jsdelivr/jsdelivr#18263

Support subscribing to an EventSource

const machine = start(Machine);
const eventSource = new EventSource();

machine.receiveFrom(eventSource);

const messagesKey = Symbol("messages");

function* Machine() {
  yield listenTo(eventSource, "open");
  yield on(new Map([["type", "error"], ["readyState", EventSource.CLOSED]]), Closed);

  function* Open() {
    yield listenTo(eventSource, "message");
    yield accumulate("message", messagesKey);
  }
  function* Closed() {}
  
  return function* Initial() {
    yield listenTo(eventSource, "open");
    yield on("open", Open);
  }
}

XState issue: managing data and context gets tricky

https://twitter.com/_jonesian/status/1440101111684165632

my biggest challenge is with data dependencies inside of context. like do i assume the data is there in the initial context or do i have an idle state in each of my machines with an initialize method to report the data.
it’s challenging with typescript when everything can possibly be undefined. then using those machines in react becomes challenging because you can’t pass undefined to a hook. so maybe i’m answering my question here haha

Support `formdata` event when FormData is created from form

See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/formdata_event

const formElem = document.querySelector('form');

// submit handler

formElem.addEventListener('submit', (e) => {
  // on form submission, prevent default
  e.preventDefault();

  console.log(form.querySelector('input[name="field1"]')); // FOO
  console.log(form.querySelector('input[name="field2"]')); // BAR

  // construct a FormData object, which fires the formdata event
  const formData = new FormData(formElem);
  // formdata gets modified by the formdata event
  console.log(formData.get('field1')); // foo
  console.log(formData.get('field2')); // bar
});

// formdata handler to retrieve data

formElem.addEventListener('formdata', (e) => {
  console.log('formdata fired');

  // modifies the form data
  const formData = e.formData; 
  // formdata gets modified by the formdata event
  formData.set('field1', formData.get('field1').toLowerCase());
  formData.set('field2', formData.get('field2').toLowerCase());
});

Add ARIA widget machine examples

  • Show the states that a widget can be in.
  • Support dynamic numbers of states e.g. multiple tabs
  • Model focus states. e.g. menu button should focus after closing.
  • Make available as a test suite package with Testing Library.

Add event listeners for IntersectionObserver, media queries, offline status, permission, etc

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.