Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions .coderabbit.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
reviews:
path_filters:
# Include these paths
- "package/src/**"
- "package/tests/**"

# Exclude these paths (prefix with !)
- "!node_modules/**"
- "!package/node_modules/**"
- "!package/dist/**"
- "!package/.svelte-kit/**"
- "!package/build/**"
- "!package/*.min.js"
- "!demo/**"
- "core/**"
- "svelte/src/lib/**"
- "vanilla/lib/**"
34 changes: 34 additions & 0 deletions core/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# dependencies (bun install)
node_modules

# output
out
dist
*.tgz

# code coverage
coverage
*.lcov

# logs
logs
_.log
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# caches
.eslintcache
.cache
*.tsbuildinfo

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
2 changes: 2 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Core
This is the core implementation of syncrostate.
File renamed without changes.
11 changes: 11 additions & 0 deletions core/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { syncroState, type SyncroStateOptions } from "./proxys/syncroState.js";
export { usePresence } from "./presence.js";
export { y } from "./schemas/schema.js";
export { type Provider } from "./provider.js";
export { type ObjectShape } from "./schemas/object.js";
export {
type SchemaOutput,
type InferSchemaType,
type Schema,
type RawSchemaOutput,
} from "./schemas/schema.js";
81 changes: 81 additions & 0 deletions core/lib/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Awareness as YAwareness } from "y-protocols/awareness";
import { Doc } from "yjs";
import { PRENSENCE, PRENSENCE_ID, PRESENCE_CONTEXT_KEY } from "./constants.js";
import type { Provider } from "./provider.js";

export type PresenceUser = Record<string, string | number | boolean>;

export class Presence<T = any> {
private awareness: YAwareness;
private provider: Provider;
private id: { value: string };
private synced: { value: boolean };
me: { value: PresenceUser };
others: { value: PresenceUser[] };

constructor({
doc,
awareness,
provider,
}: {
doc: Doc;
awareness?: YAwareness;
provider: Provider;
}) {
this.awareness = awareness ?? new YAwareness(doc);
this.provider = provider;

this.id = provider.state("");
this.synced = provider.state(false);
this.me = provider.state({});
this.others = provider.state([]);

provider.effect(() => {
if (this.synced.value) {
this.awareness.setLocalStateField(PRENSENCE, this.me.value);
}
}, []);
}

private setOthers = () => {
const users = Array.from(this.awareness.getStates().values());

this.others.value = users
.filter((user) => user.id !== this.id.value)
.reduce((acc, user) => {
if (user[PRENSENCE_ID] && user[PRENSENCE_ID] !== this.id.value) {
acc.push(user[PRENSENCE]);
}
return acc;
}, [] as any);
};
init = ({
me = {},
awareness,
}: {
me?: PresenceUser;
awareness?: YAwareness;
}) => {
if (awareness) {
this.awareness = awareness;
}
if (me.id && typeof me.id === "string") {
this.id.value = me.id as string;
}
this.me.value = me;
this.synced.value = true;
this.awareness.setLocalStateField(PRENSENCE_ID, this.id.value);
this.awareness.setLocalStateField(PRENSENCE, this.me.value);
this.setOthers();

this.awareness.on("update", this.setOthers);
};
}

export const usePresence = <T>(getContext: (key: string) => any) => {
const presence = getContext(PRESENCE_CONTEXT_KEY) as Presence<T>;
return {
others: presence.others,
me: presence.me,
};
};
26 changes: 26 additions & 0 deletions core/lib/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type ProviderState<T> = {
value: T;
};

export type ProviderEffect = (cb: () => void, deps?: any[]) => void;

export type ProviderDerived<T> = (cb: () => T) => {
value: T;
};

export interface Provider {
state: <T>(initialValue: T) => {
value: T;
};
effect: (cb: () => void, deps?: any[]) => void;
derived: <T>(cb: () => T) => {
value: T;
};
onMount: (cb: () => void) => void;
onDestroy: (cb: () => void) => void;
Map: typeof Map;
Set: typeof Set;
Date: typeof Date;
getContext: <T>(key: string) => T;
setContext: <T>(key: string, value: T) => void;
}
Loading