本文整理汇总了TypeScript中common/actions.actions.taskEnded方法的典型用法代码示例。如果您正苦于以下问题:TypeScript actions.taskEnded方法的具体用法?TypeScript actions.taskEnded怎么用?TypeScript actions.taskEnded使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common/actions.actions
的用法示例。
在下文中一共展示了actions.taskEnded方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: asTask
async function asTask(opts: IAsTaskOpts) {
const id = uuid();
const { store, name, gameId } = opts;
const memlog = new memory.WritableStream();
const logger = makeLogger({ customOut: memlog });
store.dispatch(
actions.taskStarted({
id,
name,
gameId,
startedAt: Date.now(),
})
);
const ctx = new Context(store);
ctx.registerTaskId(id);
ctx.on("progress", (ev: IProgressInfo) => {
store.dispatch(actions.taskProgress({ id, ...ev }));
});
getCurrentTasks()[id] = ctx;
let err: Error;
const { work, onError, onCancel } = opts;
try {
await work(ctx, logger);
} catch (e) {
err = e;
}
delete getCurrentTasks()[id];
try {
logger.close();
} catch (e) {
rootLogger.warn(`Couldn't close logger: ${e.stack}`);
}
if (err) {
if (isCancelled(err)) {
rootLogger.warn(`Task ${name} cancelled`);
if (onCancel) {
await onCancel();
}
} else if (isAborted(err)) {
rootLogger.warn(`Task ${name} aborted`);
if (onCancel) {
await onCancel();
}
} else {
rootLogger.warn(`Task ${name} threw: ${err.stack}`);
if (onError) {
await onError(err, memlog ? memlog.toString() : "(No log)");
}
}
}
store.dispatch(
actions.taskEnded({
id,
err: err ? `${err}` : null,
})
);
}
示例2: asTask
async function asTask(opts: AsTaskOpts) {
const id = uuid();
const { store, name, gameId, caveId } = opts;
const logger = recordingLogger(mainLogger);
store.dispatch(
actions.taskStarted({
id,
name,
gameId,
caveId,
startedAt: Date.now(),
})
);
const ctx = new Context(store);
ctx.registerTaskId(id);
ctx.on("progress", (ev: ProgressInfo) => {
store.dispatch(actions.taskProgress({ id, ...ev }));
});
getCurrentTasks()[id] = ctx;
let err: Error;
const { work, onError, onCancel } = opts;
try {
await work(ctx, logger);
} catch (e) {
err = e;
}
delete getCurrentTasks()[id];
if (err) {
if (isCancelled(err)) {
mainLogger.warn(`Task ${name} cancelled`);
if (onCancel) {
await onCancel();
}
} else if (isAborted(err)) {
mainLogger.warn(`Task ${name} aborted`);
if (onCancel) {
await onCancel();
}
} else {
mainLogger.warn(`Task ${name} threw: ${err.stack}`);
if (onError) {
await onError(err, logger.getLog());
}
}
}
store.dispatch(
actions.taskEnded({
id,
err: err ? `${err}` : null,
})
);
}