From 868dbd7bca5363ccd44892dec097615d24dee70e Mon Sep 17 00:00:00 2001 From: Xc Date: Tue, 5 Dec 2023 17:40:45 +0800 Subject: [PATCH] =?UTF-8?q?onCommit=E6=94=B9=E6=88=90=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- es/store.d.ts | 3 ++- es/store.js | 17 ++++++++++++++--- lib/store.d.ts | 3 ++- lib/store.js | 17 ++++++++++++++--- src/store.ts | 21 +++++++++++++++------ 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/es/store.d.ts b/es/store.d.ts index 0ecb905..220687e 100644 --- a/es/store.d.ts +++ b/es/store.d.ts @@ -101,8 +101,9 @@ export default class TreeStore extends C }; beginSync(): string; private commitCallbacks; - onCommit(callback: (result: OperationResult) => void): () => ((result: OperationResult) => void)[]; + onCommit(callback: (result: OperationResult) => Promise): () => ((result: OperationResult) => Promise)[]; private addToOperationResult; + private commitLogic; commitSync(uuid: string): void; rollbackSync(uuid: string): void; beginAsync(): Promise; diff --git a/es/store.js b/es/store.js index 697d9be..031b8b7 100644 --- a/es/store.js +++ b/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) + ); */ } } diff --git a/lib/store.d.ts b/lib/store.d.ts index 0ecb905..220687e 100644 --- a/lib/store.d.ts +++ b/lib/store.d.ts @@ -101,8 +101,9 @@ export default class TreeStore extends C }; beginSync(): string; private commitCallbacks; - onCommit(callback: (result: OperationResult) => void): () => ((result: OperationResult) => void)[]; + onCommit(callback: (result: OperationResult) => Promise): () => ((result: OperationResult) => Promise)[]; private addToOperationResult; + private commitLogic; commitSync(uuid: string): void; rollbackSync(uuid: string): void; beginAsync(): Promise; diff --git a/lib/store.js b/lib/store.js index fff49b5..ecc38e8 100644 --- a/lib/store.js +++ b/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; diff --git a/src/store.ts b/src/store.ts index c5ab776..c4c7ea9 100644 --- a/src/store.ts +++ b/src/store.ts @@ -2046,9 +2046,9 @@ export default class TreeStore extends C return uuid; } - private commitCallbacks: Array<(result: OperationResult) => void> = []; + private commitCallbacks: Array<(result: OperationResult) => Promise> = []; - onCommit(callback: (result: OperationResult) => void) { + onCommit(callback: (result: OperationResult) => Promise) { this.commitCallbacks.push(callback); return () => pull(this.commitCallbacks, callback); } @@ -2073,7 +2073,7 @@ export default class TreeStore 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 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 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 extends C } } - this.commitCallbacks.forEach( + // 在txn提交时应该call过了,这里看上去是多余的 + /* this.commitCallbacks.forEach( callback => callback(result) - ); + ); */ } }