Skip to content

Commit

Permalink
Lots of moving, preparing for application system setup
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 8, 2024
1 parent 666b576 commit 73c1312
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 276 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
export const ActorSystemIds = {
application: 'applicationMachineId',
// Top level system like stuff
systemManagement: 'systemManagementMachineId',

// Permissions
permissionMonitoring: 'permissionMonitoringMachineId',
permissionReporting: 'permissionReportingMachineId',
permissionCheckerAndRequester: 'permissionCheckerAndRequesterMachineId',

// Lifecycle reporting
lifecycleReporting: 'lifecycleReportingMachineId',

// Root of features machine
features: 'featuresMachineId',
// Features
counting: 'countingMachineId',
someFeature: 'someFeatureMachineId',
} as const;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { setup } from 'xstate';
import { ActorSystemIds } from '../application/actorIds';
import { featuresMachine } from '../features/features.machine';
import { systemManagementMachine } from '../systemManagement/systemManagement.machine';

export const applicationMachine = setup({
types: {} as {
children: {
[ActorSystemIds.features]: 'featuresMachine';
[ActorSystemIds.systemManagement]: 'topLevelSystemStuff';
};
},
actors: {
featuresMachine: featuresMachine,
topLevelSystemStuff: systemManagementMachine,
},
}).createMachine({
invoke: [
{
systemId: ActorSystemIds.features,
src: 'featuresMachine',
},
{
systemId: ActorSystemIds.systemManagement,
src: 'topLevelSystemStuff',
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { assign, raise, sendTo, setup } from 'xstate';
import {
Permissions,
PermissionStatus,
PermissionStatuses,
} from '../../permission.types';
import { permissionReportingMachine } from '../../permissionReporting/permissionReporting.machine';

export const countingMachineThatNeedsPermissionAt3 = setup({
actors: {
permissionReportingMachine,
},
types: {
context: {} as { count: number; permissionStatus: PermissionStatus },
events: { type: 'count.inc' },
},
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5QGMD2BXAdgFwJaagEFMIAFMAJwFtdZZdVNYA6NLPA5sTAQwCMANpADEbHM3zIA2gAYAuolAAHVPTyNFIAB6IA7ACYANCACeiAIwBWc8xl27AZhm6rDpw4C+H42I5ES5NS09IwsvvhQXLyCIr4SmNLmCkggKmoMmJo6CPoAHA7M1pYAbM4AnLoOLpZGpoj61sxllg7mVZbN5gYALLle3iCYqBBwmuEExGSUNHQZ8ClpuOqZKdkAtPrdzMWWuboyZeZlZd1lDsUOxmYIG1v2Mm0uus1l+vpePhg4EZOBMyFMVhfPyaRbLLL1bpXRAXZinY4nCoOMoPGTdYofEDjfxTIKzUJA9gRKL8IQQUGqJYZCEIcz6GyWXTdfQHBo7GrQnI1OEtR4daw9PoDbG-abBOaE76cCC0UmQCnpDSrRA1XLMLplYq5Rn6E7o4qchr6HmtGTWc7dbqtTEigJi-GAgAWPBIAgif3FoQVVKVoGy+gKDm6dJRuTslhkF10nIcuTVEZkbhkuU1Vt0uW6NuBPzteIBLGdrvd9vzEggQm94OVCHT23ZZv2lUeBrqCG6umKtjs+mKums7e6JUzwuzE1z-wlhfLxbzEp4yDwADcwJXqdWXMwgyHk+HI1VOS0tgm8marMdzEKvEA */
type: 'parallel',
id: 'countingAndPermissions',
context: {
count: 0,
permissionStatus: PermissionStatuses.unasked,
},

states: {
counting: {
initial: 'enabled',
states: {
enabled: {
always: [
{
target: 'disabled',
guard: ({ context }) => context.count >= 3,
},
],
on: {
'count.inc': [
{
actions: assign({ count: ({ context }) => context.count + 1 }),
},
],
},
},
disabled: {
id: 'countingDisabled',
on: {
'permission.granted.bluetooth': { target: 'enabled' },
'permission.denied.bluetooth': { target: 'bluetoothDenied' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
// @ts-expect-error TODO make this type safe
permission: Permissions.bluetooth,
}),
},
},
},
bluetoothDenied: {
on: {
'permission.granted.bluetooth': { target: 'enabled' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
// @ts-expect-error TODO make this type safe
permission: Permissions.bluetooth,
}),
},
},
},
},
},

handlingPermissions: {
on: {
permissionWasRequested: {
actions: [
sendTo('permissionHandler', ({ event }) => {
return {
type: 'requestPermission',
// @ts-expect-error TODO make this type safe
permission: event.permission,
};
}),
],
},
},
invoke: {
id: 'permissionHandler',
src: 'permissionReportingMachine',
input: ({ self }) => ({
permissions: [Permissions.bluetooth],
parent: self,
}),
},
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { setup } from 'xstate';
import { ActorSystemIds } from '../application/actorIds';
import { countingMachineThatNeedsPermissionAt3 } from './counting/counting.machine';
import { someFeatureMachine } from './someFeature/someFeature.machine';

export const featuresMachine = setup({
types: {} as {
children: {
[ActorSystemIds.counting]: 'countingMachine';
[ActorSystemIds.someFeature]: 'someFeatureMachine';
};
},
actors: {
countingMachine: countingMachineThatNeedsPermissionAt3,
someFeatureMachine: someFeatureMachine,
},
}).createMachine({
invoke: [
{
systemId: ActorSystemIds.counting,
src: 'countingMachine',
},
{
systemId: ActorSystemIds.someFeature,
src: 'someFeatureMachine',
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { raise, sendTo, setup } from 'xstate';
import { permissionReportingMachine } from '../../permissionReporting/permissionReporting.machine';
import { Permissions } from '../../permission.types';

export const someFeatureMachine = setup({
actors: {
permissionReportingMachine,
},
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5SwPYFswDEwEMAuArgE5gCyOAxgBYCWAdmAJIQB0ANilFPVAMQBUAbQAMAXUSgADilg08NFHQkgAHogCcANgAsLbZuHqAzAA4TAdm2GArEYA0IAJ6Ijw83vOv1AJmEBGa28THwBfEIdUDGx8YjJKWgZmFgAzFBQWWDwcIjxeKBQAFRQAdRw5HkwUIgAFMCI0GlhZRRFxJBBpWXlFZTUEb1cWdVs-XyNjYWtrP20HZwRNb3dF41MjJe9fPzCI9CxcQhJyanomVlT0gHcy+TooSpq6hqaFOl5JJ8bmuhYoIhw6HhICwAEZsAhgPBpPBUVrKTrlHrtPomIyaFjmYLCCxBdSTEx+OaIczWdH6byaSmGExubTbcIgSL7GJHeKnJIXFjXcp3B61epfV7vT4vRQsCBgOg0YFgiFQlAwuHtBHdJTIxCo9GYvE4rHWAlEhZBPSLTTmPxUqZGaw7Rl7aKHOInRLnNJcm4VKr857fXgEWB1cU0CAFHCSABC4Mh0KoACUwABHCGZb2ClpieEyRFq0B9bQmbwmizmbzGSwDayG2xGIYU7x+C3mTFmoy2pkO2LHBJnFJu2XRhVUAAikulEGFAtFPz+AKBrH78sVGeVWdVvUQM001hY3m0m2Em2tQQLhs01oxozNB806lWmjb9oOnbZLt76QXMZHUsgfoDRCDIZhpGcoxvGSZwHgqZTkqUirq864IA2fjCLWfhmJimq2OYVZGLoJLDNo1j+KMwiTA+URPqyzo9lQAIQGwPBQd8sATj6rylLAYHJnOMEdHBSK5hq6i6HuaHmn41pWPqhoEiw0y3gpdJmIE5hhAydAoBK8DtO2lFOt2zCZl08HqggAC0u6GmZ27qDSpH2Q5pHaORzKOl27KsBwXA8EZ2YIdo2FOIgUm1osvhuOo5ikcMLkdlRBmuigvlrqZllBQg2j6HodYNp41i2d4sV6e5r6cpk2R4MlJmCQgJansIugrNayEmHuwhnkVLL6R5b7ujy9xeiK3xVQJqiIFMuh+MM6w6FNSw3qe55GOYd7GFSwitgyuldSVPach+g4AOL-ICkAjTmY2IZYujLURB4FrZrWnnuLBNUsyGEZM9K7BRO0vntfZRouw6jmdK7GaNfQkShzX6iSpImNY+iLei9YUlFFK3kYaKdW5-1JLRdD0YxQ2vNpsEQxdeaBfMaGFpFRijIEpGaGhm1hEAA */
id: 'someFeatureMachineId',
type: 'parallel',
states: {
foo: {
initial: 'start',
states: {
start: {
entry: raise({ type: 'goToWaitingForPermission' }),
on: { goToWaitingForPermission: 'waitingForPermission' },
},
waitingForPermission: {
on: {
'permission.granted.bluetooth': { target: 'bluetoothGranted' },
'permission.denied.bluetooth': { target: 'bluetoothDenied' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
permission: Permissions.bluetooth,
}),
},
},
},
bluetoothGranted: {
type: 'final',
},
bluetoothDenied: {
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',
input: ({ self }) => ({
permissions: [Permissions.bluetooth],
parent: self,
}),
},
},
},
});
Loading

0 comments on commit 73c1312

Please sign in to comment.