From c9b25994cbc5381ecf25b7f6fe12431cabc4d1e6 Mon Sep 17 00:00:00 2001 From: Michael Lustig Date: Mon, 22 Apr 2024 11:39:24 -0400 Subject: [PATCH] Fix some typescript todos --- .../lib/features/counting/counting.machine.ts | 22 ++++++--------- .../reporting/permissionReporting.machine.ts | 28 +++++++++---------- 2 files changed, 22 insertions(+), 28 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 ad37a43..76a3cbb 100644 --- a/libs/permissions/permissionLogic/src/lib/features/counting/counting.machine.ts +++ b/libs/permissions/permissionLogic/src/lib/features/counting/counting.machine.ts @@ -1,5 +1,6 @@ import { assign, raise, sendTo, setup } from 'xstate'; import { + Permission, PermissionStatus, PermissionStatuses, Permissions, @@ -12,7 +13,13 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ }, types: { context: {} as { count: number; permissionStatus: PermissionStatus }, - events: { type: 'count.inc' }, + events: {} as + | { type: 'count.inc' } + | { type: 'permissionWasRequested'; permission: Permission } + | { + type: 'user.didTapBluetoothRequestPermission'; + permission: Permission; + }, }, }).createMachine({ /** @xstate-layout N4IgpgJg5mDOIC5QGMD2BXAdgFwJaagEFMIAFMAJwFtdZZdVNYA6NLPA5sTAQwCMANpADEbHM3zIA2gAYAuolAAHVPTyNFIAB6IA7ACYANCACeiAIwBWc8xl27AZhm6rDpw4C+H42I5ES5NS09IwsvvhQXLyCIr4SmNLmCkggKmoMmJo6CPoAHA7M1pYAbM4AnLoOLpZGpoj61sxllg7mVZbN5gYALLle3iCYqBBwmuEExGSUNHQZ8ClpuOqZKdkAtPrdzMWWuboyZeZlZd1lDsUOxmYIG1v2Mm0uus1l+vpePhg4EZOBMyFMVhfPyaRbLLL1bpXRAXZinY4nCoOMoPGTdYofEDjfxTIKzUJA9gRKL8IQQUGqJYZCEIcz6GyWXTdfQHBo7GrQnI1OEtR4daw9PoDbG-abBOaE76cCC0UmQCnpDSrRA1XLMLplYq5Rn6E7o4qchr6HmtGTWc7dbqtTEigJi-GAgAWPBIAgif3FoQVVKVoGy+gKDm6dJRuTslhkF10nIcuTVEZkbhkuU1Vt0uW6NuBPzteIBLGdrvd9vzEggQm94OVCHT23ZZv2lUeBrqCG6umKtjs+mKums7e6JUzwuzE1z-wlhfLxbzEp4yDwADcwJXqdWXMwgyHk+HI1VOS0tgm8marMdzEKvEA */ @@ -61,7 +68,6 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ 'user.didTapBluetoothRequestPermission': { actions: raise({ type: 'permissionWasRequested', - // @ts-expect-error TODO make this type safe permission: Permissions.bluetooth, }), }, @@ -73,7 +79,6 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ 'user.didTapBluetoothRequestPermission': { actions: raise({ type: 'permissionWasRequested', - // @ts-expect-error TODO make this type safe permission: Permissions.bluetooth, }), }, @@ -89,20 +94,9 @@ export const countingMachineThatNeedsPermissionAt3 = setup({ on: { permissionWasRequested: { actions: [ - // sendTo( - // ({ system }) => { - // return system.get(ActorSystemIds.permissionCheckerAndRequester); - // }, - // ({ event }) => ({ - // type: 'triggerPermissionRequest', - // // @ts-expect-error TODO make this type safe - // permission: event.permission, - // }) - // ), sendTo('permissionReportingCounting', ({ event }) => { return { type: 'requestPermission', - // @ts-expect-error TODO make this type safe permission: event.permission, }; }), diff --git a/libs/permissions/permissionLogic/src/lib/permission/reporting/permissionReporting.machine.ts b/libs/permissions/permissionLogic/src/lib/permission/reporting/permissionReporting.machine.ts index 404eaf9..155dcb1 100644 --- a/libs/permissions/permissionLogic/src/lib/permission/reporting/permissionReporting.machine.ts +++ b/libs/permissions/permissionLogic/src/lib/permission/reporting/permissionReporting.machine.ts @@ -7,10 +7,11 @@ import { sendTo, setup, } from 'xstate'; -import { Permission, Permissions } from '../../permission.types'; import { ActorSystemIds } from '../../application/actorIds'; +import { Permission } from '../../permission.types'; export const permissionReportingMachine = setup({ + // TODO: type these parents to ensure they accept the events we want to send them types: { input: {} as { permissions: Array; @@ -49,12 +50,14 @@ export const permissionReportingMachine = setup({ ), }, }).createMachine({ + /** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAYgCcwBHAVzgBcAFMM1XWWXAe3wG0AGALqJQAB04c6XfMJAAPRAFoATADYAnADoA7AFYALDoDMevgb4AOIwBoQAT0QBGPRrVqlai050O3Sh1oBfAJs0LDxCUhFmVnYpAGU6dDpqWABhbHR8GAh+ISQQMQkpGXkEJSUXfzUtByUdHTVzKq0bewQFB0MdDSM6hrUHCzcVJSCQjBwCYg0Ad3RcSSySWVhEujANdAAzdbJkPlJQyYjZ+cWoXJlCheL80r0Wu0RvbvNTFXMVTsMftRUg4IgfCcCBwGRHcLEK7iG7cEqKBytRCGPgqDQOHS6FTGHSWQYqHRjEAQqZEU43LLQopwu6OHRo6p8GqDQw1PTmJRIhBfQwaJlNFHmQzmcx6B4AgJAA */ description: "This actor's job is to report permission statuses to the actors that have invoked it. We abstract away this functionality so that it is reusable by any actor that needs it and so they don't need to know how permissions are checked. This keeps control centralized and easy to modify the behavior of.", context: ({ input }) => ({ permissions: input.permissions, parent: input.parent, }), + id: ActorSystemIds.permissionReporting, initial: 'waiting', states: { waiting: { @@ -69,12 +72,20 @@ export const permissionReportingMachine = setup({ }, on: { requestPermission: { + description: ` +This event is sent to the permission reporting machine from its parent feature machine. + +This will trigger the "real" check whose results will then be sent to the feature +machine. + `, actions: [ sendTo( ({ system }) => { return system.get(ActorSystemIds.permissionCheckerAndRequester); }, ({ event }) => ({ + // TODO: determine how to make this typesafe + // I'm thinking an api like sendToPermissionChecker(event: PermissionCheckerEvent) type: 'triggerPermissionRequest', permission: event.permission, }) @@ -84,7 +95,6 @@ export const permissionReportingMachine = setup({ permissionStatusChanged: { description: 'Whenever the Permission Monitoring machine reports that a permission status has changed, we receive this event and can process and share with our siblings.', - // We eventually want to communicate this to the actors that have invoked us actions: [ log( ({ event }) => @@ -104,18 +114,8 @@ export const permissionReportingMachine = setup({ type: 'checkedSendParent', params({ event }) { const { permission, status } = event; - if (permission === Permissions.bluetooth && status === 'granted') { - console.log('its granted yaya'); - return { - // TODO make these type safe - // dynamic - type: 'permission.granted.bluetooth', - }; - } else { - return { - type: 'permission.denied.bluetooth', - }; - } + const permissionEventType = `permission.${status}.${permission}`; + return { type: permissionEventType }; }, }, ],