-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] Table change updates in write transactions (#13)
* trigger update table callbacks only if changes are commited inside transactions
- Loading branch information
1 parent
7262d47
commit 3bb0212
Showing
15 changed files
with
450 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@journeyapps/react-native-quick-sqlite': minor | ||
--- | ||
|
||
Added `registerTablesChangedHook` to DB connections which reports batched table updates once `writeTransaction`s and `writeLock`s have been committed. Maintained API compatibility with `registerUpdateHook` which reports table change events as they occur. Added listeners for when write transactions have been committed or rolled back. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import _ from 'lodash'; | ||
import { registerTransactionHook, registerUpdateHook } from './table-updates'; | ||
import { | ||
BatchedUpdateCallback, | ||
BatchedUpdateNotification, | ||
TransactionEvent, | ||
UpdateCallback, | ||
UpdateNotification | ||
} from './types'; | ||
import { BaseListener, BaseObserver } from './utils/BaseObserver'; | ||
|
||
export interface DBListenerManagerOptions { | ||
dbName: string; | ||
} | ||
|
||
export interface WriteTransactionEvent { | ||
type: TransactionEvent; | ||
} | ||
|
||
export interface DBListener extends BaseListener { | ||
/** | ||
* Register a listener to be fired for any table change. | ||
* Changes inside write locks and transactions are reported immediately. | ||
*/ | ||
rawTableChange: UpdateCallback; | ||
|
||
/** | ||
* Register a listener for when table changes are persisted | ||
* into the DB. Changes during write transactions which are | ||
* rolled back are not reported. | ||
* Any changes during write locks are buffered and reported | ||
* after transaction commit and lock release. | ||
* Table changes are reported individually for now in order to maintain | ||
* API compatibility. These can be batched in future. | ||
*/ | ||
tablesUpdated: BatchedUpdateCallback; | ||
|
||
/** | ||
* Listener event triggered whenever a write transaction | ||
* is started, committed or rolled back. | ||
*/ | ||
writeTransaction: (event: WriteTransactionEvent) => void; | ||
} | ||
|
||
export class DBListenerManager extends BaseObserver<DBListener> {} | ||
|
||
export class DBListenerManagerInternal extends DBListenerManager { | ||
private updateBuffer: UpdateNotification[]; | ||
|
||
constructor(protected options: DBListenerManagerOptions) { | ||
super(); | ||
this.updateBuffer = []; | ||
registerUpdateHook(this.options.dbName, (update) => this.handleTableUpdates(update)); | ||
registerTransactionHook(this.options.dbName, (eventType) => { | ||
switch (eventType) { | ||
case TransactionEvent.COMMIT: | ||
this.flushUpdates(); | ||
break; | ||
case TransactionEvent.ROLLBACK: | ||
this.transactionReverted(); | ||
break; | ||
} | ||
|
||
this.iterateListeners((l) => | ||
l.writeTransaction?.({ | ||
type: eventType | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
flushUpdates() { | ||
if (!this.updateBuffer.length) { | ||
return; | ||
} | ||
|
||
const groupedUpdates = _.groupBy(this.updateBuffer, (update) => update.table); | ||
const batchedUpdate: BatchedUpdateNotification = { | ||
groupedUpdates, | ||
rawUpdates: this.updateBuffer, | ||
tables: _.keys(groupedUpdates) | ||
}; | ||
this.updateBuffer = []; | ||
this.iterateListeners((l) => l.tablesUpdated?.(batchedUpdate)); | ||
} | ||
|
||
protected transactionReverted() { | ||
// clear updates | ||
this.updateBuffer = []; | ||
} | ||
|
||
handleTableUpdates(notification: UpdateNotification) { | ||
// Fire updates for any change | ||
this.iterateListeners((l) => l.rawTableChange?.({ ...notification })); | ||
|
||
// Queue changes until they are flushed | ||
this.updateBuffer.push(notification); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Hooks which can be triggered during the execution of read/write locks | ||
*/ | ||
export interface LockHooks { | ||
lockAcquired?: () => Promise<void>; | ||
lockReleased?: () => Promise<void>; | ||
} |
Oops, something went wrong.