Spaces:
Runtime error
Runtime error
Download frontend/src/lib/liveblocks/useObject.ts from huggingface-projects/stable-diffusion-multiplayer: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/huggingface-projects/stable-diffusion-multiplayer/resolve/b4efffaade9a4e146f673c1611e5f71c89860b4e/frontend/src/lib/liveblocks/useObject.ts
- Command line
-
hf download hf://spaces/huggingface-projects/stable-diffusion-multiplayer@b4efffaade9a4e146f673c1611e5f71c89860b4e/frontend/src/lib/liveblocks/useObject.ts
-
curl -L -o useObject.ts https://huggingface.co/spaces/huggingface-projects/stable-diffusion-multiplayer/resolve/b4efffaade9a4e146f673c1611e5f71c89860b4e/frontend/src/lib/liveblocks/useObject.ts
1.15 kB
| // @ts-nocheck | |
| import { LiveObject } from "@liveblocks/client"; | |
| import { useStorage } from "./useStorage"; | |
| import { onDestroy } from "svelte"; | |
| import type { Writable } from "svelte/store"; | |
| import { writable } from "svelte/store"; | |
| import { useRoom } from "./useRoom"; | |
| /** | |
| * Works similarly to `liveblocks-react` useObject | |
| * https://liveblocks.io/docs/api-reference/liveblocks-react#useObject | |
| * | |
| * The main difference is that it returns a Svelte store: | |
| * const obj = useObject() | |
| * $obj.set('name', 'Chris') | |
| * console.log($obj.get('name')) | |
| */ | |
| export function useObject(name: string, initial?: any): Writable<LiveObject> { | |
| const room = useRoom(); | |
| const rootStore = useStorage(); | |
| const list = writable<LiveObject>(); | |
| let unsubscribe = () => {}; | |
| const unsubscribeRoot = rootStore.subscribe((root) => { | |
| if (!root) { | |
| return; | |
| } | |
| if (!root.get(name)) { | |
| root.set(name, new LiveObject(initial)); | |
| } | |
| list.set(root.get(name)); | |
| unsubscribe(); | |
| unsubscribe = room.subscribe(root.get(name) as LiveObject, (newObject) => { | |
| list.set(newObject); | |
| }); | |
| }); | |
| onDestroy(unsubscribeRoot); | |
| return list; | |
| } | |