本文整理汇总了TypeScript中redux-saga/effects.delay函数的典型用法代码示例。如果您正苦于以下问题:TypeScript delay函数的具体用法?TypeScript delay怎么用?TypeScript delay使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了delay函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: testDelay
function* testDelay(): SagaIterator {
// typings:expect-error
yield delay();
yield delay(1);
// typings:expect-error
yield delay<'result'>(1, 'foo');
yield delay<'result'>(1, 'result');
yield delay(1, 'result');
}
示例2: aiJudgeScore
function * aiJudgeScore () {
const { board, player, ai, version } = yield select()
const scores = []
const judge = judgeScores[version]
invariant(judge, 'version error')
const chess = getCandidate(player)
for (let r = 0; r < 8; r += 1) {
for (let c = 0; c < 8; c += 1) {
if (board[r][c] === chess) {
let score = judge(board, ai, r, c)
scores.push({
row: r,
col: c,
score: score
})
}
}
}
invariant(scores.length, 'Invalid State: Candidates not place')
const { score } = head(orderBy(scores, 'score', 'desc'))
const { row, col } = sample(filter(scores, ['score', score])) // A little random
yield delay(300) // A little delay
yield put(
pushLog({
player,
pos: `(${row}, ${col})`
})
)
yield call(flipAllChess, { row, col, player })
yield put(placeChess(row, col, player))
}
示例3: authSaga
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
}
}
}
}
示例4: demoAIMasterSaga
export function* demoAIMasterSaga() {
while (true) {
const tankId = getNextId('tank')
const tank = new TankRecord({
tankId,
x: 5.5 * B,
y: 0.5 * B,
side: 'bot',
level: 'basic',
hp: 1,
direction: 'left',
})
yield spawnTank(tank, 1.5)
yield take((action: Action) => action.type === A.Hit && action.targetTank.tankId === tankId)
yield put(actions.setTankToDead(tankId))
yield explosionFromTank(tank)
yield delay(7e3)
}
}
示例5: gameSaga
/**
* game-saga负责管理整体游戏进度
* 负责管理游戏开始界面, 游戏结束界面
* game-stage调用stage-saga来运行不同的关卡
* 并根据stage-saga返回的结果选择继续下一个关卡, 或是选择游戏结束
*/
export default function* gameSaga(action: actions.StartGame | actions.ResetGame) {
if (action.type === A.ResetGame) {
DEV.LOG && console.log('GAME RESET')
return
}
// 这里的 delay(0) 是为了「异步执行」后续的代码
// 以保证后续代码执行前已有的cancel逻辑执行完毕
yield delay(0)
DEV.LOG && console.log('GAME STARTED')
const players = [playerSaga('player-1', PLAYER_CONFIGS.player1)]
if (yield select(selectors.isInMultiPlayersMode)) {
players.push(playerSaga('player-2', PLAYER_CONFIGS.player2))
}
const result = yield race({
tick: tickEmitter({ bindESC: true }),
players: all(players),
ai: botMasterSaga(),
powerUp: powerUpManager(),
bullets: bulletsSaga(),
// 上面几个 saga 在一个 gameSaga 的生命周期内被认为是后台服务
// 当 stage-flow 退出(或者是用户直接离开了game-scene)的时候,自动取消上面几个后台服务
flow: stageFlow(action.stageIndex),
leave: take(A.LeaveGameScene),
})
if (DEV.LOG) {
if (result.leave) {
console.log('LEAVE GAME SCENE')
}
}
if (result.flow) {
DEV.LOG && console.log('GAME ENDED')
const { router }: State = yield select()
yield put(replace(`/gameover${router.location.search}`))
}
yield put(actions.beforeEndGame())
yield put(actions.endGame())
}
示例6: setFireToTrueEvery3Seconds
function* setFireToTrueEvery3Seconds() {
while (true) {
fire = true
yield delay(3000)
}
}
示例7: select
export const recommendationsSaga = (storeApi: IStoreApi) => function* () {
// No recommendations until logged in
const loggedIn = yield select(isLoggedIn)
if (!loggedIn) {
yield take(authActions.loginSuccess.getType())
}
const token = yield select(getAuthToken)
// Will initialize this later on first scroll to bottom
let tagItems: IterableIterator<ITagItems | undefined> | null = null
let similarItems: IterableIterator<string[] | undefined> | null = null
// Event channel to watch
const scrollChannel = yield call(createScrollChannel)
while (true) {
// Wait until user scrolls
const scrollInfo = yield take(scrollChannel)
if (scrollInfo.pixelsLeft < PIXELS_LEFT_TO_START) {
yield put(recommendationsActions.setIsLoading(true))
// Initialize recommendations
if (!tagItems || !similarItems) {
const { data: recommendations } = yield call(storeApi.getRecommendations, token)
tagItems = getTagItems(recommendations.tags)
similarItems = getSimilarItems(
{
wishlist: recommendations.wishlist,
friends: recommendations.friends,
curators: recommendations.curators,
played: recommendations.played,
},
yield select(getIgnoredProducts),
yield select(getOwnedProducts),
)
tagItems.next()
similarItems.next()
}
// Get next page of recommendations
const page = yield call(
getRecommendationsPage,
tagItems,
similarItems,
token,
storeApi
)
// Show 'out of recommendations' message
if (page === null) {
yield put(recommendationsActions.setIsFinished(true))
yield put(recommendationsActions.setIsLoading(false))
scrollChannel.close()
break
}
// Display recommendations page
yield put(productActions.add(page.products))
yield put(recommendationsActions.addPage(page.items))
yield put(recommendationsActions.setIsLoading(false))
}
// Throttle
yield delay(100)
}
}
示例8: testDelay
function* testDelay(): SagaIterator {
// typings:expect-error
yield delay()
yield delay(1)
}
示例9: settingsStoredToast
function* settingsStoredToast() {
yield put(addToast('Settings saved'));
yield delay(toastLifetime);
yield put(removeToast());
}