[XState] Create Actor in Vanilla Javascript
JavaScript in create Actor XState
2023-09-14 08:59:11 时间
function countBehavior(state, event) {
if (event.type === "INC") {
return {
...state,
count: state.count + 1
}
}
}
function createActor(behavior, initialState) {
let currentState = initialState
const listeners = new Set()
return {
send: (event) => {
currentState = behavior(currentState, event)
listeners.forEach(listener => {
listener(currentState)
})
},
subscribe: (listener) => {
listeners.add(listener)
listener(currentState)
},
getSnapshot: () => {
return currentState
}
}
}
const actor = createActor(countBehavior, {count: 12})
actor.subscribe(console.log)
相关文章
- JavaScript中removeEventListener()使用注意事项
- JavaScript经典代码【一】【javascript HTML控件获取值】
- Xpath in JavaScript
- [Functional Programming] Create Reusable Functions with Partial Application in JavaScript
- [Functional Programming ADT] Debug a Functional JavaScript composeK Flow
- [Javascript] JavaScript赋值时的传值与传址
- [Javascript] Identify the most important words in a document using tf-idf in Natural
- [Javascript] Identify and Deal with NaN in JavaScript
- [Javascript] Other functor
- [Javascript] Proper use of console.assert in JavaScript
- [Javascript] Natively Format JavaScript Dates and Times
- [Javascript] Create an Async Generator and Loop Through Generated Promises with "For Await Of" Loops
- [Javascript] Understanding the difference between .prototype and .__proto__ in JavaScript
- [Javascript] Create Your First Iterator in JavaScript
- [ES2019] Represent Collision-free String Constants as Symbols in JavaScript
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
- [WASM Rust] Use the js-sys Crate to Invoke Global APIs Available in Any JavaScript Environment
- [Javascript] Use JavaScript's for-in Loop on Objects with Prototypes
- [Javascript] Decorators in JavaScript
- escape in ABAP and JavaScript
- 已解决FutureWarning: The default value of regex will change from True to False in a future version. In
- web前端Javascript开发学习之JavaScript中的预编译如何进行
- JavaScript的作用域