-
Notifications
You must be signed in to change notification settings - Fork 115
/
SagaExtension.ts
63 lines (54 loc) · 1.75 KB
/
SagaExtension.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
52
53
54
55
56
57
58
59
60
61
62
63
import { default as createSagaMiddleware, SagaMiddleware } from "redux-saga";
import {
IExtension,
IItemManager,
getRefCountedManager,
IModuleManager,
} from "redux-dynamic-modules-core";
import { ISagaRegistration, ISagaModule } from "./Contracts";
import { getSagaManager } from "./SagaManager";
import { sagaEquals } from "./SagaComparer";
/**
* Get an extension that integrates saga with the store
* @param sagaContext The context to provide to the saga
*/
export function getSagaExtension<C>(
sagaContext?: C,
onError?: (error: Error) => void
): IExtension {
let sagaMonitor = undefined;
//@ts-ignore
if (process.env.NODE_ENV === "development" && typeof window !== "undefined") {
sagaMonitor = window["__SAGA_MONITOR_EXTENSION__"] || undefined;
}
// Setup the saga middleware
let sagaMiddleware: SagaMiddleware<C> = createSagaMiddleware<any>({
context: sagaContext,
sagaMonitor,
onError,
});
let _sagaManager: IItemManager<
ISagaRegistration<any>
> = getRefCountedManager(getSagaManager(sagaMiddleware), sagaEquals);
return {
middleware: [sagaMiddleware],
onModuleManagerCreated: (moduleManager: IModuleManager) => {
if (sagaContext) {
sagaContext["moduleManager"] = moduleManager;
}
},
onModuleAdded: (module: ISagaModule<any>): void => {
if (module.sagas) {
_sagaManager.add(module.sagas);
}
},
onModuleRemoved: (module: ISagaModule<any>): void => {
if (module.sagas) {
_sagaManager.remove(module.sagas);
}
},
dispose: () => {
_sagaManager.dispose();
},
};
}