generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #640 from aabidsofi19/worker-actor
[WIP] Worker actors system
- Loading branch information
Showing
12 changed files
with
661 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { assertEvent, sendTo, setup } from 'xstate'; | ||
|
||
import { Selector, Store, UnknownAction } from '@reduxjs/toolkit'; | ||
|
||
interface ReduxActorContext { | ||
store: Store; | ||
selectors: { | ||
[key: string]: Selector; | ||
}; | ||
} | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
type extraArgs = any[]; | ||
|
||
export const REDUX_COMMANDS = { | ||
DISPATCH: 'DISPATCH', | ||
GET_STATE: 'GET_STATE', | ||
GET_STATE_FROM_SELECTOR: 'GET_STATE_FROM_SELECTOR', | ||
SUBSCRIBE: 'SUBSCRIBE', | ||
UNSUBSCRIBE: 'UNSUBSCRIBE' | ||
}; | ||
|
||
export const REDUX_EVENTS = { | ||
REDUX_STATE_SNAPSHOT: 'REDUX_STATE_SNAPSHOT' | ||
}; | ||
|
||
export type REXUX_ACTOR_EVENTS = | ||
| { type: 'DISPATCH'; data: { action: UnknownAction } } | ||
| { type: 'SUBSCRIBE' } | ||
| { type: 'UNSUBSCRIBE' } | ||
| { type: 'REDUX_STATE_SNAPSHOT'; data: { snapshot: unknown } } | ||
| { type: 'GET_STATE'; returnAddress: string; data: { key: string } } | ||
| { | ||
type: 'GET_STATE_FROM_SELECTOR'; | ||
returnAddress: string; | ||
data: { selector: string; extraArgs?: extraArgs }; | ||
}; | ||
|
||
export const reduxEvents = { | ||
stateSnapshot: (snapshot: unknown) => ({ | ||
type: 'STATE_SNAPSHOT', | ||
data: { snapshot } | ||
}) | ||
}; | ||
|
||
export const reduxCommands = { | ||
dispatch: (action: UnknownAction) => ({ | ||
type: 'DISPATCH', | ||
data: { action } | ||
}), | ||
|
||
getState: (key: string, returnAddress: string) => ({ | ||
type: 'GET_STATE', | ||
returnAddress, | ||
data: { key } | ||
}), | ||
|
||
getStateFromSelector: (selector: string, returnAddress: string, extraArgs?: extraArgs) => ({ | ||
type: 'GET_STATE_FROM_SELECTOR', | ||
returnAddress, | ||
data: { selector, extraArgs } | ||
}) | ||
}; | ||
|
||
export const reduxActor = setup({ | ||
types: { | ||
context: {} as ReduxActorContext, | ||
input: {} as ReduxActorContext, | ||
events: {} as REXUX_ACTOR_EVENTS | ||
} | ||
}).createMachine({ | ||
initial: 'idle', | ||
context: ({ input }) => input, | ||
states: { | ||
idle: { | ||
on: { | ||
DISPATCH: { | ||
actions: [({ event, context }) => context.store.dispatch(event.data.action)] | ||
}, | ||
GET_STATE_FROM_SELECTOR: { | ||
actions: sendTo( | ||
({ event }) => event.returnAddress, | ||
({ context, event }) => { | ||
assertEvent(event, 'GET_STATE_FROM_SELECTOR'); | ||
const selector = context.selectors[event.data.selector]; | ||
const snapshot = selector(context.store.getState(), ...(event.data.extraArgs || [])); | ||
return reduxEvents.stateSnapshot(snapshot); | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
}); |
Oops, something went wrong.