Skip to content

Commit

Permalink
Fix some typescript todos
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 22, 2024
1 parent c46e55d commit c9b2599
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assign, raise, sendTo, setup } from 'xstate';
import {
Permission,
PermissionStatus,
PermissionStatuses,
Permissions,
Expand All @@ -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 */
Expand Down Expand Up @@ -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,
}),
},
Expand All @@ -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,
}),
},
Expand All @@ -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,
};
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Permission>;
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
})
Expand All @@ -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 }) =>
Expand All @@ -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 };
},
},
],
Expand Down

0 comments on commit c9b2599

Please sign in to comment.