-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Synchronizes database events between multiple clients #66
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb3e23b
Synchronizes database events between multiple clients
kettanaito f225af7
Runs tests aginst the built module
kettanaito 10272b4
Database: Generates unique reproducible md5 hash
kettanaito b05a39f
sync: Ignores events from unrelated databases
kettanaito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export default { | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
preset: 'ts-jest', | ||
testTimeout: 60000, | ||
moduleNameMapper: { | ||
'^@mswjs/data(.*)': '<rootDir>/$1', | ||
}, | ||
} |
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,79 @@ | ||
import { Database, DatabaseEventsMap } from '../db/Database' | ||
|
||
interface DatabaseMessageEventData< | ||
OperationType extends keyof DatabaseEventsMap | ||
> { | ||
operationType: OperationType | ||
payload: DatabaseEventsMap[OperationType] | ||
} | ||
|
||
function removeListeners<Event extends keyof DatabaseEventsMap>( | ||
event: Event, | ||
db: Database<any>, | ||
) { | ||
const listeners = db.events.listeners(event) as DatabaseEventsMap[Event][] | ||
listeners.forEach((listener) => { | ||
db.events.removeListener(event, listener) | ||
}) | ||
|
||
return () => { | ||
listeners.forEach((listener) => { | ||
db.events.addListener(event, listener) | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Synchronizes database operations across multiple clients. | ||
*/ | ||
export function sync(db: Database<any>) { | ||
const IS_BROWSER = typeof window !== 'undefined' | ||
const SUPPORTS_BROADCAST_CHANNEL = typeof BroadcastChannel !== 'undefined' | ||
|
||
if (!IS_BROWSER || !SUPPORTS_BROADCAST_CHANNEL) { | ||
return | ||
} | ||
|
||
const channel = new BroadcastChannel('mswjs/data/sync') | ||
|
||
channel.addEventListener( | ||
'message', | ||
(event: MessageEvent<DatabaseMessageEventData<any>>) => { | ||
const { operationType, payload } = event.data | ||
const [sourceId, ...args] = payload | ||
|
||
// Ignore messages originating from unrelated databases. | ||
// Useful in case of multiple databases on the same page. | ||
if (db.id !== sourceId) { | ||
return | ||
} | ||
|
||
// Remove database event listener for the signaled operation | ||
// to prevent an infinite loop when applying this operation. | ||
const restoreListeners = removeListeners(operationType, db) | ||
|
||
// Apply the database operation signaled from another client | ||
// to the current database instance. | ||
// @ts-ignore | ||
db[operationType](...args) | ||
|
||
// Re-attach database event listeners. | ||
restoreListeners() | ||
}, | ||
) | ||
|
||
function broadcastDatabaseEvent<Event extends keyof DatabaseEventsMap>( | ||
operationType: Event, | ||
) { | ||
return (...args: Parameters<DatabaseEventsMap[Event]>) => { | ||
channel.postMessage({ | ||
operationType, | ||
payload: args, | ||
}) | ||
} | ||
} | ||
|
||
db.events.on('create', broadcastDatabaseEvent('create')) | ||
db.events.on('update', broadcastDatabaseEvent('update')) | ||
db.events.on('delete', broadcastDatabaseEvent('delete')) | ||
} |
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,15 @@ | ||
import { factory, primaryKey } from '@mswjs/data' | ||
|
||
window.db = factory({ | ||
user: { | ||
id: primaryKey(String), | ||
firstName: String, | ||
}, | ||
}) | ||
|
||
window.secondDb = factory({ | ||
user: { | ||
id: primaryKey(String), | ||
firstName: String, | ||
}, | ||
}) |
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,10 @@ | ||
import { factory, primaryKey } from '@mswjs/data' | ||
|
||
const db = factory({ | ||
user: { | ||
id: primaryKey(String), | ||
firstName: String, | ||
}, | ||
}) | ||
|
||
window.db = db |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using an invocation order and database location to identify a single database instance.
The order of
factory()
calls is unlikely to change on runtime. Two first-created databases across clients aren't necessarily the same instances, as each client may load a different module, while the sameBroadcastChannel
controls the entire host. To account for that, I'm adding a module location to the hash salt.Two database instances on multiple pages are equal if: