Skip to content

Commit

Permalink
invoke dispatcher instead
Browse files Browse the repository at this point in the history
  • Loading branch information
customcommander committed Oct 6, 2024
1 parent 40506cc commit e2728eb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 57 deletions.
38 changes: 13 additions & 25 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,39 @@ import {
} from 'xstate';

const src = setup({
context: {
jobs: []
},
actions: {
start:
assign(({event: {jobs}}) => ({jobs})),

dispatch:
enqueueActions(({enqueue, context, system}) => {
dispatch: enqueueActions(({enqueue, context, system}) => {
const [{ev: type, task_id}, ...jobs] = context.jobs;
enqueue.sendTo(system.get(`task-${task_id}`), {type});
enqueue.assign({jobs});
}),

quit:
sendTo(({system}) => system.get('gamesys'), {
type: 'dispatch.done'
})
},

guards: {
'repeat?':
({context: {jobs}}) => jobs.length > 0
'repeat?': ({context}) => {
const {jobs} = context;
return jobs.length > 0
}
}
});

export default src.createMachine({
initial: 'idle',
context: ({input}) => ({
jobs: input.jobs
}),
initial: 'loop',
states: {
idle: {
on: {
dispatch: {
target: 'loop',
actions: 'start'
}
}
},
loop: {
entry: 'dispatch',
on: {
'task.ack': [
{target: 'loop', guard: 'repeat?', reenter: true},
{target: 'idle', actions: 'quit'}
{target: 'done'}
]
}
},
done: {
type: 'final'
}
}
});
64 changes: 33 additions & 31 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ const src = setup({
},

actions: {
'init-dispatcher':
spawnChild('dispatcher', {systemId: 'dispatcher'}),

'setup-new-turn':
assign(({context}) => produce(context, draft => {
draft.turn += 1;
Expand Down Expand Up @@ -135,13 +132,6 @@ const src = setup({
}
});

const dispatcher_params =
(channel) => ({context}) => [channel].flatMap(ch => {
const task_not_hidden = id => context.tasks[id].hidden !== true;
const ids = context.__dispatch[ch].filter(task_not_hidden);
return ids.map((id) => ({ev: `task.${ch}`, task_id: id}));
});

const machine = src.createMachine({
context: ({input}) => {
const turn = input?.turn ?? 1;
Expand Down Expand Up @@ -223,18 +213,15 @@ const machine = src.createMachine({
error: null,
early_exit: null,

__dispatch: {
replenish: ['107','108','109','110','114'],
'harvest-fields': ['001']
}
on_replenish: ['107','108','109','110','114'],
on_fields: ['001']
};
},
"initial": "init",
"states": {
"init": {
"entry": "init-dispatcher",
"invoke": {
"src": "task-loader",
invoke: {
src: 'task-loader',
input: ({context}) => Object.keys(context.tasks),
onDone: {
target: 'work',
Expand All @@ -252,15 +239,27 @@ const machine = src.createMachine({
}
},
replenish: {
entry: {
type: 'dispatch',
params: dispatcher_params('replenish')
},
on: {
'dispatch.done': {
invoke: {
src: 'dispatcher',
systemId: 'dispatcher',
input: ({context}) => {
const {on_replenish: notify, tasks} = context;

const jobs = notify.reduce((acc, task_id) => {
const available = tasks[task_id].hidden !== true;
// Ignore tasks not yet available
if (available) {
acc.push({task_id, ev: 'task.replenish'});
}
return acc;
}, []);

return {jobs};
},
onDone: {
target: 'done'
}
},
}
},
done: {
type: 'final'
Expand Down Expand Up @@ -320,15 +319,18 @@ const machine = src.createMachine({
"initial": "fields",
"states": {
"fields": {
entry: {
type: 'dispatch',
params: dispatcher_params('harvest-fields')
},
on: {
'dispatch.done': {
invoke: {
src: 'dispatcher',
systemId: 'dispatcher',
input: ({context}) => {
const {on_fields: notify} = context;
const jobs = notify.map(task_id => ({task_id, ev: 'task.fields'}));
return {jobs};
},
onDone: {
target: 'feed'
}
}
},
},
"feed": {
"after": {
Expand Down
2 changes: 1 addition & 1 deletion src/task-001.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function harvest_fields(_, game_context) {
export default base.createMachine({
context: {},
on: {
'task.harvest-fields': {
'task.fields': {
actions: {
type: 'game-update',
params: {
Expand Down

0 comments on commit e2728eb

Please sign in to comment.