Skip to content

Commit

Permalink
Saving progress, about to send permission request and wait on checker
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 8, 2024
1 parent 2518c23 commit 2ac4eae
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setup } from 'xstate';
import { log, setup } from 'xstate';
import { ActorSystemIds } from '../application/actorIds';
import { featuresMachine } from '../features/features.machine';
import { systemManagementMachine } from '../systemManagement/systemManagement.machine';
Expand All @@ -15,6 +15,8 @@ export const applicationMachine = setup({
topLevelSystemStuff: systemManagementMachine,
},
}).createMachine({
entry: log('Application started'),
id: ActorSystemIds.application,
invoke: [
{
id: ActorSystemIds.features,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign, raise, sendTo, setup } from 'xstate';
import { assign, log, raise, sendTo, setup } from 'xstate';
import {
Permissions,
PermissionStatus,
Expand All @@ -23,6 +23,8 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
permissionStatus: PermissionStatuses.unasked,
},

entry: log('Counting machine started'),

states: {
counting: {
initial: 'enabled',
Expand Down Expand Up @@ -87,6 +89,7 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
},
invoke: {
id: 'permissionHandler',
systemId: 'countingPermissionReporter',
src: 'permissionReportingMachine',
input: ({ self }) => ({
permissions: [Permissions.bluetooth],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setup } from 'xstate';
import { log, setup } from 'xstate';
import { ActorSystemIds } from '../application/actorIds';
import { countingMachineThatNeedsPermissionAt3 } from './counting/counting.machine';
import { someFeatureMachine } from './someFeature/someFeature.machine';
Expand All @@ -7,22 +7,26 @@ export const featuresMachine = setup({
types: {} as {
children: {
[ActorSystemIds.counting]: 'countingMachine';
[ActorSystemIds.someFeature]: 'someFeatureMachine';
// [ActorSystemIds.someFeature]: 'someFeatureMachine';
};
},
actors: {
countingMachine: countingMachineThatNeedsPermissionAt3,
someFeatureMachine: someFeatureMachine,
// someFeatureMachine: someFeatureMachine,
},
}).createMachine({
entry: log('Features started'),

invoke: [
{
id: ActorSystemIds.counting,
systemId: ActorSystemIds.counting,
src: 'countingMachine',
},
{
systemId: ActorSystemIds.someFeature,
src: 'someFeatureMachine',
},
// {
// id: ActorSystemIds.someFeature,
// systemId: ActorSystemIds.someFeature,
// src: 'someFeatureMachine',
// },
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export const someFeatureMachine = setup({
],
},
},
invoke: {
id: 'permissionHandler',
src: 'permissionReportingMachine',
input: ({ self }) => ({
permissions: [Permissions.bluetooth],
parent: self,
}),
},
// invoke: {
// id: 'permissionHandler',
// src: 'permissionReportingMachine',
// input: ({ self }) => ({
// permissions: [Permissions.bluetooth],
// parent: self,
// }),
// },
},
},
});
106 changes: 50 additions & 56 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,82 +27,74 @@ import { someFeatureMachine } from './features/someFeature/someFeature.machine';
import { countingMachineThatNeedsPermissionAt3 } from './features/counting/counting.machine';
import { applicationMachine } from './application/application.machine';

const vLongTime = 1000000000;

describe('Counting Machine That Needs Permission At 3', () => {
it('should increment count to 3, ask for permission, and continue counting to 5 when permission is granted', async () => {
const applicationActor = createActor(applicationMachine, {
systemId: ActorSystemIds.application,
// inspect: createSkyInspector({
// // @ts-expect-error
// WebSocket: WebSocket,
// inspectorType: 'node',
// autoStart: true,
// }).inspect,
});
applicationActor.start();

const permissionMonitorActor = applicationActor.system.get(
ActorSystemIds.permissionMonitoring
);

// const permissionMonitorActor = applicationActor
// .getSnapshot()
// .children[ActorSystemIds.systemManagement].getSnapshot().children[
// ActorSystemIds.permissionMonitoring
// ];
const countingPermissionReporter = applicationActor.system.get(
'countingPermissionReporter'
);

// const permissionMonitorActor = applicationActor.system
// .get(ActorSystemIds.systemManagement)
// .getSnapshot().children[ActorSystemIds.permissionMonitoring];
expect(permissionMonitorActor).toBeDefined();
expect(countingPermissionReporter).toBeDefined();

// const permissionMonitorActor = createActor(
// permissionMonitoringMachine,
// // permissionMonitoringMachine.provide({
// // actors: {
// // features: countingMachineThatNeedsPermissionAt3,
// // },
// // }),
// {
// systemId: ActorSystemIds.permissionMonitoring,
// }
// ).start();
console.log(countingPermissionReporter.getSnapshot().value);
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(countingPermissionReporter.getSnapshot().value);

const state = permissionMonitorActor.getSnapshot();
console.log({ v: state.context });
// expect(
// state.context.permissionSubscribers[Permissions.bluetooth].length
// ).toEqual(1);
console.log({ v: state.context.permissionSubscribers });
expect(
state.context.permissionSubscribers[Permissions.bluetooth]?.length
).toEqual(1);

// const featureMachineActor =
// permissionMonitorActor.getSnapshot().children.featuresMachineId;
// expect(featureMachineActor?.getSnapshot().value).toStrictEqual({
// counting: 'enabled',
// handlingPermissions: {},
// });
const countingActor = applicationActor.system.get(ActorSystemIds.counting);
expect(countingActor?.getSnapshot().value).toStrictEqual({
counting: 'enabled',
handlingPermissions: {},
});

// const countingActor = createActor(
// countingMachineThatNeedsPermissionAt3,
// {}
// ).start();
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(3);
expect(countingActor.getSnapshot().value).toStrictEqual({
counting: 'disabled',
handlingPermissions: {},
});

// countingActor.send({ type: 'count.inc' });
// countingActor.send({ type: 'count.inc' });
// countingActor.send({ type: 'count.inc' });
// expect(countingActor.getSnapshot().context.count).toBe(3);
// expect(countingActor.getSnapshot().value).toStrictEqual({
// counting: 'disabled',
// handlingPermissions: {},
// });
//
// countingActor.send({ type: 'count.inc' });
// expect(countingActor.getSnapshot().context.count).toBe(3);
// expect(countingActor.getSnapshot().value).toStrictEqual({
// counting: 'disabled',
// handlingPermissions: {},
// });
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(3);
expect(countingActor.getSnapshot().value).toStrictEqual({
counting: 'disabled',
handlingPermissions: {},
});

// Configure the permission actor to grant permission
// const permissionCheckerActor =
// countingActor.getSnapshot().children
// .permissionCheckerAndRequesterMachineId!;
// permissionCheckerActor.send({
// type: 'triggerPermissionRequest',
// permission: Permissions.bluetooth,
// });
const permissionCheckerActor = applicationActor.system.get(
ActorSystemIds.permissionCheckerAndRequester
);

// TODO, this should be handled by sending an event to the countingActor
permissionCheckerActor.send({
type: 'triggerPermissionRequest',
permission: Permissions.bluetooth,
});

// await waitFor(permissionCheckerActor, (state) => state.value === 'idle');
//
Expand All @@ -117,7 +109,9 @@ describe('Counting Machine That Needs Permission At 3', () => {
// counting: 'finished',
// handlingPermissions: 'idle',
// });
}); // prettyMuchForever
// await new Promise((resolve) => setTimeout(resolve, vLongTime));
});
// vLongTime // prettyMuchForever

// it('should start in idle state', async () => {
// const countingActor = createActor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export const permissionCheckerAndRequesterMachine = setup({

checkingPermissions: {
invoke: {
id: 'checkAllPermissionsId',
src: 'checkAllPermissions',
onError: {
actions: log('an error occurred checking permissions'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const permissionMonitoringMachine = setup({
/** @xstate-layout N4IgpgJg5mDOIC5QCMCWUDSBDAFgVwDssA6LABzIBtUBjLAF1QHsCAZVAMzBoE8bKwAYnJVaDZgQBiTAE5goMpoQiQA2gAYAuolBkmsVIxY6QAD0QBaABwBmdcQCcANicAWKwFY7D165tWAGhAeSwBGf2JQgHYbf3U7J38PACYogF80oLRMXEISEWo6IzZObj4BYQpC8RYAISwaAGsFJQIVCA1tJBA9A2KTcwQLVxjHZISoqz9bcI8gkKHwq0iYmydk9Sso9VdQq2SMrPRsfCJiMjAZAFtUWAMWWGFKSgAFS5u7iVgAYRxuRrUWhMvUMEgGiBsrmSxB2ySsVjcoS8O3Uc2CYSs9lCiSi218DiiHnU6gOmRA2ROeXO71u9wIj3oMnQMBkb2utIkv3+nWB+lBxm6gwsyVCjiRoQcW3iNiivj88zCDhsxBGoXUUQlMQRDg8VkO5OOuTOF3ZnwegkZzMubI+dIASmAAI54OD0HndEH9QWIZLQ4lOKwEgOBqa+tELSHQmW48bhX1rJz6ilGkgm21fQRpjksB3O13fJhXKhgeiArq6Ple0CDGzJVwq2sI6KeDzrcMQjz1zzYkVOGJKvsZMkEJgqeDdZOnLC8vpg71DBzJBwq1K+qFOTa2QLooY2DzLtwIpdwgPJAOJsmTqkFMTFdhcXj8MAz-kEcFDeGiqFRNdnzf+BUhlXYhaw8H8JR2WJXFRJNDSnUgqlvCR7zKJ8ENEIoJAASVgLCpFkeRFGUF8qzMSxbGVb9fw3eEAJ3CwPC-KIlQcJEbFCeN4lgnJ4JvTCWBQx8BHQ6pijw+omhaYiPUrOdq0sVwnGXH8NWJBwHGJCVXEA4VlnUNwnC8RcYy3C8jh4qkszNekSLksiEH2GF-E8fZ3GxSNAJlYhWysCDwkhXZ9yHNIgA */
id: ActorSystemIds.permissionMonitoring,
type: 'parallel',
entry: log('monitoring started'),

context: {
permissionsStatuses: InitialPermissionStatusMap,
Expand Down Expand Up @@ -189,6 +190,7 @@ export const permissionMonitoringMachine = setup({
},
permissionRequestCompleted: {
actions: [
log('this happened'),
'assignPermissionRequestResultToContext',
'broadcastPermissionsToListeners',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const permissionReportingMachine = setup({
const actorRef: AnyActorRef = system.get(
ActorSystemIds.permissionMonitoring
);
console.log({ actorRef });
console.log('hi');
return actorRef;
},
({ self, context }) => ({
Expand Down Expand Up @@ -55,10 +57,18 @@ export const permissionReportingMachine = setup({
permissions: input.permissions,
parent: input.parent,
}),
entry: [
'sendSubscriptionRequestForStatusUpdates',
log('subscribe to status updates'),
],
initial: 'waiting',
states: {
waiting: {
after: {
/* This is required due to the way actor systems are initialized. Without this delay, the lower level actor (us)
* will send out the subscription request before the top level permission monitoring actor is ready to receive it.*/
0: {
actions: ['sendSubscriptionRequestForStatusUpdates'],
},
},
},
},
on: {
requestPermission: {
actions: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createMachine, setup } from 'xstate';
import { ActorSystemIds } from '../application/actorIds';
import { permissionMonitoringMachine } from '../permissionMonitor.machine';
import { types } from 'node:util';

export const systemManagementMachine = setup({
types: {} as {
Expand Down

0 comments on commit 2ac4eae

Please sign in to comment.