-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
30 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
...entation/components/YjsPendingChangesCallbackExample/YjsPendingChangesCallbackExample.tsx
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,105 @@ | ||
import sodium, { KeyPair } from "libsodium-wrappers"; | ||
import React, { useRef, useState } from "react"; | ||
import { DevTool } from "secsync-react-devtool"; | ||
import { useYjsSync } from "secsync-react-yjs"; | ||
import * as Yjs from "yjs"; | ||
import { useYArray } from "../../hooks/useYArray"; | ||
|
||
const websocketEndpoint = | ||
process.env.NODE_ENV === "development" | ||
? "ws://localhost:4000" | ||
: "wss://secsync.fly.dev"; | ||
|
||
type Props = { | ||
documentId: string; | ||
showDevTool: boolean; | ||
}; | ||
|
||
export const YjsPendingChangesCallbackExample: React.FC<Props> = ({ | ||
documentId, | ||
showDevTool, | ||
}) => { | ||
const documentKey = sodium.from_base64( | ||
"MTcyipWZ6Kiibd5fATw55i9wyEU7KbdDoTE_MRgDR98" | ||
); | ||
|
||
const [authorKeyPair] = useState<KeyPair>(() => { | ||
return sodium.crypto_sign_keypair(); | ||
}); | ||
|
||
const yDocRef = useRef<Yjs.Doc>(new Yjs.Doc()); | ||
const yTodos: Yjs.Array<string> = yDocRef.current.getArray("todos"); | ||
const todos = useYArray(yTodos); | ||
const [newTodoText, setNewTodoText] = useState(""); | ||
|
||
const [state, send] = useYjsSync({ | ||
onPendingChangesUpdated: (allChanges) => { | ||
console.log("pending changes", allChanges); | ||
}, | ||
yDoc: yDocRef.current, | ||
documentId, | ||
signatureKeyPair: authorKeyPair, | ||
websocketEndpoint, | ||
websocketSessionKey: "your-secret-session-key", | ||
getNewSnapshotData: async ({ id }) => { | ||
return { | ||
data: Yjs.encodeStateAsUpdateV2(yDocRef.current), | ||
key: documentKey, | ||
publicData: {}, | ||
}; | ||
}, | ||
getSnapshotKey: async () => { | ||
return documentKey; | ||
}, | ||
shouldSendSnapshot: ({ snapshotUpdatesCount }) => { | ||
// create a new snapshot if the active snapshot has more than 100 updates | ||
return snapshotUpdatesCount > 3; | ||
}, | ||
isValidClient: async (signingPublicKey: string) => { | ||
return true; | ||
}, | ||
sodium, | ||
logging: "error", | ||
}); | ||
|
||
return ( | ||
<> | ||
<div className="todoapp"> | ||
<form | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
yTodos.push([newTodoText]); | ||
setNewTodoText(""); | ||
}} | ||
> | ||
<input | ||
placeholder="What needs to be done?" | ||
onChange={(event) => setNewTodoText(event.target.value)} | ||
value={newTodoText} | ||
className="new-todo" | ||
/> | ||
<button className="add">Add</button> | ||
</form> | ||
|
||
<ul className="todo-list"> | ||
{todos.map((entry, index) => { | ||
return ( | ||
<li key={`${index}-${entry}`}> | ||
<div className="edit">{entry}</div> | ||
<button | ||
className="destroy" | ||
onClick={() => { | ||
yTodos.delete(index, 1); | ||
}} | ||
/> | ||
</li> | ||
); | ||
})} | ||
</ul> | ||
</div> | ||
|
||
<div className="mt-8" /> | ||
<DevTool state={state} send={send} /> | ||
</> | ||
); | ||
}; |
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
17 changes: 17 additions & 0 deletions
17
documentation/pages/docs/other-examples/pending-changes.mdx
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,17 @@ | ||
## Pending Changes Example | ||
|
||
This example shows how to use the `onPendingChangesUpdated` callback to get the unsynced changes. This is useful to update a local storage to avoid changes getting lost e.g. in a local-first setting. | ||
|
||
See the `console.log` output in the browser console to see the pending changes. | ||
|
||
import SimpleExampleWrapper from "../../../components/SimpleExampleWrapper"; | ||
import { YjsPendingChangesCallbackExample } from "../../../components/YjsPendingChangesCallbackExample/YjsPendingChangesCallbackExample"; | ||
|
||
<SimpleExampleWrapper | ||
component={YjsPendingChangesCallbackExample} | ||
generateDocumentKey={false} | ||
/> | ||
|
||
## Code | ||
|
||
The code for this example is located [here](https://github.com/serenity-kit/secsync/blob/main/documentation/components/YjsPendingChangesCallbackExample/YjsPendingChangesCallbackExample.tsx). |
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