Skip to content

Commit

Permalink
Single subscription is working
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Mar 28, 2024
1 parent b138d0a commit 5af6547
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 33 deletions.
91 changes: 81 additions & 10 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,8 @@ describe('Permission Monitoring Machine', () => {
).toEqual(1);
});

it(/*FIXME: how do I access the actor
or trigger the subscription request again
or configure different starting context via input
*/ 'should not add a subscriber if the subscriber is already subscribed', () => {
const dummyFeatureMachineThatSubscribesTwice = setup({
it('should notify subscribers of changes to permissions', (done) => {
const dummyFeatureMachine = setup({
actions: {
sendSubscriptionRequestForStatusUpdates: sendTo(
({ system }) => {
Expand All @@ -389,16 +386,40 @@ describe('Permission Monitoring Machine', () => {
}).createMachine({
id: 'dummyFeatureId',
entry: [
'sendSubscriptionRequestForStatusUpdates',
'sendSubscriptionRequestForStatusUpdates',
log('subscribe to status updates'),
],
on: {
permissionStatusChanged: {
actions: [
log(
({ event }) =>
event.permission + ' status changed' + ' to ' + event.status
),
() => {
done();
},
],
},
// permissionGranted: {
// actions: [
// log('permission granted'),
// () => {
// console.log('another event');
// done();
// },
// ],
// },
// permissionDenied: {
// actions: log('permission denied'),
// },
},
});

const actor = createActor(
permissionMonitoringMachine.provide({
actors: {
features: dummyFeatureMachineThatSubscribesTwice,
features: dummyFeatureMachine,
},
}),
{
Expand All @@ -407,12 +428,62 @@ describe('Permission Monitoring Machine', () => {
}
).start();

const state = actor.getSnapshot();
expect(
actor.getSnapshot().context.permissionSubscribers[
Permissions.bluetooth
].length
state.context.permissionSubscribers[Permissions.bluetooth].length
).toEqual(1);
});

describe('Edge Cases', () => {
it('should not add a subscriber if the subscriber is already subscribed', () => {
/*FIXME: I don't like having to create another test actor for this
how do I access the actor
or trigger the subscription request again
or configure different starting context via input
*/
const dummyFeatureMachineThatSubscribesTwice = setup({
actions: {
sendSubscriptionRequestForStatusUpdates: sendTo(
({ system }) => {
const actorRef: AnyActorRef = system.get('bigKahuna');
return actorRef;
},
({ self }) => ({
type: 'subscribeToPermissionStatuses',
permissions: [Permissions.bluetooth],
self,
})
),
// satisfies /*TODO type these events to the receiving machine event type*/ AnyEventObject);
},
}).createMachine({
id: 'dummyFeatureId',
entry: [
'sendSubscriptionRequestForStatusUpdates',
/*Second subscription should have no effect*/ 'sendSubscriptionRequestForStatusUpdates',
log('subscribe to status updates'),
],
});

const actor = createActor(
permissionMonitoringMachine.provide({
actors: {
features: dummyFeatureMachineThatSubscribesTwice,
},
}),
{
parent: undefined,
systemId: 'bigKahuna',
}
).start();

expect(
actor.getSnapshot().context.permissionSubscribers[
Permissions.bluetooth
].length
).toEqual(1);
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ export const permissionCheckerAndRequesterMachine = setup({
// @ts-expect-error TODO how do I type these actions?
statuses: ({ event }) => event.output,
}),

/**
* I tried putting reportPermissionRequestResult as an action, but it requied
* use of checkedSendParent and ran into this error when attempting to use that
*
* in onDone, but it didn't work
*
* error: Type '"checkedSendParent"' is not assignable to type '"triggerPermissionRequest"'.ts(2322)
*/
// reportPermissionRequestResult: raise({
// type: 'checkedSendParent',
// params({ event }) {
// console.log(JSON.stringify(event, null, 2));

// return {
// type: 'permissionRequestCompleted',
// status: event.output.status,
// permission: event.output.permission,
// };
// },
// }),
},

actors: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import {
Permissions,
PermissionStatusMapType,
} from './permission.types';
import { assertEvent, assign, log, raise, sendTo, setup } from 'xstate';
import {
assertEvent,
assign,
createMachine,
enqueueActions,
log,
raise,
sendTo,
setup,
} from 'xstate';
import { stubApplicationLifecycleReportingActorLogic } from './lifecycle/lifecycle.stubs';
import { InitialPermissionStatusMap } from './permission.fixtures';
import { PermissionSubscriberMap } from './permission-logic.spec';
Expand Down Expand Up @@ -41,6 +50,20 @@ export const permissionMonitoringMachine = setup({
return event.statuses;
},
}),
broadcastPermissionsToListeners: enqueueActions(
({ context, event, enqueue }) => {
console.log(event);
Object.keys(context.permissionSubscribers).forEach((permission) => {
context.permissionSubscribers[permission].forEach((actorRef) => {
enqueue.sendTo(actorRef, {
type: 'permissionStatusChanged',
permission,
status: context.permissionsStatuses[permission],
});
});
});
}
),
assignPermissionRequestResultToContext: assign({
permissionsStatuses: ({ event, context }) => {
assertEvent(event, 'permissionRequestCompleted');
Expand Down Expand Up @@ -143,7 +166,7 @@ export const permissionMonitoringMachine = setup({
allPermissionsChecked: {
actions: [
'assignPermissionCheckResultsToContext',
// TODO 'broadcastPermissionsToListeners',
'broadcastPermissionsToListeners',
],
},

Expand Down

0 comments on commit 5af6547

Please sign in to comment.