考虑了context在restart时,如果还有活跃事务的情况,但是没有测试
This commit is contained in:
parent
9308030d2c
commit
539513236f
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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];
|
||||||
|
|
|
||||||
|
|
@ -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];
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue