Skip to content

Commit

Permalink
chore: remove unused action
Browse files Browse the repository at this point in the history
  • Loading branch information
neolectron committed Jul 28, 2023
1 parent 642a23c commit e763e04
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 233 deletions.
3 changes: 2 additions & 1 deletion src/machines/bar.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const barMachine = hotspotMachine
actions: {
updatePersons: (context) => {
console.log(
'bar.updatePersons - updating persons by sending triggerDrink',
'bar.updatePersons - updating persons by sending triggerDrink to',
context.persons,
);
context.persons.forEach((p) => p.send('triggerDrink'));
return context;
Expand Down
19 changes: 0 additions & 19 deletions src/machines/hotspot.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ export const hotspotMachine = createMachine(
},
on: {
onRegisterPerson: {
// cond: (context, event) => {
// console.log(
// 'hotspot.onRegisterPerson - checking if the person is in the hotspot',
// );
// const hostpotIsFull = context.persons.length >= context.maxPersons;
// const isAlreadyInHotspot = Boolean(
// context.persons.find((p) => p.id === event.person.id),
// );
// return !isAlreadyInHotspot && !hostpotIsFull;
// },
actions: assign((context, event) => {
console.log(
'hotspot.onRegisterPerson - adding the person to the hotspot context',
Expand Down Expand Up @@ -97,15 +87,6 @@ export const hotspotMachine = createMachine(
},
{
actions: {
// test: assign((context, event) => {
// console.log(
// 'hotspot.onRegisterPerson - adding the person to the hotspot context',
// );
// return {
// ...context,
// persons: [...context.persons, event.person],
// };
// }),
updateHype: (context) => {
if (context.persons.length < 1) return context;
sendParent({
Expand Down
206 changes: 35 additions & 171 deletions src/machines/person.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ const METERS_CONFIG = {
maxValue: 100,
clamp: (v: number) => MathUtils.clamp(v, 0, METERS_CONFIG.pee.maxValue),
},
hype: {
initialValue: 50,
incrementValue: 10,
decrementValue: 10,
maxValue: 100,
clamp: (v: number) => MathUtils.clamp(v, 0, METERS_CONFIG.hype.maxValue),
},
};

// -----------------------------------------------------------------
Expand All @@ -44,12 +37,11 @@ export const personMachine = createMachine(
meters: {
thirst: METERS_CONFIG.thirst.initialValue,
pee: METERS_CONFIG.pee.initialValue,
hype: METERS_CONFIG.hype.initialValue,
},
action: 'none',
},
on: {
onUnregisterFromAllHotspot: {
target: 'actionFlow.Idle',
actions: assign((context, _, meta) => {
console.log('Person.onUnregisterFromAllHotspot');
sendParent({
Expand All @@ -64,97 +56,43 @@ export const personMachine = createMachine(
actionFlow: {
on: {
triggerPee: {
target: '#Person.actionFlow.Doing.Pissing',
target: '#Person.actionFlow.Pissing',
actions: assign((context) => {
return {
...context,
meters: {
...context.meters,
pee: METERS_CONFIG.pee.clamp(
context.meters.pee - METERS_CONFIG.pee.decrementValue,
),
},
};
}),
},
triggerDrink: {
target: '#Person.actionFlow.Doing.Drinking',
target: '#Person.actionFlow.Drinking',
actions: assign((context) => {
console.log('person is drinking');
return {
...context,
meters: {
...context.meters,
thirst: METERS_CONFIG.thirst.clamp(
context.meters.thirst - METERS_CONFIG.thirst.decrementValue,
),
pee: METERS_CONFIG.pee.clamp(
context.meters.pee + METERS_CONFIG.pee.incrementValue,
),
},
};
}),
},
},
initial: 'Idle',
states: {
Resting: {
initial: 'Idle',
states: {
Idle: {
after: {
'1000': [
{
target: '#Person.actionFlow.Resting.Idle',
actions: 'decreaseHype',
},
],
},
on: {
onDrag: {
target: 'Dragging',
},
},
},
Dragging: {
on: {
onDrop: [
{
target: '#Person.actionFlow.Doing.Drinking',
cond: 'isActionDrink',
},
{
target: '#Person.actionFlow.Doing.Pissing',
cond: 'isActionPiss',
},
'Idle',
],
},
},
},
},
Doing: {
states: {
Drinking: {
after: {
'1000': [
{
target: '#Person.actionFlow.Doing.Drinking',
cond: 'isThirsty',
actions: ['drink', 'increaseHype'],
},
{
target: '#Person.actionFlow.Blocking',
cond: 'isPeeFull',
actions: ['decreaseHype', 'emptyPee'],
},
],
},
},
Pissing: {
after: {
'1000': [
{
cond: 'canPee',
actions: 'pee',
},
{
target: '#Person.actionFlow.Resting',
cond: 'isPeeEmpty',
},
],
},
},
},
on: {
onDrag: {
target: '#Person.actionFlow.Resting.Dragging',
},
},
},
Blocking: {
initial: 'PissingHimself',
states: {
PissingHimself: {
always: {
target: '#Person.actionFlow.Resting',
},
},
},
},
Idle: {},
Pissing: {},
Drinking: {},
},
},
meterFlow: {
Expand All @@ -180,92 +118,26 @@ export const personMachine = createMachine(
},
schema: {
context: {} as {
action: 'none' | 'drink' | 'piss';
name: string;
meters: {
thirst: number;
hype: number;
pee: number;
};
name: string;
},
events: {} as
| { type: 'onDrag' }
| { type: 'onDrop'; action: 'drink' | 'piss' | 'none' }
| { type: 'triggerPee' }
| { type: 'triggerDrink' }
| { type: 'triggerStart' }
| { type: 'onUnregisterFromAllHotspot' }
| { type: 'onTick' },
actions: {} as
| { type: 'drink' }
| { type: 'pee' }
| { type: 'incrementHype' }
| { type: 'decrementHype' }
| { type: 'updateNeeds' },
actions: {} as { type: 'updateNeeds' },
},
predictableActionArguments: true,
preserveActionOrder: true,
tsTypes: {} as import('./person.machine.typegen').Typegen0,
},
{
actions: {
drink: assign((context) => {
console.log('person is drinking');
return {
...context,
meters: {
...context.meters,
thirst: METERS_CONFIG.thirst.clamp(
context.meters.thirst - METERS_CONFIG.thirst.decrementValue,
),
pee: METERS_CONFIG.pee.clamp(
context.meters.pee + METERS_CONFIG.pee.incrementValue,
),
},
};
}),
pee: assign((context) => {
return {
...context,
meters: {
...context.meters,
pee: METERS_CONFIG.pee.clamp(
context.meters.pee - METERS_CONFIG.pee.decrementValue,
),
},
};
}),
emptyPee: assign((context) => {
return {
...context,
meters: {
...context.meters,
pee: 0,
},
};
}),
increaseHype: assign((context) => {
return {
...context,
meters: {
...context.meters,
hype: METERS_CONFIG.hype.clamp(
context.meters.hype + METERS_CONFIG.hype.incrementValue,
),
},
};
}),
decreaseHype: assign((context) => {
return {
...context,
meters: {
...context.meters,
hype: METERS_CONFIG.hype.clamp(
context.meters.hype - METERS_CONFIG.hype.decrementValue,
),
},
};
}),
updateNeeds: assign((context) => {
return {
...context,
Expand All @@ -278,13 +150,5 @@ export const personMachine = createMachine(
};
}),
},
guards: {
isPeeEmpty: (context) => context.meters.pee <= 0,
isPeeFull: (context) => context.meters.pee >= 100,
isActionDrink: (_, event) => event.action === 'drink',
isActionPiss: (_, event) => event.action === 'piss',
canPee: (context) => context.meters.pee > 0,
isThirsty: (context) => context.meters.thirst > 0,
},
},
);
47 changes: 5 additions & 42 deletions src/machines/person.machine.typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
export interface Typegen0 {
'@@xstate/typegen': true;
internalEvents: {
'': { type: '' };
'xstate.after(1000)#Person.actionFlow.Doing.Drinking': {
type: 'xstate.after(1000)#Person.actionFlow.Doing.Drinking';
};
'xstate.after(1000)#Person.actionFlow.Doing.Pissing': {
type: 'xstate.after(1000)#Person.actionFlow.Doing.Pissing';
};
'xstate.after(1000)#Person.actionFlow.Resting.Idle': {
type: 'xstate.after(1000)#Person.actionFlow.Resting.Idle';
};
'xstate.after(500)#Person.meterFlow.Active': {
type: 'xstate.after(500)#Person.meterFlow.Active';
};
Expand All @@ -26,48 +16,21 @@ export interface Typegen0 {
services: never;
};
eventsCausingActions: {
decreaseHype:
| 'xstate.after(1000)#Person.actionFlow.Doing.Drinking'
| 'xstate.after(1000)#Person.actionFlow.Resting.Idle';
drink: 'xstate.after(1000)#Person.actionFlow.Doing.Drinking';
emptyPee: 'xstate.after(1000)#Person.actionFlow.Doing.Drinking';
increaseHype: 'xstate.after(1000)#Person.actionFlow.Doing.Drinking';
pee: 'xstate.after(1000)#Person.actionFlow.Doing.Pissing';
updateNeeds: 'xstate.after(500)#Person.meterFlow.Active';
};
eventsCausingDelays: {};
eventsCausingGuards: {
canPee: 'xstate.after(1000)#Person.actionFlow.Doing.Pissing';
isActionDrink: 'onDrop';
isActionPiss: 'onDrop';
isPeeEmpty: 'xstate.after(1000)#Person.actionFlow.Doing.Pissing';
isPeeFull: 'xstate.after(1000)#Person.actionFlow.Doing.Drinking';
isThirsty: 'xstate.after(1000)#Person.actionFlow.Doing.Drinking';
};
eventsCausingGuards: {};
eventsCausingServices: {};
matchesStates:
| 'actionFlow'
| 'actionFlow.Blocking'
| 'actionFlow.Blocking.PissingHimself'
| 'actionFlow.Doing'
| 'actionFlow.Doing.Drinking'
| 'actionFlow.Doing.Pissing'
| 'actionFlow.Resting'
| 'actionFlow.Resting.Dragging'
| 'actionFlow.Resting.Idle'
| 'actionFlow.Drinking'
| 'actionFlow.Idle'
| 'actionFlow.Pissing'
| 'meterFlow'
| 'meterFlow.Active'
| 'meterFlow.Inactive'
| {
actionFlow?:
| 'Blocking'
| 'Doing'
| 'Resting'
| {
Blocking?: 'PissingHimself';
Doing?: 'Drinking' | 'Pissing';
Resting?: 'Dragging' | 'Idle';
};
actionFlow?: 'Drinking' | 'Idle' | 'Pissing';
meterFlow?: 'Active' | 'Inactive';
};
tags: never;
Expand Down

0 comments on commit e763e04

Please sign in to comment.