Skip to content

Commit

Permalink
Start fixing types
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 9, 2024
1 parent 7a23891 commit 7c6c1e6
Showing 1 changed file with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import {
ActorRef,
assertEvent,
assign,
createMachine,
enqueueActions,
fromPromise,
log,
setup,
Snapshot,
} from 'xstate';
import { unimplementedPermissionMachineActions } from './permission.actions';
import { InitialPermissionStatusMap } from './permission.fixtures';
import { unimplementedPermissionCheckAndRequestActions } from './permission.actions';
import { InitialPermissionStatusMap } from '../../permission.fixtures';
import {
Permission,
PermissionMachineEvents,
PermissionMonitoringMachineEvents,
Permissions,
PermissionStatus,
PermissionStatusMapType,
} from './permission.types';
} from '../../permission.types';
import { PermissionMachineEvents } from './permissionCheckAndRequest.types';
import { PermissionMonitoringMachineEvents } from '../monitoring/permissionMonitor.types';

export const permissionCheckerAndRequesterMachine = setup({
types: {
context: {} as {
parent?: ActorRef<Snapshot<unknown>, PermissionMonitoringMachineEvents>;
statuses: PermissionStatusMapType;
},
events: {} as PermissionMachineEvents,
// events: {} as PermissionMachineEvents,
// events : {} as PermissionCheckerAndRequesterMachineEvents,
input: {} as {
parent?: ActorRef<Snapshot<unknown>, PermissionMonitoringMachineEvents>;
},
Expand All @@ -48,23 +51,23 @@ export const permissionCheckerAndRequesterMachine = setup({
statuses: ({ context, event }) => {
return {
...context.statuses,
// @ts-expect-error TODO how do I type these actions?
[event.output.permission]: event.output.status,
};
},
}),

savePermissionCheckResult: assign({
// @ts-expect-error TODO how do I type these actions?
statuses: ({ event }) => event.output,
statuses: (_, { statuses }: { statuses: PermissionStatusMapType }) => {
return statuses;
},
}),
},

actors: {
checkAllPermissions: fromPromise(async ({ input, self, system }) => {
const result =
checkAllPermissions: fromPromise(async () => {
const result: PermissionStatusMapType =
// TODO how can i make this implementation more injectable and still ergnomic
await unimplementedPermissionMachineActions.checkAllPermissions();
await unimplementedPermissionCheckAndRequestActions.checkAllPermissions();
console.log({ result });

return result;
Expand All @@ -85,13 +88,13 @@ export const permissionCheckerAndRequesterMachine = setup({
case Permissions.bluetooth:
status =
// TODO how can i make this implementation more injectable and still ergnomic
await unimplementedPermissionMachineActions.requestBluetoothPermission();
await unimplementedPermissionCheckAndRequestActions.requestBluetoothPermission();
break;

case Permissions.microphone:
status =
// TODO how can i make this implementation more injectable and still ergnomic
await unimplementedPermissionMachineActions.requestMicrophonePermission();
await unimplementedPermissionCheckAndRequestActions.requestMicrophonePermission();
break;
}

Expand Down Expand Up @@ -122,8 +125,7 @@ export const permissionCheckerAndRequesterMachine = setup({
requestingPermission: {
invoke: {
src: 'requestPermission',
// @ts-expect-error TODO how do I get this type?
input: ({ context, event }) => ({ permission: event.permission }),
input: ({ event }) => ({ permission: event.permission }),
onDone: {
target: 'idle',
actions: [
Expand Down Expand Up @@ -163,15 +165,20 @@ export const permissionCheckerAndRequesterMachine = setup({
target: 'idle',
actions: [
log('child on done checkingPermissions'),
'savePermissionCheckResult',
{
type: 'savePermissionCheckResult',
params: ({ event }) => ({
statuses: event.output as PermissionStatusMapType,
}),
},

// This is causing the typescript erro in onDone, but not sure why
{
type: 'checkedSendParent',
params({ event }) {
return {
type: 'allPermissionsChecked',
statuses: event.output as PermissionStatusMapType,
statuses: event.output,
};
},
},
Expand Down

0 comments on commit 7c6c1e6

Please sign in to comment.