onCommit改成异步
This commit is contained in:
parent
d586cdea03
commit
868dbd7bca
|
|
@ -101,8 +101,9 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
};
|
||||
beginSync(): string;
|
||||
private commitCallbacks;
|
||||
onCommit(callback: (result: OperationResult<ED>) => void): () => ((result: OperationResult<ED>) => void)[];
|
||||
onCommit(callback: (result: OperationResult<ED>) => Promise<void>): () => ((result: OperationResult<ED>) => Promise<void>)[];
|
||||
private addToOperationResult;
|
||||
private commitLogic;
|
||||
commitSync(uuid: string): void;
|
||||
rollbackSync(uuid: string): void;
|
||||
beginAsync(): Promise<string>;
|
||||
|
|
|
|||
17
es/store.js
17
es/store.js
|
|
@ -1727,7 +1727,7 @@ export default class TreeStore extends CascadeStore {
|
|||
});
|
||||
}
|
||||
}
|
||||
commitSync(uuid) {
|
||||
commitLogic(uuid) {
|
||||
assert(this.activeTxnDict.hasOwnProperty(uuid), uuid);
|
||||
let node = this.activeTxnDict[uuid].nodeHeader;
|
||||
const result = {};
|
||||
|
|
@ -1774,6 +1774,11 @@ export default class TreeStore extends CascadeStore {
|
|||
waiter.fn();
|
||||
}
|
||||
unset(this.activeTxnDict, uuid);
|
||||
return result;
|
||||
}
|
||||
commitSync(uuid) {
|
||||
const result = this.commitLogic(uuid);
|
||||
// 这里无法等待callback完成,callback最好自身保证顺序(前端cache应当具备的特征)
|
||||
this.commitCallbacks.forEach(callback => callback(result));
|
||||
}
|
||||
rollbackSync(uuid) {
|
||||
|
|
@ -1812,7 +1817,10 @@ export default class TreeStore extends CascadeStore {
|
|||
return this.beginSync();
|
||||
}
|
||||
async commitAsync(uuid) {
|
||||
return this.commitSync(uuid);
|
||||
const result = this.commitLogic(uuid);
|
||||
for (const fn of this.commitCallbacks) {
|
||||
await fn(result);
|
||||
}
|
||||
}
|
||||
async rollbackAsync(uuid) {
|
||||
return this.rollbackSync(uuid);
|
||||
|
|
@ -1928,6 +1936,9 @@ export default class TreeStore extends CascadeStore {
|
|||
}
|
||||
}
|
||||
}
|
||||
this.commitCallbacks.forEach(callback => callback(result));
|
||||
// 在txn提交时应该call过了,这里看上去是多余的
|
||||
/* this.commitCallbacks.forEach(
|
||||
callback => callback(result)
|
||||
); */
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,8 +101,9 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
};
|
||||
beginSync(): string;
|
||||
private commitCallbacks;
|
||||
onCommit(callback: (result: OperationResult<ED>) => void): () => ((result: OperationResult<ED>) => void)[];
|
||||
onCommit(callback: (result: OperationResult<ED>) => Promise<void>): () => ((result: OperationResult<ED>) => Promise<void>)[];
|
||||
private addToOperationResult;
|
||||
private commitLogic;
|
||||
commitSync(uuid: string): void;
|
||||
rollbackSync(uuid: string): void;
|
||||
beginAsync(): Promise<string>;
|
||||
|
|
|
|||
17
lib/store.js
17
lib/store.js
|
|
@ -1729,7 +1729,7 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
});
|
||||
}
|
||||
}
|
||||
commitSync(uuid) {
|
||||
commitLogic(uuid) {
|
||||
(0, assert_1.assert)(this.activeTxnDict.hasOwnProperty(uuid), uuid);
|
||||
let node = this.activeTxnDict[uuid].nodeHeader;
|
||||
const result = {};
|
||||
|
|
@ -1776,6 +1776,11 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
waiter.fn();
|
||||
}
|
||||
(0, lodash_1.unset)(this.activeTxnDict, uuid);
|
||||
return result;
|
||||
}
|
||||
commitSync(uuid) {
|
||||
const result = this.commitLogic(uuid);
|
||||
// 这里无法等待callback完成,callback最好自身保证顺序(前端cache应当具备的特征)
|
||||
this.commitCallbacks.forEach(callback => callback(result));
|
||||
}
|
||||
rollbackSync(uuid) {
|
||||
|
|
@ -1814,7 +1819,10 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
return this.beginSync();
|
||||
}
|
||||
async commitAsync(uuid) {
|
||||
return this.commitSync(uuid);
|
||||
const result = this.commitLogic(uuid);
|
||||
for (const fn of this.commitCallbacks) {
|
||||
await fn(result);
|
||||
}
|
||||
}
|
||||
async rollbackAsync(uuid) {
|
||||
return this.rollbackSync(uuid);
|
||||
|
|
@ -1930,7 +1938,10 @@ class TreeStore extends CascadeStore_1.CascadeStore {
|
|||
}
|
||||
}
|
||||
}
|
||||
this.commitCallbacks.forEach(callback => callback(result));
|
||||
// 在txn提交时应该call过了,这里看上去是多余的
|
||||
/* this.commitCallbacks.forEach(
|
||||
callback => callback(result)
|
||||
); */
|
||||
}
|
||||
}
|
||||
exports.default = TreeStore;
|
||||
|
|
|
|||
21
src/store.ts
21
src/store.ts
|
|
@ -2046,9 +2046,9 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
return uuid;
|
||||
}
|
||||
|
||||
private commitCallbacks: Array<(result: OperationResult<ED>) => void> = [];
|
||||
private commitCallbacks: Array<(result: OperationResult<ED>) => Promise<void>> = [];
|
||||
|
||||
onCommit(callback: (result: OperationResult<ED>) => void) {
|
||||
onCommit(callback: (result: OperationResult<ED>) => Promise<void>) {
|
||||
this.commitCallbacks.push(callback);
|
||||
return () => pull(this.commitCallbacks, callback);
|
||||
}
|
||||
|
|
@ -2073,7 +2073,7 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
}
|
||||
}
|
||||
|
||||
commitSync(uuid: string) {
|
||||
private commitLogic(uuid: string) {
|
||||
assert(this.activeTxnDict.hasOwnProperty(uuid), uuid);
|
||||
let node = this.activeTxnDict[uuid].nodeHeader;
|
||||
|
||||
|
|
@ -2123,7 +2123,12 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
}
|
||||
|
||||
unset(this.activeTxnDict, uuid);
|
||||
return result;
|
||||
}
|
||||
|
||||
commitSync(uuid: string) {
|
||||
const result = this.commitLogic(uuid);
|
||||
// 这里无法等待callback完成,callback最好自身保证顺序(前端cache应当具备的特征)
|
||||
this.commitCallbacks.forEach(
|
||||
callback => callback(result)
|
||||
);
|
||||
|
|
@ -2167,7 +2172,10 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
}
|
||||
|
||||
async commitAsync(uuid: string) {
|
||||
return this.commitSync(uuid);
|
||||
const result = this.commitLogic(uuid);
|
||||
for (const fn of this.commitCallbacks) {
|
||||
await fn(result);
|
||||
}
|
||||
}
|
||||
|
||||
async rollbackAsync(uuid: string) {
|
||||
|
|
@ -2287,8 +2295,9 @@ export default class TreeStore<ED extends EntityDict & BaseEntityDict> extends C
|
|||
}
|
||||
}
|
||||
|
||||
this.commitCallbacks.forEach(
|
||||
// 在txn提交时应该call过了,这里看上去是多余的
|
||||
/* this.commitCallbacks.forEach(
|
||||
callback => callback(result)
|
||||
);
|
||||
); */
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue