Repository URL to install this package:
|
Version:
3.7.3 ▾
|
import { delay } from 'redux-saga';
import { race, take, call, put, spawn } from 'redux-saga/effects';
import { ActionTypes } from '../actions/abActions';
export function* waitForChooseExperiment(internalAction, abTestName) {
let payload;
// Busy waiting to take actually get the desired action
// since there might be more than one action with the same type
while (true) {
payload = (yield take(ActionTypes.CHOOSE_EXPERIMENT)).payload;
if (payload.name === abTestName) {
break;
}
}
yield put(internalAction);
}
/**
* Returns a generator which calls a saga if the sepecific experiment is loaded or the timeout runs out.
*
* @param {string} abTest - The name of the A/B test, the saga should wait for
* @param {number} timeout - Maximal timeout, default is 1000ms
*/
export function waitForOptimize(abTest, timeout = 1000) {
// Create an action which then can be used internally
const internalActionType = `@doodle/ab/internal/DEPEND_ON_${abTest}`;
const internalAction = { type: internalActionType };
return function*(saga, ...args) {
// If abTest is undefined, just call the wrapped saga
if (abTest) {
// Here we fork the saga to not block the generator.
// This enables us to afterwards take the internal action
yield spawn(waitForChooseExperiment, internalAction, abTest);
yield race({
action: take(internalActionType),
timeout: delay(timeout),
});
}
return yield call(saga, ...args);
};
}