Skip to content

Commit

Permalink
add tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Yarigo committed Jun 29, 2021
1 parent c1439e6 commit e84d1e6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
18 changes: 9 additions & 9 deletions packages/redux-dynamic-modules-saga/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Run

```
npm install redux-dynamic-modules-saga
npm install @yarigo/redux-dynamic-modules-saga
```

or

```
yarn add redux-dynamic-modules-saga
yarn add @yarigo/redux-dynamic-modules-saga
```

## Usage
Expand Down Expand Up @@ -39,12 +39,12 @@ import { getSagaExtension } from "redux-dynamic-modules-saga";
import { getUsersModule } from "./usersModule";

const store: IModuleStore<IState> = createStore(
{
initialState: {},
enhancers: [],
extensions: [getSagaExtension({} /* saga context */)],
},
getUsersModule()
/* ...any additional modules */
{
initialState: {},
enhancers: [],
extensions: [getSagaExtension({} /* saga context */)],
},
getUsersModule()
/* ...any additional modules */
);
```
6 changes: 3 additions & 3 deletions packages/redux-dynamic-modules-saga/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "redux-dynamic-modules-saga",
"name": "@yarigo/redux-dynamic-modules-saga",
"version": "5.2.3",
"description": "Saga Extension for redux-dynamic-modules",
"description": "Saga Extension for redux-dynamic-modules (fork https://github.com/microsoft/redux-dynamic-modules)",
"repository": {
"type": "github",
"url": "https://github.com/Microsoft/redux-dynamic-modules"
"url": "https://github.com/yarigo/redux-dynamic-modules"
},
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
25 changes: 19 additions & 6 deletions packages/redux-dynamic-modules-saga/src/SagaExtension.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import { default as createSagaMiddleware, SagaMiddleware } from "redux-saga";
import {
default as createSagaMiddleware,
SagaMiddleware,
Task,
} from "redux-saga";
import {
IExtension,
IItemManager,
getRefCountedManager,
IModuleManager,
IMap,
} from "redux-dynamic-modules-core";
import { ISagaRegistration, ISagaModule } from "./Contracts";
import { getSagaManager } from "./SagaManager";
import { getSagaManager, ISagaItemManager } from "./SagaManager";
import { sagaEquals } from "./SagaComparer";

export interface ISagaExtension extends IExtension {
tasks: IMap<ISagaRegistration<any>, Task>;
}

/**
* 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 {
): ISagaExtension {
let sagaMonitor = undefined;

//@ts-ignore
if (process.env.NODE_ENV === "development" && typeof window !== "undefined") {
if (
process.env.NODE_ENV === "development" &&
typeof window !== "undefined"
) {
sagaMonitor = window["__SAGA_MONITOR_EXTENSION__"] || undefined;
}

Expand All @@ -31,11 +42,13 @@ export function getSagaExtension<C>(
onError,
});

let _sagaManager: IItemManager<
let _sagaManager: ISagaItemManager<
ISagaRegistration<any>
> = getRefCountedManager(getSagaManager(sagaMiddleware), sagaEquals);

return {
tasks: _sagaManager.tasks,

middleware: [sagaMiddleware],

onModuleManagerCreated: (moduleManager: IModuleManager) => {
Expand Down
9 changes: 7 additions & 2 deletions packages/redux-dynamic-modules-saga/src/SagaManager.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { ISagaRegistration, ISagaWithArguments } from "./Contracts";
import { SagaMiddleware, Task } from "redux-saga";
import { sagaEquals } from "./SagaComparer";
import { IItemManager, getMap } from "redux-dynamic-modules-core";
import { IItemManager, getMap, IMap } from "redux-dynamic-modules-core";

export interface ISagaItemManager<T> extends IItemManager<T> {
tasks: IMap<ISagaRegistration<any>, Task>;
}

/**
* Creates saga items which can be used to start and stop sagas dynamically
*/
export function getSagaManager(
sagaMiddleware: SagaMiddleware<any>
): IItemManager<ISagaRegistration<any>> {
): ISagaItemManager<ISagaRegistration<any>> {
const tasks = getMap<ISagaRegistration<any>, Task>(sagaEquals);

return {
tasks,
getItems: (): ISagaRegistration<any>[] => [...tasks.keys],
add: (sagas: ISagaRegistration<any>[]) => {
if (!sagas) {
Expand Down

0 comments on commit e84d1e6

Please sign in to comment.