redux-saga/effects#take JavaScript Examples
The following examples show how to use
redux-saga/effects#take.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: stream.js From haven with MIT License | 6 votes |
function* fetchStream(action) {
try {
const { filter, sort, hashtag, appending, peerID } = action.payload;
const offset = yield select(getFeedCount(filter, sort));
const query = { offset: appending ? offset : 0, ranking: sort ? sort : undefined };
if (!StreamClient.initDone) {
yield take(streamActions.streamInitFinished);
}
let allFetched = false;
let history;
if (filter === 'hashtag') {
history = yield StreamClient.fetchHashtag(query, hashtag);
} else if (filter === 'user' || filter === 'local') {
history = yield StreamClient.fetchMyFeed(query, peerID, filter);
} else {
history = yield StreamClient.fetchStream(query);
}
let results = history.results;
if (results.length > 0) {
allFetched = results.length < STREAM_FEED_PAGE_SIZE;
const { repost, ...data } = parseFeedData(results);
if (appending) {
yield put({ type: streamActions.appendStream, payload: {filter, sort, ...data, allFetched} });
} else {
yield put({ type: streamActions.prependStream, payload: {filter, sort, ...data, allFetched} });
}
if (repost.length > 0) {
yield put({ type: streamActions.fetchFeedItems, payload: repost });
}
} else {
yield put({ type: streamActions.finishedFetchRequest })
}
} catch (err) {
yield put({ type: streamActions.finishedFetchRequest })
console.log('fetch stream saga failure', err);
}
}
Example #2
Source File: stream.js From haven with MIT License | 6 votes |
function* streamSocketConnect() {
try {
const readOnlyToken = yield StreamClient.getReadonlyToken('user', 'all');
const channel = yield call(subscribeStream, 'user', 'all', readOnlyToken);
while (true) {
const action = yield take(channel);
yield put(action);
}
} catch (err) {
}
}
Example #3
Source File: stream.js From haven with MIT License | 6 votes |
function* streamNotificationConnect() {
try {
const readOnlyToken = yield StreamClient.getReadonlyToken('notifications', StreamClient.user.id);
const channel = yield call(subscribeNotificationStream, readOnlyToken);
while (true) {
const action = yield take(channel);
yield put(action);
}
} catch (err) {
}
}
Example #4
Source File: socket.js From haven with MIT License | 6 votes |
function* read(socket) {
try {
const channel = yield call(subscribe, socket);
while (true) {
const action = yield take(channel);
yield put(action);
}
} catch (err) {
console.error(err);
}
}
Example #5
Source File: index.js From stayaway-app with European Union Public License 1.2 | 6 votes |
export function* enableExposureNotifications() {
yield put(accountActions.startTracing());
const { payload } = yield take(accountTypes.START_TRACING_RESULT);
if (payload !== TRACING_RESULTS.SUCCESS) {
if (Platform.OS === 'ios') {
Linking.openURL('app-settings://');
}
}
}
Example #6
Source File: logger.js From jc-calendar with MIT License | 6 votes |
export function* watchAndLog() {
while (true) {
const action = yield take('*');
const stateAfter = yield select();
console.groupCollapsed('Ran action', action.type);
console.log({ action, stateAfter });
console.groupEnd();
}
}
Example #7
Source File: forecast.js From jc-calendar with MIT License | 6 votes |
export function* watchGetForecast() {
let task;
while (true) {
const action = yield take(forecastUIActions.GET_FORECAST);
if (task) {
yield cancel(task);
}
task = yield fork(getForecastDebounced, action);
}
}
Example #8
Source File: index.js From stayaway-app with European Union Public License 1.2 | 6 votes |
export function* watchTracingStatus() {
// Set event listener value
const channel = eventChannel((emitter) => {
TracingManager.addUpdateEventListener(emitter);
return () => TracingManager.removeUpdateEventListener();
});
try {
yield put(accountActions.tracingStatusListenerRegistered());
while (true) {
const status = yield take(channel);
console.log(status);
// Update redux store
yield put(accountActions.updateStatus(status));
}
} catch (error) {
// Do nothing
console.log(error);
}
}
Example #9
Source File: saga.js From real-frontend with GNU General Public License v3.0 | 6 votes |
/**
*
*/
function* cacheFetchSequentialRequest(buffer, action) {
const channel = yield actionChannel(action, buffer)
while (true) {
const req = yield take(channel)
try {
const data = yield retry(3, 10000, handleCacheFetchRequest, req.payload)
yield put(actions.cacheFetchSuccess({ data }))
} catch (error) {
yield put(actions.cacheFetchFailure({ message: error.message }))
}
}
}
Example #10
Source File: SagaManager.js From movies with MIT License | 6 votes |
function createAbortableSaga(saga) {
if (process.env.NODE_ENV === 'development') {
return function* main() {
const sagaTask = yield fork(saga);
yield take(CANCEL_SAGAS_HMR);
yield cancel(sagaTask);
};
}
return saga;
}
Example #11
Source File: index.js From stayaway-app with European Union Public License 1.2 | 5 votes |
function* watchAppStateChange() {
const channel = eventChannel((emitter) => {
AppState.addEventListener('change', emitter);
return () => AppState.removeEventListener('change', emitter);
});
try {
let previousState = 'unknown';
while (true) {
const nextState = yield take(channel);
const onboarding = yield select(isOnboarding);
if (! onboarding && previousState !== nextState) {
if (nextState === 'active') {
const isProtectorOpen = yield select(isProtectorModalOpen);
if (isProtectorOpen) {
yield delay(200);
yield put(modalsActions.closeProtectorModal());
yield take(modalsTypes.PROTECTOR_MODAL_CLOSED);
}
try {
if (! Configuration.UI) {
yield call(TracingManager.sync);
// Get status
const status = yield call(TracingManager.getStatus);
yield put(accountActions.updateStatus(status));
}
} catch (error) {
// Sync error. Probably exposure check limit reached.
console.log(error);
}
} else if (nextState === 'inactive') {
const isProtectorOpen = yield select(isProtectorModalOpen);
if (! isProtectorOpen) {
yield put(modalsActions.openProtectorModal());
yield take(modalsTypes.PROTECTOR_MODAL_OPEN);
}
}
}
previousState = nextState;
}
} finally {
channel.close();
}
}
Example #12
Source File: index.js From stayaway-app with European Union Public License 1.2 | 5 votes |
export function* switchTracing() {
const tracingEnabled = yield select(isTracingEnabled);
if (tracingEnabled) {
yield put(accountActions.stopTracing());
yield take(accountTypes.STOP_TRACING_RESULT);
yield put(accountActions.setTracingEnabled(false));
yield put(accountActions.setErrors([]));
return;
}
// Ensure permissions are all granted
yield put(permissionsActions.checkAllPermissions());
const { payload: allPermissionsGranted } = yield take(permissionsTypes.CHECK_ALL_PERMISSIONS_RESULT);
if (! allPermissionsGranted) {
// Request permissions
yield put(permissionsActions.requestAllPermissions());
const { payload: allPermissionsAllowed } = yield take(permissionsTypes.REQUEST_ALL_PERMISSIONS_RESULT);
if (! allPermissionsAllowed) {
if (Platform.OS === 'android') {
// Show alert
Alert.alert(
i18n.translate('common.dialogs.permissions.title'),
i18n.translate('common.dialogs.permissions.description'),
[
{
text: i18n.translate('common.actions.ok'),
style: 'default',
},
],
);
}
// Set tracing deactivated
yield put(accountActions.setTracingEnabled(false));
return;
}
}
// Start tracing manager
yield put(accountActions.startTracing());
const { payload } = yield take(accountTypes.START_TRACING_RESULT);
if (payload === TRACING_RESULTS.SUCCESS) {
// Set tracing activated
yield put(accountActions.setTracingEnabled(true));
} else {
yield put(accountActions.setTracingEnabled(false));
}
}
Example #13
Source File: index.js From stayaway-app with European Union Public License 1.2 | 5 votes |
export function* startTracing() {
if (Configuration.UI) {
yield put(accountActions.setTracingEnabled(true));
yield put(accountActions.startTracingResult(TRACING_RESULTS.SUCCESS));
yield take(accountTypes.STOP_TRACING);
yield put(accountActions.stopTracingResult(TRACING_RESULTS.SUCCESS));
return;
}
const watcher = yield fork(watchTracingStatus);
// Wait for listener to registered
yield take(accountTypes.TRACING_STATUS_LISTENER_REGISTERED);
try {
const result = yield call(TracingManager.start);
if (result === GAEN_RESULTS.EN_CANCELLED) {
if (Platform.OS === 'android') {
// Show alert
Alert.alert(
i18n.translate('common.dialogs.gaen.enable.title'),
i18n.translate('common.dialogs.gaen.enable.description'),
[
{
text: i18n.translate('common.actions.ok'),
style: 'default',
},
],
);
}
yield put(accountActions.startTracingResult(TRACING_RESULTS.GAEN));
yield cancel(watcher);
return;
}
try {
yield call(TracingManager.sync);
// Get status
const status = yield call(TracingManager.getStatus);
yield put(accountActions.updateStatus(status));
} catch (error) {
// Sync error. Probably exposure check limit reached.
// Clear errors
yield put(accountActions.setErrors([]));
console.log(error);
}
yield put(accountActions.startTracingResult(TRACING_RESULTS.SUCCESS));
} catch (error) {
console.log(error);
yield put(accountActions.startTracingResult(TRACING_RESULTS.FAILED));
yield cancel(watcher);
return;
}
try {
yield take(accountTypes.STOP_TRACING);
yield cancel(watcher);
yield call(TracingManager.stop);
yield put(accountActions.stopTracingResult(TRACING_RESULTS.SUCCESS));
} catch (error) {
console.log(error);
yield put(accountActions.stopTracingResult('ERROR'));
}
}
Example #14
Source File: index.js From stayaway-app with European Union Public License 1.2 | 5 votes |
export function* setupNewAccount() {
yield put(accountActions.setupNewAccountPending());
// Set default state
yield put(accountActions.updateStatus({
lastSyncDate: 0,
infectionStatus: 0,
exposureDays: [],
errors: [],
}));
// Check if permissions are all granted
yield put(permissionsActions.checkAllPermissions());
const { payload: allPermissionsGranted } = yield take(permissionsTypes.CHECK_ALL_PERMISSIONS_RESULT);
if (! allPermissionsGranted) {
// Request permissions
yield put(permissionsActions.requestAllPermissions());
yield take(permissionsTypes.REQUEST_ALL_PERMISSIONS_RESULT);
}
// Start tracing manager
if (Configuration.UI) {
yield put(accountActions.updateStatus({
lastSyncDate: Moment().toJSON(),
infectionStatus: 0,
exposureDays: [],
errors: [],
}));
}
yield put(accountActions.startTracing());
const { payload } = yield take(accountTypes.START_TRACING_RESULT);
if (payload === TRACING_RESULTS.SUCCESS) {
// Set tracing activated
yield put(accountActions.setTracingEnabled(true));
} else if (payload === TRACING_RESULTS.GAEN) {
yield put(accountActions.setTracingEnabled(false));
// Add tracing error
yield put(accountActions.setErrors([ERRORS[Platform.OS].GAEN_UNEXPECTEDLY_DISABLED]));
} else {
yield put(accountActions.setTracingEnabled(false));
}
// Update new account redux
yield put(accountActions.setSignUpDate(Moment().toJSON()));
// Delay 1 second
yield delay(1000);
// Navigate to home
yield put(onboardingActions.setOnboarding(false));
yield put(accountActions.setupNewAccountDone());
NavigationService.navigate(AppRoutes.APP);
}
Example #15
Source File: saga.js From hackchat-client with Do What The F*ck You Want To Public License | 5 votes |
export default function* communicationProviderSaga() {
const client = yield call(initWebsocket);
// Channel Actions
yield takeLatest(START_JOIN, (action) =>
hcClient.join(action.username, action.password, action.channel),
);
yield takeLatest(SEND_CHAT, (action) =>
hcClient.say(action.channel, action.message),
);
yield takeLatest(ENABLE_CAPTCHA, (action) =>
hcClient.enableCaptcha(action.channel),
);
yield takeLatest(DISABLE_CAPTCHA, (action) =>
hcClient.disableCaptcha(action.channel),
);
yield takeLatest(LOCK_CHANNEL, (action) =>
hcClient.lockChannel(action.channel),
);
yield takeLatest(UNLOCK_CHANNEL, (action) =>
hcClient.unlockChannel(action.channel),
);
// User Actions
yield takeLatest(INVITE_USER, (action) =>
action.user.sendInvite(action.channel),
);
yield takeLatest(WHISPER_USER, (action) =>
hcClient.say(action.channel, 'I wish the developer wasnt so lazy. . .'),
);
yield takeLatest(IGNORE_USER, (action) => action.user.toggleBlock());
yield takeLatest(KICK_USER, (action) => action.user.kick(action.channel));
yield takeLatest(BAN_USER, (action) => action.user.ban(action.channel));
yield takeLatest(MUTE_USER, (action) => action.user.mute(action.channel));
yield takeLatest(UNMUTE_USER, (action) => action.user.unmute(action.channel));
while (true) {
const action = yield take(client);
yield put(action);
}
}
Example #16
Source File: forecast.js From jc-calendar with MIT License | 5 votes |
export function* raceWithResetForecast(effect) {
return yield race({
result: effect,
reset: take(forecastUIActions.RESET_FORECAST),
});
}
Example #17
Source File: auth.saga.js From React-Native-Boilerplate with MIT License | 5 votes |
function* watchSignup() {
while (true) {
const action = yield take(REGISTER_REQUEST);
yield* signupSaga(action);
}
}
Example #18
Source File: auth.saga.js From React-Native-Boilerplate with MIT License | 5 votes |
function* watchLogin() {
while (true) {
const action = yield take(LOGIN_REQUEST);
yield* loginSaga(action);
}
}
Example #19
Source File: index.js From stayaway-app with European Union Public License 1.2 | 4 votes |
export function* submitDiagnosis({ payload: code }) {
// Open loading modal
yield put(accountActions.submitDiagnosisPending());
yield put(modalsActions.openLoadingModal());
yield take(modalsTypes.LOADING_MODAL_OPEN);
try {
if (Configuration.UI) {
// Delay 1.5 seconds
yield delay(1500);
// Mark as infected
yield put(accountActions.setInfectionStatus(INFECTION_STATUS.INFECTED));
// Stop tracing
yield put(accountActions.setTracingEnabled(false));
yield put(accountActions.submitDiagnosisDone());
yield put(modalsActions.closeLoadingModal());
yield take(modalsTypes.LOADING_MODAL_CLOSED);
return;
}
// Submit exposed code
const result = yield call(TracingManager.exposed, code);
if (result === GAEN_RESULTS.EN_CANCELLED) {
if (Platform.OS === 'android') {
// Show alert
Alert.alert(
i18n.translate('common.dialogs.gaen.export.title'),
i18n.translate('common.dialogs.gaen.export.description'),
[
{
text: i18n.translate('common.actions.ok'),
style: 'default',
},
],
);
}
yield put(accountActions.submitDiagnosisDone());
yield put(modalsActions.closeLoadingModal());
yield take(modalsTypes.LOADING_MODAL_CLOSED);
return;
}
// Update status
yield put(accountActions.setInfectionStatus(INFECTION_STATUS.INFECTED));
yield take(accountTypes.UPDATE_STATUS_RESULT);
// Stop tracing
yield call(TracingManager.removeUpdateEventListener);
yield put(accountActions.setTracingEnabled(false));
yield put(accountActions.submitDiagnosisDone());
yield put(modalsActions.closeLoadingModal());
yield take(modalsTypes.LOADING_MODAL_CLOSED);
} catch (error) {
console.log(error);
yield put(accountActions.submitDiagnosisDone());
yield put(modalsActions.closeLoadingModal());
yield take(modalsTypes.LOADING_MODAL_CLOSED);
if (error.message === ERRORS[Platform.OS].UNKNOWN_HOST_EXCEPTION.toString()) {
yield put(accountActions.submitDiagnosisError(i18n.translate('common.errors.network')));
yield put(modalsActions.openNetworkModal());
yield take(modalsTypes.NETWORK_MODAL_OPEN);
} else if (error.message === ERRORS[Platform.OS].INVALID_CODE_EXCEPTION.toString()) {
yield put(accountActions.submitDiagnosisError(i18n.translate('common.errors.submit.code')));
yield put(modalsActions.openInvalidCodeModal());
yield take(modalsTypes.INVALID_CODE_MODAL_OPEN);
} else {
yield put(accountActions.submitDiagnosisError(i18n.translate('common.errors.general')));
yield put(modalsActions.openServerErrorModal());
yield take(modalsTypes.SERVER_ERROR_MODAL_OPEN);
}
}
}
Example #20
Source File: saga.js From react-redux-saga-sample with MIT License | 4 votes |
createAuthSaga = (options: {
loginActions?: Object,
reducerKey: string,
OAUTH_URL: string,
OAUTH_CLIENT_ID: string,
OAUTH_CLIENT_SECRET: string,
}) => {
const {
loginActions,
OAUTH_URL,
OAUTH_CLIENT_ID,
OAUTH_CLIENT_SECRET,
reducerKey,
} = options;
const getAuth = state => state[reducerKey];
function* RefreshToken(refresh_token) {
try {
const params = {
refresh_token,
client_id: OAUTH_CLIENT_ID,
client_secret: OAUTH_CLIENT_SECRET,
grant_type: "refresh_token",
};
const { data: token } = yield call(axios.post, OAUTH_URL, params);
yield put(authRefreshSuccess(token));
return true;
} catch (error) {
if (error.response) {
if (error.response.status === 401) {
yield put(authInvalidError(error.response));
} else {
yield put(authRefreshError(error.response));
}
} else {
yield put(authRefreshError(error));
}
return false;
}
}
function* RefreshLoop() {
const maxRetries = 5;
let retries = 0;
while (true) {
const { expires_in, created_at, refresh_token } = yield select(getAuth);
// if the token has expired, refresh it
if (
expires_in !== null &&
created_at !== null &&
tokenHasExpired({ expires_in, created_at })
) {
const refreshed = yield call(RefreshToken, refresh_token);
// if the refresh succeeded set the retires to 0
// if the refresh failed, log a failure
if (refreshed) {
// if the token has been refreshed, and their had been retries
// let the user know everything is okay
if (retries > 0) {
// @TODO add hook
}
retries = 0;
} else {
retries = retries + 1;
}
if (retries > 0 && retries < maxRetries) {
// @TODO add hook
}
if (retries === maxRetries) {
// @TODO add hook
}
}
// check again in 5 seconds
// this will also replay failed refresh attempts
yield delay(5000);
}
}
function* Authorize(action) {
try {
const { onSuccess, payload } = action;
const params = {
...payload,
client_id: OAUTH_CLIENT_ID,
client_secret: OAUTH_CLIENT_SECRET,
};
const { data: token } = yield call(axios.post, OAUTH_URL, params);
yield put(authLogin(token));
if (onSuccess) {
onSuccess();
}
} catch (error) {
const { onError } = action;
if (onError) {
onError(error.response ? error.response.data : error);
}
if (error.response) {
yield put(authLoginError(error.response.data));
} else {
yield put(authLoginError(error));
}
}
}
function* Authentication(): Generator<*, *, *> {
while (true) {
const { loggedIn } = yield select(getAuth);
var authorizeTask = null;
// if the users is logged in, we can skip over this bit
if (!loggedIn) {
// wait for a user to request to login
// or any custom login actions
const actions = yield race({
login: take(AUTH_LOGIN_REQUEST),
...loginActions,
});
if (actions.login) {
// in the background, run a task to log them in
authorizeTask = yield fork(Authorize, actions.login);
}
} else {
// dispatch an action so we know the user is back into an
// authenticated state
yield put(authRestore());
}
// wait for...
// the user to logout (AUTH_LOGOUT_REQUEST)
// OR an error to occur during login (AUTH_LOGIN_ERROR)
// OR the user to become unauthorized (AUTH_INVALID_ERROR)
// but while they are logged in, begin the refresh token loop
const actions = yield race({
logout: take(AUTH_LOGOUT_REQUEST),
loginError: take(AUTH_LOGIN_ERROR),
unauthorized: take(AUTH_INVALID_ERROR),
refresh: call(RefreshLoop),
});
// cancel the authorizeTask task if it's running and exists
if (authorizeTask !== null) {
yield cancel(authorizeTask);
}
// finally log the user out
yield put(authLogout());
}
}
return Authentication;
}
Example #21
Source File: index.js From stayaway-app with European Union Public License 1.2 | 4 votes |
export function* startup() {
try {
// Check if has previous language configuration
const hasLanguage = yield call([Storage, 'hasItem'], 'language');
if (hasLanguage) {
const languageTag = yield call([Storage, 'getItem'], 'language');
const language = i18n.setI18nConfig(languageTag);
yield put(accountActions.setLanguage(language));
} else {
const language = i18n.setDefaultI18nConfig();
yield put(accountActions.setLanguage(language));
}
// Check if has previous theme configuration
const hasTheme = yield call([Storage, 'hasItem'], 'theme');
if (hasTheme) {
const theme = yield call([Storage, 'getItem'], 'theme');
yield put(accountActions.setTheme(theme));
}
// Check if OS version is supported
if (Platform.OS === 'ios') {
const isSupported = yield call(TracingManager.isENSupported);
if (! isSupported) {
// Navigate to unsupported screen
yield put(startupActions.setUnsupported(true));
return;
}
}
// Check if has previous configuration
const hasSignUpDate = yield call([Storage, 'hasItem'], 'signup_date');
if (! hasSignUpDate) {
// Navigate to onboarding
yield put(onboardingActions.setOnboarding(true));
return;
}
// Check if tracing was enabled
const isTracingEnabled = yield call(TracingManager.isTracingEnabled);
// Get previous stored state
const signUpDate = yield call([Storage, 'getItem'], 'signup_date', '');
const status = JSON.parse(yield call([Storage, 'getItem'], 'status', '{}'));
// Update account redux
yield put(accountActions.setSignUpDate(signUpDate));
yield put(accountActions.updateStatus(status));
// Navigate to home
yield put(onboardingActions.setOnboarding(false));
// Check if is UI mode
if (Configuration.UI) {
const tracing = yield call([Storage, 'getItem'], 'tracing_enabled', 'false');
yield put(accountActions.setTracingEnabled(tracing === 'true'));
yield put(accountActions.startTracing());
return;
}
// Check if tracing was enabled
if (! isTracingEnabled) {
// Set tracing deactivated
yield put(accountActions.setTracingEnabled(false));
return;
}
yield put(accountActions.startTracing());
const { payload } = yield take(accountTypes.START_TRACING_RESULT);
if (payload === TRACING_RESULTS.SUCCESS) {
// Set tracing activated
yield put(accountActions.setTracingEnabled(true));
} else if (payload === TRACING_RESULTS.GAEN) {
yield put(accountActions.setTracingEnabled(false));
// Add tracing error
yield put(accountActions.setErrors([ERRORS[Platform.OS].GAEN_UNEXPECTEDLY_DISABLED]));
} else {
yield put(accountActions.setTracingEnabled(false));
}
} finally {
// Set app launched
yield put(startupActions.setAppLaunched(true));
}
}