forked from cvat-ai/cvat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcvat-store.ts
51 lines (41 loc) · 1.43 KB
/
cvat-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (C) 2020-2022 Intel Corporation
// Copyright (C) 2023 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT
import thunk from 'redux-thunk';
import {
createStore, applyMiddleware, Store, Reducer,
} from 'redux';
import { createLogger } from 'redux-logger';
import { isDev } from 'utils/environment';
import { CombinedState } from 'reducers';
const logger = createLogger({
predicate: isDev,
collapsed: true,
});
const middlewares = [thunk, logger];
let store: Store | null = null;
export default function createCVATStore(createRootReducer: () => Reducer): void {
let appliedMiddlewares = applyMiddleware(...middlewares);
if (isDev()) {
// eslint-disable-next-line @typescript-eslint/no-var-requires, global-require
const { composeWithDevTools } = require('redux-devtools-extension');
appliedMiddlewares = composeWithDevTools(appliedMiddlewares);
}
store = createStore(createRootReducer(), appliedMiddlewares);
store.subscribe(() => {
const state = (store as Store).getState() as CombinedState;
for (const plugin of Object.values(state.plugins.current)) {
const { globalStateDidUpdate } = plugin;
if (globalStateDidUpdate) {
globalStateDidUpdate(state);
}
}
});
}
export function getCVATStore(): Store<CombinedState> {
if (store) {
return store;
}
throw new Error('First create a store');
}