本文整理匯總了TypeScript中main/context.Context.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Context.on方法的具體用法?TypeScript Context.on怎麽用?TypeScript Context.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類main/context.Context
的用法示例。
在下文中一共展示了Context.on方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it("Context propagates progress", async () => {
const c = new Context(store);
let p1 = 0;
let p2 = 0;
c.on("progress", info => {
p1 = info.progress;
});
c.on("progress", info => {
p2 = info.progress;
});
assert.equal(p1, 0);
assert.equal(p2, 0);
c.emitProgress({ progress: 0.5 });
assert.equal(p1, 0.5);
assert.equal(p2, 0.5);
});
示例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,
})
);
}