-
Notifications
You must be signed in to change notification settings - Fork 31
Added COUNTER mode for saga injectors #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,12 +2,19 @@ import invariant from 'invariant'; | |||||
import isEmpty from 'lodash/isEmpty'; | ||||||
import isFunction from 'lodash/isFunction'; | ||||||
import isString from 'lodash/isString'; | ||||||
import isNumber from 'lodash/isNumber'; | ||||||
import conformsTo from 'lodash/conformsTo'; | ||||||
|
||||||
import checkStore from './checkStore'; | ||||||
import { DAEMON, ONCE_TILL_UNMOUNT, RESTART_ON_REMOUNT } from './constants'; | ||||||
import { | ||||||
DAEMON, | ||||||
ONCE_TILL_UNMOUNT, | ||||||
RESTART_ON_REMOUNT, | ||||||
COUNTER, | ||||||
COUNTER_PROP, | ||||||
} from './constants'; | ||||||
|
||||||
const allowedModes = [RESTART_ON_REMOUNT, DAEMON, ONCE_TILL_UNMOUNT]; | ||||||
const allowedModes = [RESTART_ON_REMOUNT, DAEMON, ONCE_TILL_UNMOUNT, COUNTER]; | ||||||
|
||||||
const checkKey = key => | ||||||
invariant( | ||||||
|
@@ -19,6 +26,7 @@ const checkDescriptor = descriptor => { | |||||
const shape = { | ||||||
saga: isFunction, | ||||||
mode: mode => isString(mode) && allowedModes.includes(mode), | ||||||
[COUNTER_PROP]: isNumber, | ||||||
}; | ||||||
invariant( | ||||||
conformsTo(descriptor, shape), | ||||||
|
@@ -33,6 +41,7 @@ export function injectSagaFactory(store, isValid) { | |||||
const newDescriptor = { | ||||||
...descriptor, | ||||||
mode: descriptor.mode || DAEMON, | ||||||
[COUNTER_PROP]: 0, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just call this
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, its updated as mentioned! |
||||||
}; | ||||||
const { saga, mode } = newDescriptor; | ||||||
|
||||||
|
@@ -50,16 +59,36 @@ export function injectSagaFactory(store, isValid) { | |||||
} | ||||||
} | ||||||
|
||||||
if (mode === COUNTER) { | ||||||
// COUNTER must be added if saga is done | ||||||
if (store.injectedSagas[key] === 'done') hasSaga = false; | ||||||
let oldCounterValue = 0; | ||||||
if (hasSaga) { | ||||||
oldCounterValue = Math.max( | ||||||
store.injectedSagas[key][COUNTER_PROP] || 0, | ||||||
0, | ||||||
); | ||||||
} | ||||||
newDescriptor[COUNTER_PROP] = oldCounterValue + 1; | ||||||
} | ||||||
|
||||||
if ( | ||||||
!hasSaga || | ||||||
(hasSaga && mode !== DAEMON && mode !== ONCE_TILL_UNMOUNT) | ||||||
(hasSaga && | ||||||
mode !== DAEMON && | ||||||
mode !== ONCE_TILL_UNMOUNT && | ||||||
mode !== COUNTER) | ||||||
) { | ||||||
/* eslint-disable no-param-reassign */ | ||||||
store.injectedSagas[key] = { | ||||||
...newDescriptor, | ||||||
task: store.runSaga(saga), | ||||||
}; | ||||||
/* eslint-enable no-param-reassign */ | ||||||
} else if (hasSaga && mode === COUNTER) { | ||||||
// increment num of sagas that wants to be injected | ||||||
/* eslint-disable no-param-reassign */ | ||||||
store.injectedSagas[key][COUNTER_PROP] = newDescriptor[COUNTER_PROP]; | ||||||
} | ||||||
}; | ||||||
} | ||||||
|
@@ -72,12 +101,31 @@ export function ejectSagaFactory(store, isValid) { | |||||
|
||||||
if (Reflect.has(store.injectedSagas, key)) { | ||||||
const descriptor = store.injectedSagas[key]; | ||||||
if (descriptor.mode && descriptor.mode !== DAEMON) { | ||||||
descriptor.task.cancel(); | ||||||
// Clean up in production; in development we need `descriptor.saga` for hot reloading | ||||||
if (process.env.NODE_ENV === 'production') { | ||||||
// Need some value to be able to detect `ONCE_TILL_UNMOUNT` sagas in `injectSaga` | ||||||
store.injectedSagas[key] = 'done'; // eslint-disable-line no-param-reassign | ||||||
|
||||||
if (descriptor.mode) { | ||||||
if (descriptor.mode === COUNTER) { | ||||||
descriptor[COUNTER_PROP] -= 1; | ||||||
|
||||||
// don't cancel task if still not 0 | ||||||
if (descriptor[COUNTER_PROP] > 0) return; | ||||||
} | ||||||
|
||||||
if (descriptor.mode !== DAEMON) { | ||||||
descriptor.task.cancel(); | ||||||
// Clean up in production; in development we need `descriptor.saga` for hot reloading | ||||||
if (process.env.NODE_ENV === 'production') { | ||||||
// Need some value to be able to detect `ONCE_TILL_UNMOUNT` sagas in `injectSaga` | ||||||
store.injectedSagas[key] = 'done'; // eslint-disable-line no-param-reassign | ||||||
} | ||||||
|
||||||
// Clean up in development when mode is COUNTER | ||||||
if ( | ||||||
process.env.NODE_ENV !== 'production' && | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to check if not production? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH i don't even remember, i will make example with issue and solution I might figure out why i did that a long time ago
|
||||||
descriptor.mode === COUNTER | ||||||
) { | ||||||
delete store.injectedSagas[key]; | ||||||
// only problem with this approach is that in development will have 'undefined' value | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for help, i have updated as u suggested
now its much better!