context restartToExecute改成新上下文

This commit is contained in:
Xu Chang 2024-05-09 20:32:59 +08:00
parent d8064e8b64
commit 9048839a1b
3 changed files with 9 additions and 12 deletions

View File

@ -50,6 +50,7 @@ export declare abstract class AsyncContext<ED extends EntityDict> implements Con
abstract getCurrentUserId(allowUnloggedIn?: boolean): string | undefined; abstract getCurrentUserId(allowUnloggedIn?: boolean): string | undefined;
abstract setCurrentUserId(userId: string | undefined): void; abstract setCurrentUserId(userId: string | undefined): void;
abstract toString(): Promise<string>; abstract toString(): Promise<string>;
abstract getSerializedData(): object;
abstract initialize(data?: any, later?: boolean): Promise<void>; abstract initialize(data?: any, later?: boolean): Promise<void>;
abstract allowUserUpdate(): boolean; abstract allowUserUpdate(): boolean;
abstract openRootMode(): () => void; abstract openRootMode(): () => void;

View File

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

View File

@ -38,12 +38,9 @@ export abstract class AsyncContext<ED extends EntityDict> implements Context {
// 使一个上下文重新开始事务执行,清除历史数据(定时器中使用) // 使一个上下文重新开始事务执行,清除历史数据(定时器中使用)
async restartToExecute(routine: (context: this) => Promise<any>) { async restartToExecute(routine: (context: this) => Promise<any>) {
const newContext = !this.uuid ? this : { const data = this.getSerializedData();
...this, const newContext = (new (Object.getPrototypeOf(this).constructor)()) as typeof this;
}; // 这里可能有问题继承的context对象中如果有对象属性会变成指针公用但是估计目前是跑不到的。by Xc 20231215 await newContext.initialize(data, true);
if (newContext !== this) {
console.warn('restartToExecute跑出了非重用当前context的情况请仔细调试');
}
newContext.opRecords = []; newContext.opRecords = [];
newContext.events = { newContext.events = {
@ -233,6 +230,8 @@ export abstract class AsyncContext<ED extends EntityDict> implements Context {
// 此接口将上下文变成可以serialized的字符串 // 此接口将上下文变成可以serialized的字符串
abstract toString(): Promise<string>; abstract toString(): Promise<string>;
abstract getSerializedData(): object;
// 此接口将字符串parse成对象再进行初始化 // 此接口将字符串parse成对象再进行初始化
// later表示允许延时状态上下文要处理在时间维度上可能的异常比如用户token已经注销等 // later表示允许延时状态上下文要处理在时间维度上可能的异常比如用户token已经注销等
abstract initialize(data?: any, later?: boolean): Promise<void>; abstract initialize(data?: any, later?: boolean): Promise<void>;