Skip to content

Commit

Permalink
move guards and actions out of inline
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 22, 2024
1 parent c9b2599 commit 8504178
Showing 1 changed file with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
actors: {
permissionReportingMachine,
},

actions: {
incrementCount: assign({ count: ({ context }) => context.count + 1 }),
assignBluetoothStatusGranted: assign({ permissionStatus: 'granted' }),
triggerBluetoothPermissionRequest: raise({
type: 'permissionWasRequested',
permission: Permissions.bluetooth,
}),
},

guards: {
requiresPermission: ({ context }) =>
context.count >= 3 &&
context.permissionStatus !== PermissionStatuses.granted,
countingCompleted: ({ context }) => context.count >= 5,
},

types: {
context: {} as { count: number; permissionStatus: PermissionStatus },
events: {} as
Expand All @@ -22,7 +39,7 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
},
},
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5QGMD2BXAdgFwJaagEFMIAFMAJwFtdZZdVNYA6NLPA5sTAQwCMANpADEbHM3zIA2gAYAuolAAHVPTyNFIAB6IA7ACYANCACeiAIwBWc8xl27AZhm6rDpw4C+H42I5ES5NS09IwsvvhQXLyCIr4SmNLmCkggKmoMmJo6CPoAHA7M1pYAbM4AnLoOLpZGpoj61sxllg7mVZbN5gYALLle3iCYqBBwmuEExGSUNHQZ8ClpuOqZKdkAtPrdzMWWuboyZeZlZd1lDsUOxmYIG1v2Mm0uus1l+vpePhg4EZOBMyFMVhfPyaRbLLL1bpXRAXZinY4nCoOMoPGTdYofEDjfxTIKzUJA9gRKL8IQQUGqJYZCEIcz6GyWXTdfQHBo7GrQnI1OEtR4daw9PoDbG-abBOaE76cCC0UmQCnpDSrRA1XLMLplYq5Rn6E7o4qchr6HmtGTWc7dbqtTEigJi-GAgAWPBIAgif3FoQVVKVoGy+gKDm6dJRuTslhkF10nIcuTVEZkbhkuU1Vt0uW6NuBPzteIBLGdrvd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lijhm47ql+Sn2QW9ZFguZYQClKEaQg6UKichIOFo1TNJs76xVOyn0gAZgQzzYJANXqYUDWNOYrUGD2ZwaMcOjCBeXV3D++B-gBh7+vAwpsa5dWaJU3jYoY5TmGU6zCYg-jxqYSpJnYQTeRoNHhEAA */
type: 'parallel',
context: {
count: 0,
Expand All @@ -37,50 +54,37 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
always: [
{
target: 'disabled',
guard: ({ context }) =>
context.count >= 3 &&
context.permissionStatus !== PermissionStatuses.granted,
guard: 'requiresPermission',
},
{
target: 'finished',
guard: ({ context }) => context.count >= 5,
guard: 'countingCompleted',
},
],
on: {
'count.inc': [
{
actions: assign({ count: ({ context }) => context.count + 1 }),
},
],
'count.inc': {
actions: 'incrementCount',
},
},
},
disabled: {
id: 'countingDisabled',
on: {
'permission.granted.bluetooth': {
target: 'enabled',
actions: [
() => console.log('permission granted'),
assign({ permissionStatus: PermissionStatuses.granted }),
],
actions: 'assignBluetoothStatusGranted',
},
'permission.denied.bluetooth': { target: 'bluetoothDenied' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
permission: Permissions.bluetooth,
}),
actions: 'triggerBluetoothPermissionRequest',
},
},
},
bluetoothDenied: {
on: {
'permission.granted.bluetooth': { target: 'enabled' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
permission: Permissions.bluetooth,
}),
actions: 'triggerBluetoothPermissionRequest',
},
},
},
Expand Down

0 comments on commit 8504178

Please sign in to comment.