API reference › @evolu/svelte › ownerSync
function ownerSync<
S extends Readonly<
Record<string, Readonly<Record<string, AnyStandardSchemaV1>>>
>,
>(
evolu: AppOwnerEvolu<S>,
observedOwner: () => ReadonlyOwner | Owner | null,
observedTransports?: () =>
readonly [OwnerWebSocketTransport, OwnerWebSocketTransport] | undefined,
): void;
Defined in: index.svelte.ts:373
Uses an owner for sync through Evolu.useOwner while the component or
root that calls it lives, and stops when it's destroyed or when the owner
that observedOwner returns, or the transports that observedTransports
returns, change. Without transports, the owner syncs with the transports of
the instance's config. A null owner uses none, for a component that waits for
one. The owner and transports are compared by content, so recreating them,
such as in a $derived, keeps the same registration.
Evolu.deleteOwner of the owner ends its use, and it uses the owner
again only once the component mounts again or the owner or transports change.
An instance whose database was deleted uses none.
It creates an $effect, so call it where Svelte allows one, such as during
component initialization or inside $effect.root. Server rendering runs no
effect, so it uses none there.
Example
Runes such as $state work only in Svelte components and .svelte.ts or
.svelte.js modules. A component calls projectSync during its
initialization.
import {
type AppOwnerEvolu,
type ShardOwner,
type TestEvoluSchema,
} from "@evolu/common";
import { ownerSync } from "@evolu/svelte";
// Syncs the open project's owner, and none while no project is open.
const projectSync = (evolu: AppOwnerEvolu<TestEvoluSchema>) => {
let project: ShardOwner | null = $state.raw(null);
ownerSync(evolu, () => project);
return {
open: (owner: ShardOwner | null) => {
project = owner;
},
};
};
void projectSync;