mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 01:23:52 +00:00
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2020 The Pybricks Authors
|
|
|
|
import { Action, Dispatch, Middleware } from 'redux';
|
|
import { RootState } from '../reducers';
|
|
import bootloader from './bootloader';
|
|
import editor from './editor';
|
|
|
|
type Service = (action: Action, dispatch: Dispatch, state: RootState) => Promise<void>;
|
|
|
|
function runService(
|
|
service: Service,
|
|
action: Action,
|
|
dispatch: Dispatch,
|
|
state: RootState,
|
|
): void {
|
|
service(action, dispatch, state).catch((err) =>
|
|
console.log(`Unhandled exception in service: ${err}`),
|
|
);
|
|
}
|
|
|
|
export function combineServices(...services: Service[]): Service {
|
|
return (a, d, s): Promise<void> => {
|
|
services.forEach((x) => runService(x, a, d, s));
|
|
return Promise.resolve();
|
|
};
|
|
}
|
|
|
|
const rootService = combineServices(bootloader, editor);
|
|
|
|
const serviceMiddleware: Middleware = (store) => (next) => (action): unknown => {
|
|
runService(rootService, action, store.dispatch, store.getState());
|
|
return next(action);
|
|
};
|
|
|
|
export default serviceMiddleware;
|