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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ node_modules
**/.svelte-kit
/build
/dist
/package/dist


# OS
.DS_Store
Expand Down
5 changes: 4 additions & 1 deletion package/.svelte-kit/__package__/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ export declare const NULL = "$/_NULL_/$";
export declare const NULL_ARRAY = "$/_NULL_ARRAY_/$";
export declare const NULL_OBJECT = "$/_NULL_OBJECT_/$";
export declare const INITIALIZED = "$/_INITIALIZED_/$";
export declare const CONTEXT_KEY = "SYNCED_STATE_CONTEXT";
export declare const PRENSENCE_ID = "$/_PRENSENCE_ID_/$";
export declare const PRENSENCE = "$/_PRENSENCE_/$";
export declare const CONTEXT_KEY = "$/_SYNCED_STATE_CONTEXT_$/";
export declare const PRESENCE_CONTEXT_KEY = "$/_SYNCED_PRESENCE_CONTEXT_$/";
export declare const TRANSACTION_KEY: {
new (): {};
};
5 changes: 4 additions & 1 deletion package/.svelte-kit/__package__/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ export const NULL_ARRAY = `$/_NULL_ARRAY_/$`;
export const NULL_OBJECT = `$/_NULL_OBJECT_/$`;
// Used to mark a value as initialized in the root document and skip the initial validation
export const INITIALIZED = `$/_INITIALIZED_/$`;
export const CONTEXT_KEY = 'SYNCED_STATE_CONTEXT';
export const PRENSENCE_ID = `$/_PRENSENCE_ID_/$`;
export const PRENSENCE = `$/_PRENSENCE_/$`;
export const CONTEXT_KEY = '$/_SYNCED_STATE_CONTEXT_$/';
export const PRESENCE_CONTEXT_KEY = '$/_SYNCED_PRESENCE_CONTEXT_$/';
export const TRANSACTION_KEY = class Transaction {
};
1 change: 1 addition & 0 deletions package/.svelte-kit/__package__/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { syncroState } from './proxys/syncroState.svelte.js';
export { usePresence } from './presence.svelte.js';
export { y } from './schemas/schema.js';
1 change: 1 addition & 0 deletions package/.svelte-kit/__package__/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { syncroState } from './proxys/syncroState.svelte.js';
export { usePresence } from './presence.svelte.js';
export { y } from './schemas/schema.js';
2 changes: 1 addition & 1 deletion package/.svelte-kit/__package__/proxys/array.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export declare class SyncedArray<T extends any = any> {
});
toJSON: () => any[];
sync: (value?: any[]) => void;
observe: () => (e: Y.YArrayEvent<any>, _transaction: Y.Transaction) => void;
observe: (e: Y.YArrayEvent<any>, _transaction: Y.Transaction) => void;
methods: {
slice: (start?: number | undefined, end?: number | undefined) => any[];
toReversed: () => any[];
Expand Down
8 changes: 7 additions & 1 deletion package/.svelte-kit/__package__/proxys/array.svelte.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Y from 'yjs';
import { createSyncroState } from './syncroState.svelte.js';
import { isArrayNull, logError, observeArray, propertyToNumber, setArrayToNull } from '../utils.js';
import { NULL_ARRAY } from '../constants.js';
export class SyncedArray {
state;
validator;
Expand All @@ -16,7 +17,12 @@ export class SyncedArray {
get array() {
return this.syncroStates.map((state) => state.value);
}
setNull = setArrayToNull.bind(this);
setNull = () => {
this.yType.delete(0, this.yType.length);
this.yType.insert(0, [new Y.Text(NULL_ARRAY)]);
this.isNull = true;
};
// setNull = setArrayToNull.bind(this);
deleteProperty = (target, prop) => {
const index = propertyToNumber(prop);
if (typeof index !== 'number') {
Expand Down
24 changes: 6 additions & 18 deletions package/.svelte-kit/__package__/proxys/object.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@ import { isMissingOptionnal } from '../utils.js';
import { createSyncroState } from './syncroState.svelte.js';
import { logError } from '../utils.js';
import { NULL_OBJECT } from '../constants.js';
const createYTypesObjectProxy = (yType) => {
return new Proxy({}, {
get: (target, key) => {
if (typeof key === 'string' && yType.has(key)) {
return yType.get(key);
}
return undefined;
}
});
};
export class SyncedObject {
state;
validator;
Expand All @@ -36,8 +26,8 @@ export class SyncedObject {
logError('Can not delete non optional property', p);
return true;
}
this.yType.delete(p);
syncroState.destroy();
this.yType.delete(p);
delete this.syncroStates[p];
return true;
};
Expand Down Expand Up @@ -68,9 +58,7 @@ export class SyncedObject {
}
const remainingStates = Object.keys(this.syncroStates).filter((key) => !(key in value));
remainingStates.forEach((key) => {
this.syncroStates[key].destroy();
delete this.syncroStates[key];
this.yType.delete(key);
this.deleteProperty({}, key);
});
Object.entries(value).forEach(([key, value]) => {
if (key in shape) {
Expand Down Expand Up @@ -175,6 +163,10 @@ export class SyncedObject {
}
observe = (e, _transaction) => {
if (_transaction.origin !== this.state.transactionKey) {
if (this.yType.has(NULL_OBJECT)) {
this.isNull = true;
return;
}
e.changes?.keys.forEach(({ action }, key) => {
const syncedType = this.syncroStates[key];
if (action === 'delete' && syncedType) {
Expand All @@ -192,10 +184,6 @@ export class SyncedObject {
Object.assign(this.syncroStates, { [key]: syncroState });
}
});
if (this.yType.has(NULL_OBJECT)) {
this.isNull = true;
return;
}
}
};
toJSON = () => {
Expand Down
14 changes: 10 additions & 4 deletions package/.svelte-kit/__package__/proxys/syncroState.svelte.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ import { SyncedNumber } from './number.svelte.js';
import { SyncedArray } from './array.svelte.js';
import type { SyncedContainer } from './common.js';
import { SyncedSet } from './set.svelte.js';
export type SyncroStates = SyncedText | SyncedNumber | SyncedBoolean | SyncedDate | SyncedEnum | SyncedObject | SyncedArray | SyncedSet;
export type State = {
import { Presence, type PresenceUser } from '../presence.svelte.js';
import { SyncedMap } from './map.svelte.js';
export type SyncroStates = SyncedText | SyncedNumber | SyncedBoolean | SyncedDate | SyncedEnum | SyncedObject | SyncedArray | SyncedSet | SyncedMap;
export type State<P extends ObjectShape = ObjectShape> = {
synced: boolean;
awareness: Awareness;
doc: Y.Doc;
undoManager: Y.UndoManager;
initialized: boolean;
transaction: (fn: () => void) => void;
transactionKey: any;
presence: Presence<P>;
undo: () => void;
redo: () => void;
};
export declare const syncroState: <T extends ObjectShape>({ schema, sync }: {
export declare const syncroState: <T extends ObjectShape, P extends ObjectShape>({ schema, sync, doc: customDoc, awareness: customAwareness, presence: p }: {
schema: T;
doc?: Y.Doc;
awareness?: Awareness;
presence?: Omit<PresenceUser, "$/_PRENSENCE_ID_/$">;
sync?: ({ doc, awareness, synced }: {
doc: Y.Doc;
awareness: Awareness;
synced: () => void;
synced: (provider?: any) => void;
}) => Promise<void>;
}) => SchemaOutput<T>;
export declare const createSyncroState: ({ key, validator, forceNewType, value, parent, state }: {
Expand Down
37 changes: 26 additions & 11 deletions package/.svelte-kit/__package__/proxys/syncroState.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { onMount, setContext } from 'svelte';
import { CONTEXT_KEY, INITIALIZED, TRANSACTION_KEY } from '../constants.js';
import { SyncedArray } from './array.svelte.js';
import { SyncedSet } from './set.svelte.js';
import { Presence } from '../presence.svelte.js';
import { SyncedMap } from './map.svelte.js';
// For testing purpose
const safeSetContext = (key, value) => {
try {
Expand All @@ -21,22 +23,25 @@ const safeSetContext = (key, value) => {
//
}
};
export const syncroState = ({ schema, sync }) => {
const doc = new Y.Doc();
const awareness = new Awareness(doc);
export const syncroState = ({ schema, sync, doc: customDoc, awareness: customAwareness, presence: p }) => {
const doc = customDoc ?? new Y.Doc();
const awareness = customAwareness ?? new Awareness(doc);
const presence = new Presence({ doc, awareness });
const schemaValidator = new ObjectValidator(schema);
const stateMap = doc.getMap('$state');
const undoManager = new Y.UndoManager(stateMap);
const transactionKey = new TRANSACTION_KEY();
let state = $state({
synced: sync ? false : true,
initialized: false,
awareness,
doc,
undoManager,
presence: presence,
transaction: (fn) => {
state.doc.transact(fn, TRANSACTION_KEY);
state.doc.transact(fn, transactionKey);
},
transactionKey: TRANSACTION_KEY,
transactionKey,
undo: () => {
if (undoManager?.canUndo()) {
undoManager.undo();
Expand Down Expand Up @@ -67,7 +72,6 @@ export const syncroState = ({ schema, sync }) => {
const initialize = (doc, cb) => {
const text = doc.getText(INITIALIZED);
const initialized = text?.toString() === INITIALIZED;
console.log({ initialized });
Object.assign(doc, { initialized });
state.initialized = initialized;
cb();
Expand All @@ -78,21 +82,22 @@ export const syncroState = ({ schema, sync }) => {
text.insert(0, INITIALIZED);
}
};
const synced = () => {
const synced = (provider) => {
initialize(doc, () => {
syncroStateProxy.sync(syncroStateProxy.value);
stateMap.observe(syncroStateProxy.observe);
presence.init({ me: p, awareness: provider?.awareness });
state.synced = true;
});
};
if (!sync) {
synced();
}
else {
onMount(() => {
onMount(() => {
if (sync) {
sync({ doc, awareness, synced });
});
}
}
});
return syncroStateProxy.value;
};
export const createSyncroState = ({ key, validator, forceNewType, value, parent, state }) => {
Expand Down Expand Up @@ -175,5 +180,15 @@ export const createSyncroState = ({ key, validator, forceNewType, value, parent,
state
});
}
case 'map': {
return new SyncedMap({
yType: type,
validator: validator,
value,
parent,
key,
state
});
}
}
};
2 changes: 1 addition & 1 deletion package/.svelte-kit/__package__/schemas/base.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type BaseSchema<T> = {
kind: 'array' | 'object' | 'string' | 'boolean' | 'number' | 'enum' | 'date' | 'richText' | 'set';
kind: 'array' | 'object' | 'string' | 'boolean' | 'number' | 'enum' | 'date' | 'richText' | 'set' | 'map';
optional: boolean;
nullable: boolean;
default?: T;
Expand Down
6 changes: 5 additions & 1 deletion package/.svelte-kit/__package__/schemas/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ export class ObjectValidator {
return null;
}
return Object.entries(this.$schema.shape).reduce((acc, [key, validator]) => {
Object.assign(acc, { [key]: validator.coerce(value[key]) });
const isValid = validator.isValid(value[key]);
if (!isValid) {
return acc;
}
Object.assign(acc, { [key]: value[key] });
return acc;
}, {});
}
Expand Down
14 changes: 8 additions & 6 deletions package/.svelte-kit/__package__/schemas/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { NumberValidator, type NumberSchema } from './number.js';
import { SetValidator, type SetSchema } from './set.js';
import type { State } from '../proxys/syncroState.svelte.js';
import type { Array as YArray, Map as YMap, AbstractType, Text as YText } from 'yjs';
export type Schema = ArraySchema<any> | ObjectSchema<any> | BooleanSchema | DateSchema | StringSchema | NumberSchema | EnumSchema<any> | SetSchema<any>;
import { MapValidator, type MapSchema } from './map.js';
export type Schema = ArraySchema<any> | ObjectSchema<any> | BooleanSchema | DateSchema | StringSchema | NumberSchema | EnumSchema<any> | SetSchema<any> | MapSchema<any>;
export type PrimitiveValidator = BaseValidator<Schema, boolean, boolean>;
export type Validator = PrimitiveValidator | ArrayValidator<any> | ObjectValidator<any> | SetValidator<any>;
export type Validator = PrimitiveValidator | ArrayValidator<any> | ObjectValidator<any> | SetValidator<any> | MapValidator<any>;
export declare const y: {
boolean: () => BooleanValidator<false, false>;
date: () => DateValidator<false, false>;
Expand All @@ -24,18 +25,19 @@ export declare const y: {
array: <T extends Validator>(shape: T) => ArrayValidator<T, false, false>;
number: () => NumberValidator<false, false>;
set: <T extends PrimitiveValidator>(shape: T) => SetValidator<T, false, false>;
map: <T extends Validator>(shape: T) => MapValidator<T, false, false>;
};
type Optional<T, N extends boolean = false> = N extends true ? T | undefined : T;
type InferYTypeFromShape<Shape extends Validator | ObjectShape> = Shape extends ObjectShape ? {
[K in keyof Shape]: Optional<InferYTypeFromShape<Shape[K]>, Shape[K]['$schema']['optional']>;
} : Shape extends ArrayValidator<any> ? YArray<any> : Shape extends ObjectValidator<any> ? YMap<any> : Shape extends SetValidator<any> ? YArray<any> : YText;
type Getters<T extends 'object' | 'array', Shape extends Validator | ObjectShape> = {
} : Shape extends ArrayValidator<any> ? YArray<any> : Shape extends ObjectValidator<any> ? YMap<any> : Shape extends SetValidator<any> ? YArray<any> : Shape extends MapValidator<any> ? YMap<any> : YText;
type Getters<T extends 'object' | 'array' | 'map', Shape extends Validator | ObjectShape> = {
getState?: () => State;
getYTypes?: () => T extends 'array' ? AbstractType<any>[] : InferYTypeFromShape<Shape>;
getYTypes?: () => T extends 'array' ? AbstractType<any>[] : T extends 'map' ? Record<string, InferYTypeFromShape<Shape>> : InferYTypeFromShape<Shape>;
getYType?: () => T extends 'array' ? YArray<any> : YMap<any>;
};
type NORO<N extends boolean, O extends boolean, T> = N extends true ? O extends true ? T | null | undefined : T | null : O extends true ? T | undefined : T;
type InferSchemaType<T> = T extends ObjectValidator<infer Shape, infer O, infer N> ? NORO<N, O, SchemaOutput<Shape>> : T extends BaseValidator<infer S, infer O, infer N> ? NORO<N, O, S extends BaseSchema<infer T> ? T : never> : T extends ArrayValidator<infer Shape> ? InferSchemaType<Shape>[] & Getters<'array', Shape> : T extends SetValidator<infer Shape, infer O, infer N> ? NORO<N, O, Set<InferSchemaType<Shape>>> & Getters<'array', Shape> : never;
export type InferSchemaType<T> = T extends ObjectValidator<infer Shape, infer O, infer N> ? NORO<N, O, SchemaOutput<Shape>> : T extends BaseValidator<infer S, infer O, infer N> ? NORO<N, O, S extends BaseSchema<infer T> ? T : never> : T extends ArrayValidator<infer Shape> ? InferSchemaType<Shape>[] & Getters<'array', Shape> : T extends SetValidator<infer Shape, infer O, infer N> ? NORO<N, O, Set<InferSchemaType<Shape>>> & Getters<'array', Shape> : T extends MapValidator<infer Shape, infer O, infer N> ? NORO<N, O, Map<string, InferSchemaType<Shape>>> & Getters<'map', Shape> : never;
export type SchemaOutput<T extends ObjectShape> = Simplify<{
[K in keyof T]: InferSchemaType<T[K]>;
}> & Getters<'object', T>;
Expand Down
4 changes: 3 additions & 1 deletion package/.svelte-kit/__package__/schemas/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ObjectValidator } from './object.js';
import { RichTextValidator } from './richtext.js';
import { NumberValidator } from './number.js';
import { SetValidator } from './set.js';
import { MapValidator } from './map.js';
export const y = {
boolean: () => new BooleanValidator(),
date: () => new DateValidator(),
Expand All @@ -17,5 +18,6 @@ export const y = {
object: (shape) => new ObjectValidator(shape),
array: (shape) => new ArrayValidator(shape),
number: () => new NumberValidator(),
set: (shape) => new SetValidator(shape)
set: (shape) => new SetValidator(shape),
map: (shape) => new MapValidator(shape)
};
2 changes: 1 addition & 1 deletion package/.svelte-kit/__package__/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ export declare function setArrayToNull(this: SyncedArray | SyncedSet): void;
export declare const isArrayNull: ({ yType }: {
yType: Y.Array<any>;
}) => boolean;
export declare function observeArray(this: SyncedArray | SyncedSet): (e: Y.YArrayEvent<any>, _transaction: Y.Transaction) => void;
export declare function observeArray(this: SyncedArray | SyncedSet, e: Y.YArrayEvent<any>, _transaction: Y.Transaction): void;
Loading