From 8504178117e234d1ccdcd5291b2dd6dd5c88e52e Mon Sep 17 00:00:00 2001 From: Michael Lustig Date: Mon, 22 Apr 2024 12:52:15 -0400 Subject: [PATCH] move guards and actions out of inline --- .../lib/features/counting/counting.machine.ts | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/libs/permissions/permissionLogic/src/lib/features/counting/counting.machine.ts b/libs/permissions/permissionLogic/src/lib/features/counting/counting.machine.ts index 76a3cbb..ed61ccc 100644 --- a/libs/permissions/permissionLogic/src/lib/features/counting/counting.machine.ts +++ b/libs/permissions/permissionLogic/src/lib/features/counting/counting.machine.ts @@ -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 @@ -22,7 +39,7 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ }, }, }).createMachine({ - /** @xstate-layout N4IgpgJg5mDOIC5QGMD2BXAdgFwJaagEFMIAFMAJwFtdZZdVNYA6NLPA5sTAQwCMANpADEbHM3zIA2gAYAuolAAHVPTyNFIAB6IA7ACYANCACeiAIwBWc8xl27AZhm6rDpw4C+H42I5ES5NS09IwsvvhQXLyCIr4SmNLmCkggKmoMmJo6CPoAHA7M1pYAbM4AnLoOLpZGpoj61sxllg7mVZbN5gYALLle3iCYqBBwmuEExGSUNHQZ8ClpuOqZKdkAtPrdzMWWuboyZeZlZd1lDsUOxmYIG1v2Mm0uus1l+vpePhg4EZOBMyFMVhfPyaRbLLL1bpXRAXZinY4nCoOMoPGTdYofEDjfxT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KloliEhoGxmBmyq+FRkRdPg5AQHASiTvE8LIUiqHqJY-gGMYpgWNYtgOARjRqBeejKjoD7aL46aeCmn5Unc2r0KxwbsSonHNDxJhmFYNh2I4MaaDJJBGEmSbdlhDgKbcWq0skozMqpiL5BxCCaERpRGAcMoOL4F7+L4MaJgYJwWNUxymMRMk2bm046iaTLjM5rZucIMb6NYBj+GZlijhm47ql+Sn2QW9ZFguZYQClKEaQg6UKichIOFo1TNJs76xVOyn0gAZgQzzYJANXqYUDWNOYrUGD2ZwaMcOjCBeXV3D++B-gBh7+vAwpsa5dWaJU3jYoY5TmGU6zCYg-jxqYSpJnYQTeRoNHhEAA */ type: 'parallel', context: { count: 0, @@ -37,21 +54,17 @@ 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: { @@ -59,17 +72,11 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ 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', }, }, }, @@ -77,10 +84,7 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ on: { 'permission.granted.bluetooth': { target: 'enabled' }, 'user.didTapBluetoothRequestPermission': { - actions: raise({ - type: 'permissionWasRequested', - permission: Permissions.bluetooth, - }), + actions: 'triggerBluetoothPermissionRequest', }, }, },