Skip to content

Commit

Permalink
initial fix for #10 pending further testing
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGAndrews committed Aug 31, 2021
1 parent 49ae4f2 commit 60a1098
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/xstate-fsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ const executeStateActions = (state, event) => state.actions.forEach(({ exec }) =
exports.interpret = function interpret(machine) {
let state = machine.initialState;
let status = InterpreterStatus.NotStarted;
const listeners = new Set();
// const listeners = new Set();
listeners = {};
const service = {
_machine: machine,
send: (event) => {
Expand All @@ -186,13 +187,19 @@ exports.interpret = function interpret(machine) {
}
state = machine.transition(state, event);
executeStateActions(state, toEventObject(event));
listeners.forEach((listener) => listener(state));
// listeners.forEach((listener) => listener(state));
const keys = Object.keys(listeners);
for (const key of keys) {
listeners[key](state);
};
},
subscribe: (listener) => {
listeners.add(listener);
// listeners.add(listener);
listeners[listener] = listener
listener(state);
return {
unsubscribe: () => listeners.delete(listener)
// unsubscribe: () => listeners.delete(listener)
unsubscribe: () => delete listeners(listener)
};
},
start: (initialState) => {
Expand All @@ -218,7 +225,8 @@ exports.interpret = function interpret(machine) {
},
stop: () => {
status = InterpreterStatus.Stopped;
listeners.clear();
// listeners.clear();
listeners = {};
return service;
},
get state() {
Expand Down
22 changes: 22 additions & 0 deletions test/Node/test_basicInterpreter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { createMachine, assign, interpret } = require ('../../src/xstate-fsm')

const toggleMachine = createMachine({
id: 'toggle',
initial: 'inactive',
states: {
inactive: { on: { TOGGLE: 'active' } },
active: { on: { TOGGLE: 'inactive' } }
}
});

console.log(toggleMachine);

const toggleService = interpret(toggleMachine).start();

toggleService.subscribe((state) => {
console.log(state.value);
});

toggleService.send('TOGGLE');
toggleService.send('TOGGLE');
toggleService.stop();

0 comments on commit 60a1098

Please sign in to comment.