首页 > 编程语言 >[Javascript] Create an Actor model in Javascript

[Javascript] Create an Actor model in Javascript

时间:2024-08-14 15:07:19浏览次数:11  
标签:Create Javascript actor events listener state model event currentState

Refer to: https://stately.ai/docs/actor-model

What defines an “actor”?

Actors are independent “live” objects that can communicate with each other via asynchronous message passing. In XState, we refer to these messages as events.

  • An actor has its own internal, encapsulated state that can only be updated by the actor itself. An actor may choose to update its internal state in response to a message it receives, but it cannot be updated by any other entity.
  • Actors communicate with other actors by sending and receiving events asynchronously.
  • Actors process one message at a time. They have an internal “mailbox” that acts like an event queue, processing events sequentially.
  • Internal actor state is not shared between actors. The only way for an actor to share any part of its internal state is by:
    • Sending events to other actors
    • Or emitting snapshots, which can be considered implicit events sent to subscribers.
  • Actors can create (spawn/invoke) new actors.

 

function counterBehavior(state, event) {
  if (event.type === "INCREMENT") {
    return {
      ...state,
      count: state.count + 1,
    };
  }

  return state;
}

function createActor(behavior, initialState) {
  let currentState = initialState;
  const listeners = new Set();
  function notifyListeners() {
    for (const listener of listeners) {
      listener(currentState);
    }
  }
  return {
    send: (event) => {
      currentState = behavior(currentState, event);
      notifyListeners();
    },
    subscribe: (listener) => {
      listeners.add(listener);
      listener(currentState);
    },
    getSnapshot: () => {
      return currentState;
    },
  };
}

const actor = createActor(counterBehavior, { count: 0 });

actor.subscribe((state) => {
  console.log("State is now", state);
});

actor.send({ type: "INCREMENT" });

 

标签:Create,Javascript,actor,events,listener,state,model,event,currentState
From: https://www.cnblogs.com/Answer1215/p/18359027

相关文章

  • [1041] JavaScript Tutorial
    ref:https://www.w3schools.com/js/default.aspJSTutorialJSHOMEJSIntroductionJSWhereToJSOutputJSStatementsJSSyntaxJSCommentsJSVariablesJSLetJSConstJSOperatorsJSArithmeticJSAssignmentJSDataTypesJSFunctionsJSObjectsJSObj......
  • 身份证实名认证类接口怎么选择?JavaScript身份证三要素核验接口返回参数说明
    当我们在选择身份证实名认证接口的时候,首先要考虑的是接口的稳定性和可靠性,翔云身份证实名认证接口,一般是指通过身份证三要素:身份证号、姓名、证件人像核验的方式来对身份证真伪的一致性进行核验,且接口的部署方式简单便捷。翔云身份证核验接口返回参数说明序号 名称 类......
  • 深入理解 JavaScript 闭包
    前言在JavaScript中,闭包(Closure)是一个非常强大且常见的概念,它使得函数可以访问其外部作用域中的变量,即使在该函数外部作用域已经执行完毕的情况下。闭包广泛应用于回调函数、事件处理器、模块化编程等多个场景。本文将详细探讨闭包的定义、工作原理、常见应用场景以及潜在的陷......
  • Vue3如何使用v-model写一个多条件联合搜索
    在Vue3中,使用v-model进行多条件联合搜索通常涉及到绑定多个输入字段到组件的数据属性上,并在搜索逻辑中根据这些属性的值来过滤数据。虽然v-model本身是针对单个表单元素进行双向数据绑定的,但你可以通过结合使用多个v-model和计算属性或方法来处理多条件搜索。以下是一个简单......
  • vue使用JavaScript运算符
    第一:加法运算符{{变量+n}}<p>num参与运算{{num+12}}</p>letvm=newVue({el:"#app",data:{num:101,isOK:true,message:'你......
  • JavaScript 中的宏任务与微任务
    JavaScript是一种单线程的编程语言,这意味着在同一时间只能执行一个任务。为了有效地处理并发操作,JavaScript引入了事件循环(EventLoop)机制,其中宏任务(MacroTask)和微任务(MicroTask)在其中扮演着关键角色。1.什么是宏任务和微任务?宏任务(MacroTask)是JavaScript中执行的大......
  • To create a new mock, the existing static mock registration must be deregistered
    1、异常提示:Tocreateanewmock,theexistingstaticmockregistrationmustbederegistered  2、原因分析由提示信息可知,静态模拟已经注册过了,再次注册时必须先将之前的撤销。所以我们要撤销之前的注册信息,再执行。这里提供另一种方法,将模拟静态的方法......
  • JavaScript函数
    定义函数形如functionabs(x){if(x>=0){returnx;}else{return-x;}}或varabs=function(x){if(x>=0){returnx;}else{return-x;}};如上所表达的函数为一个匿名函数,它没有函数名,该......
  • JavaScript高阶笔记总结(Xmind格式):第三天
    Xmind鸟瞰图:简单文字总结:js高阶笔记总结:严格模式:  1.开启严格模式:"usestrict"  2.不使用var关键字声明会报错  3.严格模式下普通函数的this指向undefined高阶函数:  满足其中之一即高阶函数:    1.函数作为参数    2.函数作为返回值......
  • JavaScript魔法:在线Excel附件上传与下载的完美解决方案
    最新技术资源(建议收藏)https://www.grapecity.com.cn/resources/前言在本地使用Excel时,经常会有需要在Excel中添加一些附件文件的需求,例如在Excel中附带一些Word,CAD图等等。同样的,类比到Web端,现在很多人用的在线Excel是否也可以像本地一样实现附件文件的操作呢?答案是肯定的,不......