-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch worker communication to comlink (#68)
- Loading branch information
Showing
33 changed files
with
990 additions
and
799 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,29 @@ | ||
import { useDispatch, useSelector } from "react-redux"; | ||
import type { TypedUseSelectorHook } from "react-redux"; | ||
import type { RootState, AppDispatch } from "./store"; | ||
import { AsyncThunkPayloadCreator, createAsyncThunk } from "@reduxjs/toolkit"; | ||
import { AsyncThunkConfig } from "@reduxjs/toolkit/dist/createAsyncThunk"; | ||
|
||
// Use throughout your app instead of plain `useDispatch` and `useSelector` | ||
export const useAppDispatch: () => AppDispatch = useDispatch; | ||
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector; | ||
|
||
/** | ||
* A wrapper around `createAsyncThunk` that logs to the console | ||
* when an error is thrown. Errors thrown in thunks created with `createAsyncThunk` are | ||
* throw silently. (You are expected to catch the `rejected` status to deal with the error.) | ||
*/ | ||
export const createLoggingAsyncThunk = <Returned, ThunkArg = void>( | ||
typePrefix: string, | ||
payloadCreator: AsyncThunkPayloadCreator<Returned, ThunkArg, AsyncThunkConfig & {state: RootState}>, | ||
) => { | ||
const wrappedPayloadCreator: typeof payloadCreator = (async (...args) => { | ||
try { | ||
return await payloadCreator(...args); | ||
} catch (e) { | ||
console.warn(e); | ||
throw e; | ||
} | ||
}) as typeof payloadCreator; | ||
return createAsyncThunk(typePrefix, wrappedPayloadCreator); | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/doenetml-prototype/src/state/redux-slices/core/index.ts
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,5 @@ | ||
export * from "./slice"; | ||
import { _coreReducerActions } from "./slice"; | ||
import { coreThunks } from "./thunks"; | ||
|
||
export const coreActions = { ..._coreReducerActions, ...coreThunks }; |
53 changes: 53 additions & 0 deletions
53
packages/doenetml-prototype/src/state/redux-slices/core/slice.ts
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,53 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
import type { PayloadAction } from "@reduxjs/toolkit"; | ||
import type { RootState } from "../../store"; | ||
|
||
export interface CoreState { | ||
/** | ||
* Whether core has been initialized. | ||
*/ | ||
initialized: boolean; | ||
/** | ||
* Whether core has been started and initialized with a DAST tree | ||
*/ | ||
launched: boolean; | ||
/** | ||
* The webworker used by core is stored outside of the redux store (since it is not serializable). | ||
* This key is used to retrieve it. | ||
*/ | ||
workerCacheKey?: number; | ||
inErrorState: boolean; | ||
} | ||
|
||
// Define the initial state using that type | ||
const initialState: CoreState = { | ||
initialized: false, | ||
launched: false, | ||
workerCacheKey: undefined, | ||
inErrorState: false, | ||
}; | ||
|
||
const coreSlice = createSlice({ | ||
name: "core", | ||
initialState, | ||
reducers: { | ||
_setInitialized: (state, action: PayloadAction<boolean>) => { | ||
state.initialized = action.payload; | ||
}, | ||
_setWorkerCacheKey: (state, action: PayloadAction<number>) => { | ||
state.workerCacheKey = action.payload; | ||
}, | ||
_setInErrorState: (state, action: PayloadAction<boolean>) => { | ||
state.inErrorState = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const coreReducer = coreSlice.reducer; | ||
|
||
/** | ||
* Synchronous actions that directly manipulate data in the store. | ||
*/ | ||
export const _coreReducerActions = { ...coreSlice.actions }; | ||
|
||
export const selfSelector = (state: RootState) => state.core; |
Oops, something went wrong.