mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-09-20 16:00:38 +02:00
feat: multiplayer undo / redo (#7348)
This commit is contained in:
21
packages/excalidraw/hooks/useEmitter.ts
Normal file
21
packages/excalidraw/hooks/useEmitter.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Emitter } from "../emitter";
|
||||
|
||||
export const useEmitter = <TEvent extends unknown>(
|
||||
emitter: Emitter<[TEvent]>,
|
||||
initialState: TEvent,
|
||||
) => {
|
||||
const [event, setEvent] = useState<TEvent>(initialState);
|
||||
|
||||
useEffect(() => {
|
||||
const unsubscribe = emitter.on((event) => {
|
||||
setEvent(event);
|
||||
});
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
}, [emitter]);
|
||||
|
||||
return event;
|
||||
};
|
Reference in New Issue
Block a user