Skip to content

Commit

Permalink
More moving and organizing, tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Mar 26, 2024
1 parent 49445e0 commit c3b8061
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 87 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { fromCallback } from 'xstate';
import { stubSubscribeToApplicationStateChanges } from './lifecycle.types';

export const stubApplicationLifecycleReportingActorLogic =
// TODO figure out how to type what events this sends back
fromCallback(({ sendBack }) => {
/**
* The real implementation of this actor should setup a subscription
* to the application lifecycle events for when the application
* is backgrounded or foregrounded and then report those messages via
* sendBack
*
* Implementations should also return a function that will unsubscribe
* any listeners
*/
const unsubscribeApplicationStateListeners =
stubSubscribeToApplicationStateChanges((event) => {
switch (event) {
case 'applicationForegrounded':
sendBack({ type: 'applicationForegrounded' });
break;
case 'applicationBackgrounded':
sendBack({ type: 'applicationBackgrounded' });
break;
}
});

return unsubscribeApplicationStateListeners;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { fromCallback } from 'xstate';

export const ApplicationLifecycleEvents = {
applicationForegrounded: 'applicationForegrounded',
applicationBackgrounded: 'applicationBackgrounded',
} as const;

export type ApplicationLifecycleEvent =
(typeof ApplicationLifecycleEvents)[keyof typeof ApplicationLifecycleEvents];

export const ApplicationLifecycleStates = {
applicationInForeground: 'application is in foreground',
applicationInBackground: 'application is in background',
} as const;

export type ApplicationLifecycleState =
| 'applicationForegrounded'
| 'applicationBackgrounded';

export type ApplicationStateChangeHandler = (
event: ApplicationLifecycleState
) => void;
export const stubSubscribeToApplicationStateChanges = (
handleApplicationStateChange: ApplicationStateChangeHandler
) => {
console.log('subscribed to fake handler');
handleApplicationStateChange('applicationForegrounded');

return () => {
console.log('unsubscribed from fake handler');
};
};
60 changes: 2 additions & 58 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,10 @@ import {
PermissionStatusMapType,
PermissionStatuses,
Permissions,
PermissionMonitoringMachineEvents,
} from './permission.types';
import { InitialPermissionStatusMap } from './permission.fixtures';

export type PermissionMonitoringMachineEvents =
| {
type: 'allPermissionsChecked';
statuses: PermissionStatusMapType;
}
| { type: 'triggerPermissionRequest'; permission: Permission }
| {
type: 'permissionRequestCompleted';
status: PermissionStatus;
permission: Permission;
}
| { type: 'triggerPermissionCheck' }
| { type: 'applicationForegrounded' }
| { type: 'applicationBackgrounded' };

type ApplicationLifecycleState =
| 'applicationForegrounded'
| 'applicationBackgrounded';

type ApplicationStateChangeHandler = (event: ApplicationLifecycleState) => void;
const stubSubscribeToApplicationStateChanges = (
handleApplicationStateChange: ApplicationStateChangeHandler
) => {
console.log('subscribed to fake handler');
handleApplicationStateChange('applicationForegrounded');

return () => {
console.log('unsubscribed from fake handler');
};
};

const stubApplicationLifecycleReportingActorLogic =
// TODO figure out how to type what events this sends back
fromCallback(({ sendBack }) => {
/**
* The real implementation of this actor should setup a subscription
* to the application lifecycle events for when the application
* is backgrounded or foregrounded and then report those messages via
* sendBack
*
* Implementations should also return a function that will unsubscribe
* any listeners
*/
const unsubscribeApplicationStateListeners =
stubSubscribeToApplicationStateChanges((event) => {
switch (event) {
case 'applicationForegrounded':
sendBack({ type: 'applicationForegrounded' });
break;
case 'applicationBackgrounded':
sendBack({ type: 'applicationBackgrounded' });
break;
}
});

return unsubscribeApplicationStateListeners;
});
import { stubApplicationLifecycleReportingActorLogic } from './lifecycle/lifecycle.stubs';

describe('Permission Requester and Checker Machine', () => {
describe('Checking Permissions', () => {
Expand Down
49 changes: 32 additions & 17 deletions libs/permissions/permissionLogic/src/lib/permission.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ export const PermissionStatuses = {
export type PermissionStatus =
(typeof PermissionStatuses)[keyof typeof PermissionStatuses];

export type PermissionMonitoringMachineEvents =
| {
type: 'allPermissionsChecked';
statuses: PermissionStatusMapType;
}
| { type: 'triggerPermissionRequest'; permission: Permission }
| {
type: 'permissionRequestCompleted';
status: PermissionStatus;
permission: Permission;
}
| { type: 'triggerPermissionCheck' }
| { type: 'applicationForegrounded' }
| { type: 'applicationBackgrounded' };

export interface PermissionMachineActions {
checkAllPermissions: () => Promise<PermissionStatusMapType>;
requestBluetoothPermission: () => Promise<PermissionStatus>;
Expand All @@ -29,23 +44,23 @@ export const PermissionCheckingStates = {
export type PermissionMonitoringMachineContext = {
permissionStatuses: PermissionStatusMapType;
};
export type PermissionMonitoringMachineEvents =
| { type: 'checkPermissions' }
| {
type: 'permissionChecked';
permission: Permission;
status: PermissionStatus;
}
| {
type: 'triggerPermissionCheck';
permission: Permission;
}
| {
type: 'triggerPermissionRequest';
permission: Permission;
}
| { type: 'applicationForegrounded' }
| { type: 'applicationBackgrounded' };
// export type PermissionMonitoringMachineEvents =
// | { type: 'checkPermissions' }
// | {
// type: 'permissionChecked';
// permission: Permission;
// status: PermissionStatus;
// }
// | {
// type: 'triggerPermissionCheck';
// permission: Permission;
// }
// | {
// type: 'triggerPermissionRequest';
// permission: Permission;
// }
// | { type: 'applicationForegrounded' }
// | { type: 'applicationBackgrounded' };

export type PermissionMachineEvents =
| { type: 'triggerPermissionCheck' }
Expand Down

0 comments on commit c3b8061

Please sign in to comment.