Skip to content

Commit

Permalink
Merge pull request #640 from aabidsofi19/worker-actor
Browse files Browse the repository at this point in the history
[WIP] Worker actors system
  • Loading branch information
aabidsofi19 authored Jun 12, 2024
2 parents 601ffe1 + e5d9255 commit 162ca61
Show file tree
Hide file tree
Showing 12 changed files with 661 additions and 4 deletions.
95 changes: 95 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@commitlint/cli": "^17.7.2",
"@commitlint/config-conventional": "^17.7.0",
"@mui/icons-material": "^5.15.11",
"@reduxjs/toolkit": "^2.2.5",
"@testing-library/react": "^14.1.2",
"@types/jest": "^29.5.11",
"@types/react": "^18.2.45",
Expand Down Expand Up @@ -76,10 +77,10 @@
"@emotion/styled": "^11.11.0",
"@mui/material": "^5.15.11",
"@types/mui-datatables": "*",
"@xstate/react": "^4.1.1",
"mui-datatables": "*",
"react": ">=17",
"react-dom": ">=17",
"@xstate/react": "^4.1.1",
"xstate": "^5.13.0"
},
"peerDependenciesMeta": {
Expand Down
21 changes: 21 additions & 0 deletions src/actors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,28 @@ export {
selectValidationResults
} from './validators/dataValidator';

export * from './worker';

export {
REDUX_COMMANDS,
REDUX_EVENTS,
reduxActor,
reduxCommands,
reduxEvents,
type REXUX_ACTOR_EVENTS
} from './reduxActor';

export {
RTK_EVENTS,
rtkQueryActor,
rtkQueryActorCommands,
rtkQueryActorEvents
} from './rtkQueryActor';

export const REEE = 'xxx';

export {
DeferEvents,
XSTATE_DEBUG_EVENT,
deadLetter,
forwardToActors,
Expand Down
94 changes: 94 additions & 0 deletions src/actors/reduxActor.ts
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);
}
)
}
}
}
}
});
Loading

0 comments on commit 162ca61

Please sign in to comment.