Skip to content

Commit

Permalink
feat(react): added usePluvDoc to access the root yjs doc (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
pluvrt authored Dec 6, 2023
1 parent 1126215 commit 4c4b47f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .changeset/mighty-maps-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@pluv/react": minor
---

Added `usePluvDoc` to access the root Yjs doc.

```ts
import type { Doc } from "yjs";

const doc: Doc = usePluvDoc();
```
4 changes: 3 additions & 1 deletion packages/client/src/AbstractRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
IOLike,
JsonObject,
} from "@pluv/types";
import type { AbstractType } from "yjs";
import type { AbstractType, Doc } from "yjs";
import type { EventNotifierSubscriptionCallback } from "./EventNotifier";
import type { OtherNotifierSubscriptionCallback } from "./OtherNotifier";
import type {
Expand Down Expand Up @@ -35,6 +35,8 @@ export abstract class AbstractRoom<

public abstract canUndo(): boolean;

public abstract getDoc(): Doc;

public abstract event<TEvent extends keyof InferIOOutput<TIO>>(
event: TEvent,
callback: EventNotifierSubscriptionCallback<TIO, TEvent>,
Expand Down
6 changes: 5 additions & 1 deletion packages/client/src/MockedRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
IOLike,
JsonObject,
} from "@pluv/types";
import type { AbstractType } from "yjs";
import type { AbstractType, Doc } from "yjs";
import { AbstractRoom } from "./AbstractRoom";
import { CrdtManager, CrdtManagerOptions } from "./CrdtManager";
import { CrdtNotifier } from "./CrdtNotifier";
Expand Down Expand Up @@ -139,6 +139,10 @@ export class MockedRoom<
);
}

public getDoc(): Doc {
return this._crdtManager.doc.value;
}

public getMyPresence = (): TPresence => {
return this._usersManager.myPresence;
};
Expand Down
10 changes: 9 additions & 1 deletion packages/react/src/createRoomBundle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
useEffect,
useState,
} from "react";
import type { AbstractType } from "yjs";
import type { AbstractType, Doc } from "yjs";
import {
identity,
shallowArrayEqual,
Expand Down Expand Up @@ -104,6 +104,7 @@ export interface CreateRoomBundle<
selector: (connection: WebSocketConnection) => T,
options?: SubscriptionHookOptions<Id<T>>,
) => Id<T>;
usePluvDoc: () => Doc;
usePluvEvent: <TType extends keyof InferIOOutput<TIO>>(
type: TType,
callback: (data: Id<IOEventMessage<TIO, TType>>) => void,
Expand Down Expand Up @@ -350,6 +351,12 @@ export const createRoomBundle = <
);
};

const usePluvDoc = () => {
const room = usePluvRoom();

return room.getDoc();
};

const usePluvEvent = <TType extends keyof InferIOOutput<TIO>>(
type: TType,
callback: (data: Id<IOEventMessage<TIO, TType>>) => void,
Expand Down Expand Up @@ -574,6 +581,7 @@ export const createRoomBundle = <
usePluvCanRedo,
usePluvCanUndo,
usePluvConnection,
usePluvDoc,
usePluvEvent,
usePluvMyPresence,
usePluvMyself,
Expand Down

1 comment on commit 4c4b47f

@vercel
Copy link

@vercel vercel bot commented on 4c4b47f Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pluv – ./

pluv.vercel.app
pluv.io
pluv-pluv.vercel.app
pluv-git-master-pluv.vercel.app
www.pluv.io

Please sign in to comment.