import Actor from "actor-model";
const counter = {
init() {
return { count: 0 };
},
incrementBy(state, { number }) {
let count = state.count + number;
return { count };
},
logTotal(state) {
console.log(state.count);
}
};
const address = Actor.start(counter);
Actor.send(address, ["logTotal"]); // => { count: 0 }
Actor.send(address, ["incrementBy", { number: 2 }]);
Actor.send(address, ["logTotal"]); // => { count: 2 }
const mailbox = new EventEmitter();
const Actor = {
start() {
const address = Symbol();
// Use the address as the event name
mailbox.on(address, function() {});
return address;
}
};
import EventEmitter from "events";
const mailbox = new EventEmitter();
const Actor = {
start(behavior) {
const address = Symbol();
let state = typeof behavior.init === "function" ? behavior.init() : {};
mailbox.on(address, function([method, message]) {
state = behavior[method](state, message) || state;
});
return address;
},
send(target, message) {
mailbox.emit(target, message);
}
};
export default Actor;