This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
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.
Refactor to follow API conventions from other YJS providers.
- Loading branch information
Showing
7 changed files
with
122 additions
and
72 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
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
98 changes: 98 additions & 0 deletions
98
demos/powersync-supabase-yjs-text-collab-demo/src/library/powersync/PowerSyncYjsProvider.ts
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,98 @@ | ||
|
||
import * as Y from 'yjs'; | ||
|
||
import { b64ToUint8Array, Uint8ArrayTob64 } from '@/library/binary-utils'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { AbstractPowerSyncDatabase } from '@journeyapps/powersync-sdk-web'; | ||
import { ObservableV2 } from 'lib0/observable' | ||
|
||
export interface PowerSyncYjsEvents { | ||
/** | ||
* Triggered when document contents have been loaded from the database the first time. | ||
* | ||
* The document data may not have been synced from the PowerSync service at this point. | ||
*/ | ||
synced: () => void; | ||
} | ||
|
||
/** | ||
* Configure bidirectional sync for a Yjs document with a PowerSync database. | ||
* | ||
* Updates are stored in the `document_updates` table in the database. | ||
* | ||
* @param ydoc | ||
* @param db | ||
* @param documentId | ||
*/ | ||
export class PowerSyncYjsProvider extends ObservableV2<PowerSyncYjsEvents> { | ||
private seenDocUpdates = new Set<string>(); | ||
private abortController = new AbortController(); | ||
|
||
constructor(public readonly doc: Y.Doc, public readonly db: AbstractPowerSyncDatabase, public readonly documentId: string) { | ||
super(); | ||
|
||
const updates = db.watch('SELECT * FROM document_updates WHERE document_id = ?', [documentId], { signal: this.abortController.signal }); | ||
|
||
this._storeUpdate = this._storeUpdate.bind(this); | ||
this.destroy = this.destroy.bind(this); | ||
|
||
let synced = false; | ||
|
||
const watchLoop = async () => { | ||
for await (let results of updates) { | ||
if (this.abortController.signal.aborted) { | ||
break; | ||
} | ||
|
||
// New data detected in the database | ||
for (let update of results.rows!._array) { | ||
// Ignore any updates we've already seen | ||
if (!this.seenDocUpdates.has(update.id)) { | ||
this.seenDocUpdates.add(update.id); | ||
// apply the update from the database to the doc | ||
const origin = this; | ||
Y.applyUpdateV2(doc, b64ToUint8Array(update.update_b64), origin); | ||
} | ||
} | ||
|
||
if (!synced) { | ||
synced = true; | ||
this.emit('synced', []); | ||
} | ||
} | ||
} | ||
watchLoop(); | ||
|
||
doc.on('updateV2', this._storeUpdate); | ||
doc.on('destroy', this.destroy) | ||
} | ||
|
||
private async _storeUpdate(update: Uint8Array, origin: any) { | ||
if (origin === this) { | ||
// update originated from the database / PowerSync - ignore | ||
return; | ||
} | ||
// update originated from elsewhere - save to the database | ||
const docUpdateId = uuidv4(); | ||
this.seenDocUpdates.add(docUpdateId); | ||
await this.db.execute('INSERT INTO document_updates(id, document_id, update_b64) VALUES(?, ?, ?)', [docUpdateId, this.documentId, Uint8ArrayTob64(update)]); | ||
} | ||
|
||
/** | ||
* Destroy this persistence provider, removing any attached event listeners. | ||
*/ | ||
destroy() { | ||
this.abortController.abort(); | ||
this.doc.off('updateV2', this._storeUpdate) | ||
this.doc.off('destroy', this.destroy) | ||
} | ||
|
||
/** | ||
* Delete data associated with this document from the database. | ||
* | ||
* Also call `destroy()` to remove any event listeners and prevent future updates to the database. | ||
*/ | ||
async deleteData() { | ||
await this.db.execute('DELETE FROM document_updates WHERE document_id = ?', [this.documentId]); | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
demos/powersync-supabase-yjs-text-collab-demo/src/library/powersync/powersyncYjs.ts
This file was deleted.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.