add Hyper Dispatch type

This commit is contained in:
Labhansh Agrawal 2020-01-02 23:19:57 +05:30 committed by Benjamin Staneck
parent 7355d871d5
commit 1b70f9e727
9 changed files with 34 additions and 12 deletions

5
lib/hyper.d.ts vendored
View file

@ -180,6 +180,11 @@ export type HyperActions = (
type immutableRecord<T> = {[k in keyof T]: Immutable<T[k]>};
import {ThunkDispatch} from 'redux-thunk';
import configureStore from './store/configure-store';
export type HyperThunkDispatch = ThunkDispatch<HyperState, undefined, HyperActions>;
export type HyperDispatch = ReturnType<typeof configureStore>['dispatch'];
export type TermsProps = {
activeRootGroup: string | null;
activeSession: string | null;

View file

@ -1,14 +1,15 @@
import {createStore, applyMiddleware, compose} from 'redux';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers/index';
import effects from '../utils/effects';
import * as plugins from '../utils/plugins';
import writeMiddleware from './write-middleware';
import {composeWithDevTools} from 'redux-devtools-extension';
import {HyperState, HyperThunkDispatch} from '../hyper';
export default () => {
const enhancer = compose(
applyMiddleware(thunk, plugins.middleware, thunk, writeMiddleware, effects),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
const enhancer = composeWithDevTools(
applyMiddleware<HyperThunkDispatch, HyperState>(thunk, plugins.middleware, thunk, writeMiddleware, effects)
);
return createStore(rootReducer, enhancer);

View file

@ -4,6 +4,10 @@ import rootReducer from '../reducers/index';
import effects from '../utils/effects';
import * as plugins from '../utils/plugins';
import writeMiddleware from './write-middleware';
import {HyperState, HyperThunkDispatch} from '../hyper';
export default () =>
createStore(rootReducer, applyMiddleware(thunk, plugins.middleware, thunk, writeMiddleware, effects));
createStore(
rootReducer,
applyMiddleware<HyperThunkDispatch, HyperState>(thunk, plugins.middleware, thunk, writeMiddleware, effects)
);

View file

@ -1,9 +1,10 @@
import terms from '../terms';
import {Middleware} from 'redux';
// the only side effect we perform from middleware
// is to write to the react term instance directly
// to avoid a performance hit
export default () => next => action => {
const writeMiddleware: Middleware = () => next => action => {
if (action.type === 'SESSION_PTY_DATA') {
const term = terms[action.uid];
if (term) {
@ -12,3 +13,5 @@ export default () => next => action => {
}
next(action);
};
export default writeMiddleware;

View file

@ -1,3 +1,5 @@
import Term from './components/term';
// react Term components add themselves
// to this object upon mounting / unmounting
// this is to allow imperative access to the
@ -5,5 +7,5 @@
// optimization for the most common action
// within the system
const terms = {};
const terms: Record<string, Term> = {};
export default terms;

View file

@ -6,8 +6,8 @@
* defer or add to existing side effects at will
* as the result of an action being triggered.
*/
export default () => (next: (arg0: any) => any) => (action: {effect: () => void}) => {
import {Middleware} from 'redux';
const effectsMiddleware: Middleware = () => next => action => {
const ret = next(action);
if (action.effect) {
action.effect();
@ -15,3 +15,4 @@ export default () => (next: (arg0: any) => any) => (action: {effect: () => void}
}
return ret;
};
export default effectsMiddleware;

View file

@ -12,7 +12,7 @@ import ReactDOM from 'react-dom';
import Notification from '../components/notification';
import notify from './notify';
import {hyperPlugin, IUiReducer, ISessionReducer, ITermGroupReducer, HyperState} from '../hyper';
import {Dispatch} from 'redux';
import {Dispatch, Middleware} from 'redux';
// remote interface to `../plugins`
const plugins = remote.require('./plugins') as typeof import('../../app/plugins');
@ -539,8 +539,8 @@ export function decorateSessionsReducer(fn: ISessionReducer) {
}
// redux middleware generator
export const middleware = (store: any) => (next: any) => (action: any) => {
const nextMiddleware = (remaining: any[]) => (action_: any) =>
export const middleware: Middleware = store => next => action => {
const nextMiddleware = (remaining: Middleware[]) => (action_: any) =>
remaining.length ? remaining[0](store)(nextMiddleware(remaining.slice(1)))(action_) : next(action_);
nextMiddleware(middlewares)(action);
};

View file

@ -110,6 +110,7 @@
"plist": "3.0.1",
"prettier": "1.19.1",
"proxyquire": "2.1.3",
"redux-devtools-extension": "2.13.8",
"spectron": "9.0.0",
"style-loader": "1.1.2",
"ts-node": "8.5.4",

View file

@ -7103,6 +7103,11 @@ redent@^2.0.0:
indent-string "^3.0.0"
strip-indent "^2.0.0"
redux-devtools-extension@2.13.8:
version "2.13.8"
resolved "https://registry.yarnpkg.com/redux-devtools-extension/-/redux-devtools-extension-2.13.8.tgz#37b982688626e5e4993ff87220c9bbb7cd2d96e1"
integrity sha512-8qlpooP2QqPtZHQZRhx3x3OP5skEV1py/zUdMY28WNAocbafxdG2tRD1MWE7sp8obGMNYuLWanhhQ7EQvT1FBg==
redux-thunk@2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"