Skip to content

Commit

Permalink
cleanup counting feature machine
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 22, 2024
1 parent 59ad3da commit 0ebe3c5
Showing 1 changed file with 16 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assign, log, not, raise, sendTo, setup } from 'xstate';
import { assign, not, sendTo, setup } from 'xstate';
import {
Permission,
PermissionStatus,
Expand All @@ -15,8 +15,9 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
actions: {
incrementCount: assign({ count: ({ context }) => context.count + 1 }),
assignBluetoothStatusGranted: assign({ permissionStatus: 'granted' }),
triggerBluetoothPermissionRequest: raise({
type: 'permissionWasRequested',

triggerBluetoothPermissionRequest: sendTo('permissionReportingCounting', {
type: 'requestPermission',
permission: Permissions.bluetooth,
}),
},
Expand All @@ -30,21 +31,18 @@ export const countingMachineThatNeedsPermissionAt3 = setup({

types: {
context: {} as { count: number; permissionStatus: PermissionStatus },
events:
{} as // TODO pull these out into their own discrete types and do a union here
events: {} as // TODO pull these out into their own discrete types and do a union here
| { type: 'count.inc' }
| { type: 'permissions.bluetooth.revoked' }
| { type: 'permissions.bluetooth.granted' }
| { type: 'permissions.bluetooth.denied' }
| { type: 'permissionWasRequested'; permission: Permission }
| {
type: 'user.didTapBluetoothRequestPermission';
permission: Permission;
},
| { type: 'permissions.bluetooth.revoked' }
| { type: 'permissions.bluetooth.granted' }
| { type: 'permissions.bluetooth.denied' }
| {
type: 'user.didTapBluetoothRequestPermission';
permission: Permission;
},
},
}).createMachine({
/** @xstate-layout N4IgpgJg5mDOIC5QAoC2BDAxgCwJYDswBKAYgAcwAnVXWWXAe31gDoAjAGwFcwAXBhr2wtKYAG4MA1pADaABgC6iUGQb1ejfMpAAPRAFoAjADYArCwAshgJwAmABwBmC6YDst03YA0IAJ6JHQzlLazkrR1tHOUjHVwBfOJ80LDxCIhYwfHROSBJMBi58XhYCTHklJBBVdU1tPQR7IJZHY3tTW1tDQwtHa1NTH38EDosWe3s5Q1dJ02Mw21d4xJBknAJiDKyciBJy7WrcDSY6xGtrexZDRrlo+wtbMPvBxAXDFmiruVMJszlxhKSGDWaU22Q4uRkhgqKjUh1qlXqZwuVyCt3uj1szwQzmCrnsxiseIJ1kcpgBKyBqQ22HQ+AgHAIUAAClQaHRNLByKzaPQmCwoJRabxIOxuHwBEI9pUDkctAjEEFDOZ+u4LCS7v0OliOm8bMYHN97rYLHJnOTVlT0jS6Qz8MzueymJyKNQeZoWBBMrgRZwePxBNgpTCasd5QhumdmmZQk5rMZrPcLNr+s17LZ44Z0+FFubKet0r7xQGACJe3Iutm8-D8wVFH1i-2SxT7WGyk7htXWKOeP69eOJrGolh40kefGTOTGbq5lL50V+iXYUv4b07LiwKge3AQAAq6DIACEG4uAEpgACOPFgvBZrsd+CDVVb8NA9Sm7jGcjOU46E08Sb8BU3GHVxPBsNNHFiCwLCWQFZxBQtG2wM8JGkHYKzdPkBSFesFwDR8ZRfXREFMFoWDsaD4wcCI8VcLFAlcYdHHxRZwlMcCLBnYENkQ09xCkXJ103CBtz3Q9jwDM9LzgG8HSrAjn1DV8FScFhbHOQxmMmB4E2sbUphYYxAlCTx3FA4xjC4y15yLIQUIEtcN0oLdd33AA5dAxFwKB0GFHcGAAZT4DQ7VgBSQzlZThgM9w7msTTmNCLpjEHDpmjcExpjOVwpgJBJlnwBhPXgSoLXzFsIvbfROkcSwbGolx3E8TFAIQfR2ijKdzkCdSVVcTjljKkFMjBSAKrhJTiIaZj3jjWJnEg1pWnojxLlMNUv2iGMpisudrXpRlb0rDlxrbMNMwTd5JiiHVZnjbUIkuUlQm6rTJ1gil4J4iShGXVdTqIt98TeexznY6YLHGfU6Nasd3lMSdvnjAlNJcXaEJ+5D+LQgHJvqdNjDGIJ2i-eMvlCAChgY8jJwJSHGr6SzBrzEEADMCFobAxulRTIqmi7RhuTTok6O69NayDGJcTt02+frAjJfKgA */
// type: 'parallel',
context: {
count: 0,
permissionStatus: PermissionStatuses.unasked,
Expand Down Expand Up @@ -72,12 +70,12 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
enabled: {
always: [
{
target: 'handlingPermissions',
guard: 'isPermissionRequiredToContinue',
target: 'handlingPermissions',
},
{
target: 'finished',
guard: 'isCountingCompleted',
target: 'finished',
},
],
on: {
Expand All @@ -90,12 +88,7 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
handlingPermissions: {
on: {
'user.didTapBluetoothRequestPermission': {
actions: sendTo('permissionReportingCounting', ({ event }) => {
return {
type: 'requestPermission',
permission: Permissions.bluetooth,
};
}),
actions: 'triggerBluetoothPermissionRequest',
},

'permissions.bluetooth.granted': {
Expand All @@ -110,12 +103,7 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
on: {
'permissions.bluetooth.granted': {
target: 'enabled',
actions: [
'assignBluetoothStatusGranted',
log(({ event }) => {
console.log(JSON.stringify(event, null, 2));
}),
],
actions: ['assignBluetoothStatusGranted'],
},
'user.didTapBluetoothRequestPermission': {
actions: 'triggerBluetoothPermissionRequest',
Expand Down

0 comments on commit 0ebe3c5

Please sign in to comment.