export function* getSearchResult({ payload }: ActionType<typeof bookmarkCreators.getSearchResult>): SagaIterator { try { if (!payload.searchKeyword) { yield put(bookmarkCreators.initBookmarkTrees()) return } const {options}: RootState = yield select(R.identity) const searchResult: Array<BookmarkInfo> = yield call(searchBookmarks, { query: payload.searchKeyword }) const isSearchTitleOnly = options[CST.OPTIONS.SEARCH_TARGET] === 1 const sortedPartialResult = R.compose( sortByTitle, R.slice(0, options[CST.OPTIONS.MAX_RESULTS] || 0), (result: Array<BookmarkInfo>) => { const filteredResult = result.filter(R.propEq('type', CST.BOOKMARK_TYPES.BOOKMARK)) if (isSearchTitleOnly) { return filteredResult.filter((bookmarkInfo: BookmarkInfo) => { return searchKeywordMatcher(payload.searchKeyword, bookmarkInfo.title) }) } return filteredResult } )(searchResult) const searchResultTrees = [ { children: sortedPartialResult, parent: simulateBookmark({ id: CST.SEARCH_RESULT_ID, type: CST.BOOKMARK_TYPES.FOLDER }) } ] yield put(bookmarkCreators.setBookmarkTrees(searchResultTrees)) } catch (err) { console.error(err) } }
export function* authSaga( history: History, authApi: IAuthApi, usersApi: IUsersApi ) { let token: IToken = yield call(authApi.getAuthToken) if (token) { token = yield call(login, history, authApi, usersApi, { type: 'token', token }) } else { yield put(actions.logoutSuccess()) } yield put(actions.authCheckFinished(token && token.value || null)) while (true) { if (!token) { const { payload: credentials } = yield take(actions.login.getType()) token = yield call(login, history, authApi, usersApi, credentials) if (token) { history.push('/') } } if (!token) { continue } let userLoggedOut = false while (!userLoggedOut) { const { expired } = yield race({ expired: delay(Math.max(token.exp - Date.now() - 30000, 0)), loggedOut: take(actions.logout.getType()), }) if (expired) { token = yield call(login, history, authApi, usersApi, { type: 'token', token }) } else { yield call(logout, authApi) userLoggedOut = true } } } }
function* deletePostDeal(action: any): any { try { // 发送请求查询 const result = yield call(() => { return client.del('/api/posts/' + action.payload.id, { params: {}, }); }); if (result.rt !== 1) { yield put({type: DEL_WIKI_SPECPOST_FAILED}); } else { yield put({type: DEL_WIKI_SPECPOST_SUCCESS, payload: result.data}); } } catch (e) { yield put({type: DEL_WIKI_SPECPOST_FAILED}); } }
return function* () { yield put(languagesActions.set([ { id: 'english', caption: 'English' }, { id: 'bulgarian', caption: 'Български (Bulgarian)' }, { id: 'czech', caption: 'čeština (Czech)' }, { id: 'russian', caption: 'Русский (Russian)' }, ])) yield fork(authSaga, history, authApi, usersApi) yield fork(router, history, routeSettings) }
export function* onModelStatusChanged({ modelData, teamspace, project, modelId, modelName }) { yield put(TeamspacesActions.setModelUploadStatus(teamspace, project, modelId, modelData)); const currentUser = yield select(selectCurrentUser); if (modelData.user !== currentUser.username) { return; } if (modelData.status === uploadFileStatuses.ok) { yield put(SnackbarActions.show(`Model ${modelName} uploaded successfully`)); } if (modelData.status === uploadFileStatuses.failed) { if (modelData.hasOwnProperty('errorReason') && modelData.errorReason.message) { yield put(SnackbarActions.show(`Failed to import ${modelName} model: ${modelData.errorReason.message}`)); } else { yield put(SnackbarActions.show(`Failed to import ${modelName} model`)); } } }
/** 在每个 TICK 的时候,更新坦克的 telmet 持续时间 */ function* handleHelmetDuration() { while (true) { const { delta }: actions.Tick = yield take(actions.A.Tick) const { tanks }: State = yield select() for (const tank of tanks.filter(t => t.alive && t.helmetDuration > 0).values()) { const nextDuration = Math.max(0, tank.helmetDuration - delta) yield put(actions.setHelmetDuration(tank.tankId, nextDuration)) } } }
function* spawnHitActions({ tanks }: State, stat: Stat) { for (const [targetTankId, hitBullets] of stat.tankHitMap) { // 这里假设一帧内最多只有一发子弹同时击中一架坦克 const bullet = hitBullets[0] const sourceTankId = bullet.tankId const targetTank = tanks.get(targetTankId) const sourceTank = tanks.get(sourceTankId) yield put(actions.hit(bullet, targetTank, sourceTank)) } }
function* putFakeDirectory() { const rootDirectory = xmlToTreeNode(` <directory name="components"> <file name="main.pc"> </file> </directory> `) as Directory; yield put(projectDirectoryLoaded(rootDirectory)); }
export function* removeComment({ teamspace, modelId, issueData }) { try { const { issueNumber, commentIndex, _id, rev_id, guid } = issueData; const commentData = { comment: '', number: issueNumber, delete: true, commentIndex, _id, rev_id }; yield API.updateIssue(teamspace, modelId, commentData); yield put(IssuesActions.deleteCommentSuccess(guid, issueData._id)); yield put(SnackbarActions.show('Comment removed')); } catch (error) { yield put(DialogActions.showEndpointErrorDialog('remove', 'comment', error)); } }
export function* deactivateMeasure() { try { yield all([ Viewer.disableMeasure(), put(MeasureActions.setActiveSuccess(false)) ]); } catch (error) { DialogActions.showErrorDialog('deactivate', 'measure', error); } }