From 2afd3531bca883eeda0446589dfbc1118e500517 Mon Sep 17 00:00:00 2001 From: Michael Lustig Date: Thu, 4 Apr 2024 16:58:49 -0400 Subject: [PATCH] okay we are CCOOOOOKING --- .../src/lib/permission-logic.spec.ts | 92 +++++++++++++++++-- .../src/lib/permissionMonitor.machine.ts | 8 +- 2 files changed, 90 insertions(+), 10 deletions(-) diff --git a/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts b/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts index e0ba398..9030f08 100644 --- a/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts +++ b/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts @@ -29,6 +29,7 @@ import { permissionMonitoringMachine, } from './permissionMonitor.machine'; +const forever = 2 ^ (28 - 1); const countingMachineThatNeedsPermissionAt3 = setup({ types: { context: {} as { count: number; permissionStatus: PermissionStatus }, @@ -396,6 +397,19 @@ describe('Permission Monitoring Machine', () => { log('subscribe to status updates'), ], on: { + requestPermission: { + actions: [ + sendTo( + ({ system }) => { + return system.get(ActorSystemIds.permissionCheckerAndRequester); + }, + ({ event }) => ({ + type: 'triggerPermissionRequest', + permission: event.permission, + }) + ), + ], + }, 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.', @@ -426,11 +440,11 @@ describe('Permission Monitoring Machine', () => { console.log('its granted yaya'); return { // dynamic - type: 'permission.bluetooth.granted', + type: 'permission.granted.bluetooth', }; } else { return { - type: 'permission.bluetooth.denied', + type: 'permission.denied.bluetooth', }; } }, @@ -448,6 +462,17 @@ describe('Permission Monitoring Machine', () => { id: 'someFeatureMachineId', type: 'parallel', states: { + logging: { + on: { + '*': { + actions: [ + ({ event }) => { + console.log('logging::::' + JSON.stringify(event, null, 2)); + }, + ], + }, + }, + }, foo: { initial: 'start', states: { @@ -459,17 +484,43 @@ describe('Permission Monitoring Machine', () => { on: { 'permission.granted.bluetooth': { target: 'bluetoothGranted' }, 'permission.denied.bluetooth': { target: 'bluetoothDenied' }, + 'user.didTapBluetoothRequestPermission': { + actions: raise({ + type: 'permissionWasRequested', + permission: Permissions.bluetooth, + }), + }, }, }, bluetoothGranted: { type: 'final', }, bluetoothDenied: { - type: 'final', + on: { + 'permission.granted.bluetooth': { target: 'bluetoothGranted' }, + 'user.didTapBluetoothRequestPermission': { + actions: raise({ + type: 'permissionWasRequested', + permission: Permissions.bluetooth, + }), + }, + }, }, }, }, handlingPermissions: { + on: { + permissionWasRequested: { + actions: [ + sendTo('permissionHandler', ({ event }) => { + return { + type: 'requestPermission', + permission: event.permission, + }; + }), + ], + }, + }, invoke: { id: 'permissionHandler', src: 'permissionReportingMachine', @@ -531,13 +582,9 @@ describe('Permission Monitoring Machine', () => { expect(featureMachineActor?.getSnapshot().value).toStrictEqual({ foo: 'waitingForPermission', handlingPermissions: {}, + logging: {}, }); - // featureMachineActor?.send({ - // type: 'sendPermissionRequest', - // permission: Permissions.bluetooth, - // }); - expect(permissionMonitorActor.getSnapshot().value).toStrictEqual({ applicationLifecycle: 'applicationIsInForeground', permissions: {}, @@ -561,12 +608,41 @@ describe('Permission Monitoring Machine', () => { await waitFor(permissionCheckerActor, (state) => { return state.value === 'idle'; }); + expect( + permissionMonitorActor.getSnapshot().context.permissionsStatuses[ + Permissions.bluetooth + ] + ).toBe('denied'); expect(permissionCheckerActor?.getSnapshot().value).toBe('idle'); expect(featureMachineActor?.getSnapshot().value).toStrictEqual({ foo: 'bluetoothDenied', handlingPermissions: {}, + logging: {}, + }); + + // await waitFor(featureMachineActor, (state) => { + // return state.value === 'bluetoothDenied'; + // }); + + featureMachineActor?.send({ + type: 'user.didTapBluetoothRequestPermission', + permission: Permissions.bluetooth, + }); + + expect(permissionCheckerActor?.getSnapshot().value).toBe( + 'requestingPermission' + ); + + await waitFor(permissionCheckerActor, (state) => { + return state.value === 'idle'; + }); + expect(featureMachineActor?.getSnapshot().value).toStrictEqual({ + foo: 'bluetoothGranted', + handlingPermissions: {}, + logging: {}, }); + // await new Promise((resolve) => setTimeout(resolve, forever)); }); describe('Edge Cases', () => { diff --git a/libs/permissions/permissionLogic/src/lib/permissionMonitor.machine.ts b/libs/permissions/permissionLogic/src/lib/permissionMonitor.machine.ts index 1fbc3ea..a232467 100644 --- a/libs/permissions/permissionLogic/src/lib/permissionMonitor.machine.ts +++ b/libs/permissions/permissionLogic/src/lib/permissionMonitor.machine.ts @@ -58,6 +58,8 @@ export const permissionMonitoringMachine = setup({ }), broadcastPermissionsToListeners: enqueueActions( ({ context, event, enqueue }) => { + // TODO this should only send permission updates for the recently modified permissions + // and is currently sending updates to all permissions to everyone Object.keys(context.permissionSubscribers).forEach((permission) => { context.permissionSubscribers[permission].forEach( (actorRef: AnyActorRef) => { @@ -194,8 +196,10 @@ export const permissionMonitoringMachine = setup({ ], }, permissionRequestCompleted: { - actions: 'assignPermissionRequestResultToContext', - // TODO 'broadcastPermissionsToListeners', + actions: [ + 'assignPermissionRequestResultToContext', + 'broadcastPermissionsToListeners', + ], }, }, invoke: {