zoukankan      html  css  js  c++  java
  • [XState] Actor Model

    Actor, which can invoke a promise to fetch data.

    import { createMachine, assign, interpret } from "xstate";
    
    const elBox = document.querySelector("#box");
    
    const randomFetch = () => {
      return new Promise((res, rej) => {
        setTimeout(() => {
          if (Math.random() < 0.5) {
            rej("Fetch failed!");
          } else {
            res("Fetch succeeded!");
          }
        }, 2000);
      });
    };
    
    const machine = createMachine({
      initial: "idle",
      states: {
        idle: {
          on: {
            FETCH: "pending",
          },
        },
        pending: {
          on: {
            RESOLVE: "resolved",
            // cancel the promise
            CANCEL: "idle",
          },
          invoke: {
            // Invoke your promise here.
            // The `src` should be a function that returns the source.
            src: (context, event) => {
              return randomFetch();
            },
            onDone: {
              target: "resolved",
            },
            onError: {
              target: "rejected",
            },
          },
        },
        resolved: {
          // Add a transition to fetch again
          on: {
            FETCH: "pending",
          },
        },
        rejected: {
          // After 2s, retry again
          after: {
            2000: {
              target: "pending",
            },
          },
        },
      },
    });
    
    const service = interpret(machine);
    
    service.onTransition((state) => {
      elBox.dataset.state = state.toStrings().join(" ");
    
      console.log(state);
    });
    
    service.start();
    
    elBox.addEventListener("click", (event) => {
      service.send("FETCH");
    });
    
    const cancelBtn = document.querySelector("#cancel");
    cancelBtn.addEventListener("click", (event) => {
      service.send("CANCEL");
    });
  • 相关阅读:
    操作技巧——电脑远程控制
    软件安装——internal error2503/2502
    系统常识——虚拟内存地址
    操作技巧——保障无线上网的技巧
    操作技巧——输入法转换问题
    软件安装——彻底卸载MySQL
    Java——this
    python百度贴吧爬虫
    糗事百科python爬虫
    python请求带cookie
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13470159.html
Copyright © 2011-2022 走看看