-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96c3eaa
commit bb1c91d
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { select, race, put, take } from 'redux-saga/effects'; | ||
|
||
export const createSyncedActions = (actionsObject) => { | ||
const syncedActionsObject = {}; | ||
|
||
function* syncedAction(sa, actionObject, force) { | ||
const state = yield select(sa.state); | ||
if (!state[sa.success] || force) { | ||
if (!state[sa.loading]) yield put(actionObject); | ||
yield race(sa.race.map((a) => take(a))); | ||
const newState = yield select(sa.state); | ||
if (newState[sa.success]) return true; | ||
if (newState[sa.failure]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
Object.keys(actionsObject).forEach((key) => { | ||
syncedActionsObject[key] = syncedAction.bind(null, actionsObject[key]); | ||
}); | ||
|
||
return { | ||
putSync: function* (actionObject) { | ||
const type = actionObject.type; | ||
const action = syncedActionsObject[type]; | ||
if (action) yield syncedActionsObject[type](actionObject, false); | ||
else console.error(`no synced action defined for: ${type}`); | ||
}, | ||
putSyncForce: function* (actionObject) { | ||
const type = actionObject.type; | ||
const action = syncedActionsObject[type]; | ||
if (action) yield syncedActionsObject[type](actionObject, true); | ||
else console.error(`no synced action defined for: ${type}`); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters