diff --git a/Machine best practices.md b/Machine best practices.md index efbc4ce..c377199 100644 --- a/Machine best practices.md +++ b/Machine best practices.md @@ -7,4 +7,15 @@ Machine best practices ├── .machine.ts ├── .types.ts ├── .actions.ts - ├── .actors.ts \ No newline at end of file + ├── .actors.ts + +3) Should we have every actor declared in a system??? + - PROS + - system.get works + - everything is explicit + - you can simulate everything in that actor system headlessly + - CONS + - Complexity... + +4) Should we use emit? +I don't think so, because it causes implicit dependencies at the cost of convenience diff --git a/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts b/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts index a102d99..5acd343 100644 --- a/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts +++ b/libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts @@ -15,6 +15,8 @@ import { sendTo, setup, waitFor, + createMachine, + AnyEventObject, } from 'xstate'; import { stubApplicationLifecycleReportingActorLogic } from './lifecycle/lifecycle.stubs'; @@ -561,8 +563,11 @@ const permissionMonitoringMachine = setup({ permissionSubscribers: EmptyPermissionSubscriberMap, }, on: { - // subscribeToPermissionStatuses: {}, + subscribeToPermissionStatuses: { + actions: [log('received permission subscription'), log('or did we')], + }, }, + // entry: raise({ type: 'subscribeToPermissionStatuses', permissions: [] }), states: { applicationLifecycle: { on: { @@ -631,7 +636,44 @@ describe('Permission Monitoring Machine', () => { ); }); describe('Single Subscriber', () => { - it('should allow subscriptions from a subscriber to any permissions', () => {}); + const dummyFeatureMachine = setup({ + actions: { + sendSubscriptionRequestForStatusUpdates: + //TODO this is not sending figure out whhy + sendTo( + ({ system }) => { + const actorRef: AnyActorRef = system.get('bigKahuna'); + console.log({ actorRef }); + return actorRef; + }, + { + type: 'subscribeToPermissionStatuses', + // permissions: [Permissions.bluetooth], + } + ), + // satisfies /*TODO type these events to the receiving machine event type*/ AnyEventObject); + }, + }).createMachine({ + id: 'dummyFeatureId', + entry: [ + 'sendSubscriptionRequestForStatusUpdates', + log('subscribe to status updates'), + ], + }); + it('should allow subscriptions from a subscriber to any permissions', () => { + const actor = createActor(permissionMonitoringMachine, { + parent: undefined, + systemId: 'bigKahuna', + }).start(); + + const dummyFeatureActor = createActor(dummyFeatureMachine).start(); + + const state = actor.getSnapshot(); + expect( + state.context.permissionSubscribers?.[Permissions.bluetooth] + ?.length ?? 0 + ).toEqual(1); + }); }); }); it('handle the happy path of being invoked, checking permission initially and then handle a permission request', async () => { diff --git a/libs/permissions/permissionLogic/src/lib/permission.types.ts b/libs/permissions/permissionLogic/src/lib/permission.types.ts index c9cf85e..350025a 100644 --- a/libs/permissions/permissionLogic/src/lib/permission.types.ts +++ b/libs/permissions/permissionLogic/src/lib/permission.types.ts @@ -14,6 +14,7 @@ export type PermissionStatus = (typeof PermissionStatuses)[keyof typeof PermissionStatuses]; export type PermissionMonitoringMachineEvents = + | { type: 'subscribeToPermissionStatuses'; permissions: Permission[] } | { type: 'allPermissionsChecked'; statuses: PermissionStatusMapType; diff --git a/xstate questions.md b/xstate questions.md index 344f707..bf0dc3c 100644 --- a/xstate questions.md +++ b/xstate questions.md @@ -1,4 +1,7 @@ Questions about xstate: 1) Can I type the onDone events from a fromPromise event when handling them, for example, in the onDone handler? -2) \ No newline at end of file +- https://discord.com/channels/795785288994652170/1021738689434501181/threads/1219788098209058897 + + +2)