Add inject utils

This commit is contained in:
Jannis Mattheis 2018-10-21 15:45:10 +02:00
parent 95846da7b7
commit bbb344be72
1 changed files with 34 additions and 0 deletions

34
ui/src/inject.tsx Normal file
View File

@ -0,0 +1,34 @@
import * as React from 'react';
import {UserStore} from './stores/UserStore';
import {SnackManager} from './stores/SnackManager';
import {MessagesStore} from './stores/MessagesStore';
import {CurrentUser} from './stores/CurrentUser';
import {ClientStore} from './stores/ClientStore';
import {AppStore} from './stores/AppStore';
import {inject as mobxInject, Provider} from 'mobx-react';
import {WebSocketStore} from './stores/WebSocketStore';
export interface StoreMapping {
userStore: UserStore;
snackManager: SnackManager;
messagesStore: MessagesStore;
currentUser: CurrentUser;
clientStore: ClientStore;
appStore: AppStore;
wsStore: WebSocketStore;
}
export type AllStores = Extract<keyof StoreMapping, string>;
export type Stores<T extends AllStores> = Pick<StoreMapping, T>;
export const inject = <I extends AllStores>(...stores: I[]) => {
return <P extends {}>(
node: React.ComponentType<P>
): React.ComponentType<Pick<P, Exclude<keyof P, I>>> => {
return mobxInject(...stores)(node);
};
};
export const InjectProvider: React.SFC<{stores: StoreMapping}> = ({children, stores}) => {
return <Provider {...stores}>{children}</Provider>;
};