考虑了context在restart时,如果还有活跃事务的情况,但是没有测试

This commit is contained in:
Xu Chang 2023-12-15 20:18:31 +08:00
parent 9308030d2c
commit 539513236f
3 changed files with 52 additions and 0 deletions

View File

@ -22,6 +22,7 @@ export declare abstract class AsyncContext<ED extends EntityDict> implements Con
*/ */
abstract refineOpRecords(): Promise<void>; abstract refineOpRecords(): Promise<void>;
constructor(store: AsyncRowStore<ED, AsyncContext<ED>>); constructor(store: AsyncRowStore<ED, AsyncContext<ED>>);
restartToExecute(routine: (context: this) => Promise<any>): Promise<void>;
getHeader(key: string): string | string[] | undefined; getHeader(key: string): string | string[] | undefined;
getScene(): string | undefined; getScene(): string | undefined;
setScene(scene?: string): void; setScene(scene?: string): void;

View File

@ -26,6 +26,30 @@ class AsyncContext {
}; };
this.opResult = {}; this.opResult = {};
} }
// 使一个上下文重新开始事务执行,清除历史数据(定时器中使用)
async restartToExecute(routine) {
const newContext = !this.uuid ? this : {
...this,
}; // 这里可能有问题继承的context对象中如果有对象属性会变成指针公用但是估计目前是跑不到的。by Xc 20231215
if (newContext !== this) {
console.warn('restartToExecute跑出了非重用当前context的情况请仔细调试');
}
newContext.opRecords = [];
newContext.events = {
commit: [],
rollback: [],
};
newContext.opResult = {};
await newContext.begin();
try {
await routine(newContext);
await newContext.commit();
}
catch (err) {
await newContext.rollback();
throw err;
}
}
getHeader(key) { getHeader(key) {
if (this.headers) { if (this.headers) {
return this.headers[key]; return this.headers[key];

View File

@ -36,6 +36,33 @@ export abstract class AsyncContext<ED extends EntityDict> implements Context {
this.opResult = {}; this.opResult = {};
} }
// 使一个上下文重新开始事务执行,清除历史数据(定时器中使用)
async restartToExecute(routine: (context: this) => Promise<any>) {
const newContext = !this.uuid ? this : {
...this,
}; // 这里可能有问题继承的context对象中如果有对象属性会变成指针公用但是估计目前是跑不到的。by Xc 20231215
if (newContext !== this) {
console.warn('restartToExecute跑出了非重用当前context的情况请仔细调试');
}
newContext.opRecords = [];
newContext.events = {
commit: [],
rollback: [],
};
newContext.opResult = {};
await newContext.begin();
try {
await routine(newContext);
await newContext.commit();
}
catch (err) {
await newContext.rollback();
throw err;
}
}
getHeader(key: string): string | string[] | undefined { getHeader(key: string): string | string[] | undefined {
if (this.headers) { if (this.headers) {
return this.headers[key]; return this.headers[key];