Skip to content

Commit

Permalink
Doc mapper (#6)
Browse files Browse the repository at this point in the history
* added new docMapper option, which allows saving data to custom fields and allows custom data structure

* increment package version to 2.1.0

* added example for docMapper

* indentation fix
  • Loading branch information
deathg0d authored Oct 6, 2024
1 parent 3d783ed commit 937b77e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ A database and connection provider for Yjs based on Firestore.

y-fire is a Firestore (Firebase) and WebRTC-based provider, built especially for serverless infrastructure, that offers real-time capabilities to your Yjs-based applications. y-fire is built with efficiency in mind to reduce the number of calls that the application makes to and from Firestore. With y-fire, Firestore will act as both 1. persistent storage and 2. a peer discovery platform for WebRTC connections. This means that real-time updates are shared through a peer-to-peer network, thus reducing connections to Firestore. y-fire was inspired by [yjs-firestore-provider](https://github.com/gmcfall/yjs-firestore-provider) but implements few things differently.


https://github.com/podraven/y-fire/assets/2324523/3aa27a40-6cfb-4b93-b043-4e0fa57c96d4

# Features
Expand Down Expand Up @@ -105,6 +104,7 @@ You need to grant **read and write** permissions to the document `/path/to/your/
- **firebaseApp**: FirebaseApp (required)
- **ydoc**: Y.Doc (required)
- **path**: path to your **document** (required) ex. users/username/tasks/task-1
- **docMapper**: Custom structure for your document (saves to the `content` field by default)
- **maxUpdatesThreshold**: Number of updates before triggering real-time data share, defaults to 20
- **maxWaitTime**: Time in milliseconds before triggering real-time data share, defaults to 100
- **maxWaitFirestoreTime**: Time in milliseconds before triggering persistent data sync to Firestore, defaults to 3000
Expand All @@ -122,6 +122,20 @@ new FireProvider({
});
```

docMapper example with custom document structure

```
new FireProvider({
firebaseApp,
ydoc,
path: "username/tasks/taskuid",
docMapper: (bytes) => ({
title: "Custom title",
file: { filename: "file.docx", content: bytes }, // "bytes" contains your yjs data
}),
});
```

#### Methods

- **destroy**: Destroys the y-fire instance. You may want to destroy the y-fire instance when navigating out of the page to avoid the initialization of duplicate instances. Use `provider.destroy();` to destroy the instance.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "y-fire",
"version": "2.0.0",
"version": "2.1.0",
"description": "A firebase (firestore) provider for Yjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 8 additions & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Parameters {
firebaseApp: FirebaseApp;
ydoc: Y.Doc;
path: string;
docMapper?: (bytes: Bytes) => object;
maxUpdatesThreshold?: number;
maxWaitTime?: number;
maxWaitFirestoreTime?: number;
Expand Down Expand Up @@ -63,6 +64,8 @@ export class FireProvider extends ObservableV2<any> {
senders: {},
};

documentMapper: (bytes: Bytes) => object = (bytes) => ({ content: bytes });

cache: Uint8Array | null;
maxCacheUpdates: number = 20;
cacheUpdateCount: number = 0;
Expand Down Expand Up @@ -310,7 +313,9 @@ export class FireProvider extends ObservableV2<any> {
const ref = doc(this.db, this.documentPath);
await setDoc(
ref,
{ content: Bytes.fromUint8Array(Y.encodeStateAsUpdate(this.doc)) },
this.documentMapper(
Bytes.fromUint8Array(Y.encodeStateAsUpdate(this.doc))
),
{ merge: true }
);
this.deleteLocal(); // We have successfully saved to Firestore, empty indexedDb for now
Expand Down Expand Up @@ -481,6 +486,7 @@ export class FireProvider extends ObservableV2<any> {
firebaseApp,
ydoc,
path,
docMapper,
maxUpdatesThreshold,
maxWaitTime,
maxWaitFirestoreTime,
Expand All @@ -492,6 +498,7 @@ export class FireProvider extends ObservableV2<any> {
this.db = getFirestore(this.firebaseApp);
this.doc = ydoc;
this.documentPath = path;
if (docMapper) this.documentMapper = docMapper;
if (maxUpdatesThreshold) this.maxCacheUpdates = maxUpdatesThreshold;
if (maxWaitTime) this.maxRTCWait = maxWaitTime;
if (maxWaitFirestoreTime) this.maxFirestoreWait = maxWaitFirestoreTime;
Expand Down

0 comments on commit 937b77e

Please sign in to comment.