@voidhash/mimic 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +257 -42
- package/dist/Document.cjs +152 -0
- package/dist/Document.d.cts +67 -0
- package/dist/Document.d.cts.map +1 -0
- package/dist/Document.d.mts +68 -0
- package/dist/Document.d.mts.map +1 -0
- package/dist/Document.mjs +147 -0
- package/dist/Document.mjs.map +1 -0
- package/dist/EffectSchema.cjs +180 -0
- package/dist/EffectSchema.d.cts +84 -0
- package/dist/EffectSchema.d.cts.map +1 -0
- package/dist/EffectSchema.d.mts +84 -0
- package/dist/EffectSchema.d.mts.map +1 -0
- package/dist/EffectSchema.mjs +176 -0
- package/dist/EffectSchema.mjs.map +1 -0
- package/dist/FractionalIndex.cjs +365 -0
- package/dist/FractionalIndex.mjs +364 -0
- package/dist/FractionalIndex.mjs.map +1 -0
- package/dist/Operation.cjs +53 -0
- package/dist/Operation.d.cts +39 -0
- package/dist/Operation.d.cts.map +1 -0
- package/dist/Operation.d.mts +39 -0
- package/dist/Operation.d.mts.map +1 -0
- package/dist/Operation.mjs +46 -0
- package/dist/Operation.mjs.map +1 -0
- package/dist/OperationDefinition.cjs +13 -0
- package/dist/OperationDefinition.d.cts +12 -0
- package/dist/OperationDefinition.d.cts.map +1 -0
- package/dist/OperationDefinition.d.mts +12 -0
- package/dist/OperationDefinition.d.mts.map +1 -0
- package/dist/OperationDefinition.mjs +13 -0
- package/dist/OperationDefinition.mjs.map +1 -0
- package/dist/OperationPath.cjs +148 -0
- package/dist/OperationPath.d.cts +60 -0
- package/dist/OperationPath.d.cts.map +1 -0
- package/dist/OperationPath.d.mts +60 -0
- package/dist/OperationPath.d.mts.map +1 -0
- package/dist/OperationPath.mjs +138 -0
- package/dist/OperationPath.mjs.map +1 -0
- package/dist/{Presence-gWrmGBeu.cjs → Presence.cjs} +4 -39
- package/dist/{Presence-N8u7Eppr.d.mts → Presence.d.cts} +2 -2
- package/dist/Presence.d.cts.map +1 -0
- package/dist/{Presence-DKKP4v5X.d.cts → Presence.d.mts} +2 -2
- package/dist/Presence.d.mts.map +1 -0
- package/dist/{Presence-DdMVKcOv.mjs → Presence.mjs} +3 -28
- package/dist/Presence.mjs.map +1 -0
- package/dist/Primitive.cjs +52 -0
- package/dist/Primitive.d.cts +20 -0
- package/dist/Primitive.d.cts.map +1 -0
- package/dist/Primitive.d.mts +20 -0
- package/dist/Primitive.d.mts.map +1 -0
- package/dist/Primitive.mjs +48 -0
- package/dist/Primitive.mjs.map +1 -0
- package/dist/ProxyEnvironment.cjs +34 -0
- package/dist/ProxyEnvironment.d.cts +31 -0
- package/dist/ProxyEnvironment.d.cts.map +1 -0
- package/dist/ProxyEnvironment.d.mts +31 -0
- package/dist/ProxyEnvironment.d.mts.map +1 -0
- package/dist/ProxyEnvironment.mjs +29 -0
- package/dist/ProxyEnvironment.mjs.map +1 -0
- package/dist/Transaction.cjs +66 -0
- package/dist/Transaction.d.cts +56 -0
- package/dist/Transaction.d.cts.map +1 -0
- package/dist/Transaction.d.mts +56 -0
- package/dist/Transaction.d.mts.map +1 -0
- package/dist/Transaction.mjs +58 -0
- package/dist/Transaction.mjs.map +1 -0
- package/dist/Transform.cjs +11 -0
- package/dist/Transform.d.cts +21 -0
- package/dist/Transform.d.cts.map +1 -0
- package/dist/Transform.d.mts +21 -0
- package/dist/Transform.d.mts.map +1 -0
- package/dist/Transform.mjs +6 -0
- package/dist/Transform.mjs.map +1 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs +14 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs +27 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.cjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPrimitive.mjs +16 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.cjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/toPropertyKey.mjs +11 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.cjs +18 -0
- package/dist/_virtual/_@oxc-project_runtime@0.103.0/helpers/typeof.mjs +12 -0
- package/dist/_virtual/rolldown_runtime.cjs +43 -0
- package/dist/{chunk-CLMFDpHK.mjs → _virtual/rolldown_runtime.mjs} +1 -1
- package/dist/client/ClientDocument.cjs +590 -0
- package/dist/client/ClientDocument.d.cts +161 -0
- package/dist/client/ClientDocument.d.cts.map +1 -0
- package/dist/client/ClientDocument.d.mts +162 -0
- package/dist/client/ClientDocument.d.mts.map +1 -0
- package/dist/client/ClientDocument.mjs +586 -0
- package/dist/client/ClientDocument.mjs.map +1 -0
- package/dist/client/Rebase.cjs +204 -0
- package/dist/client/Rebase.d.cts +87 -0
- package/dist/client/Rebase.d.cts.map +1 -0
- package/dist/client/Rebase.d.mts +88 -0
- package/dist/client/Rebase.d.mts.map +1 -0
- package/dist/client/Rebase.mjs +198 -0
- package/dist/client/Rebase.mjs.map +1 -0
- package/dist/client/StateMonitor.cjs +133 -0
- package/dist/client/StateMonitor.d.cts +122 -0
- package/dist/client/StateMonitor.d.cts.map +1 -0
- package/dist/client/StateMonitor.d.mts +122 -0
- package/dist/client/StateMonitor.d.mts.map +1 -0
- package/dist/client/StateMonitor.mjs +129 -0
- package/dist/client/StateMonitor.mjs.map +1 -0
- package/dist/client/Transport.cjs +11 -0
- package/dist/client/Transport.d.cts +237 -0
- package/dist/client/Transport.d.cts.map +1 -0
- package/dist/client/Transport.d.mts +237 -0
- package/dist/client/Transport.d.mts.map +1 -0
- package/dist/client/Transport.mjs +6 -0
- package/dist/client/Transport.mjs.map +1 -0
- package/dist/client/WebSocketTransport.cjs +396 -0
- package/dist/client/WebSocketTransport.d.cts +29 -0
- package/dist/client/WebSocketTransport.d.cts.map +1 -0
- package/dist/client/WebSocketTransport.d.mts +29 -0
- package/dist/client/WebSocketTransport.d.mts.map +1 -0
- package/dist/client/WebSocketTransport.mjs +392 -0
- package/dist/client/WebSocketTransport.mjs.map +1 -0
- package/dist/client/errors.cjs +135 -0
- package/dist/client/errors.d.cts +87 -0
- package/dist/client/errors.d.cts.map +1 -0
- package/dist/client/errors.d.mts +87 -0
- package/dist/client/errors.d.mts.map +1 -0
- package/dist/client/errors.mjs +127 -0
- package/dist/client/errors.mjs.map +1 -0
- package/dist/client/index.cjs +22 -1424
- package/dist/client/index.d.cts +8 -692
- package/dist/client/index.d.mts +8 -692
- package/dist/client/index.mjs +9 -1413
- package/dist/index.cjs +20 -2973
- package/dist/index.d.cts +12 -419
- package/dist/index.d.mts +12 -419
- package/dist/index.mjs +13 -2968
- package/dist/primitives/Array.cjs +302 -0
- package/dist/primitives/Array.d.cts +95 -0
- package/dist/primitives/Array.d.cts.map +1 -0
- package/dist/primitives/Array.d.mts +95 -0
- package/dist/primitives/Array.d.mts.map +1 -0
- package/dist/primitives/Array.mjs +301 -0
- package/dist/primitives/Array.mjs.map +1 -0
- package/dist/primitives/Boolean.cjs +95 -0
- package/dist/primitives/Boolean.d.cts +44 -0
- package/dist/primitives/Boolean.d.cts.map +1 -0
- package/dist/primitives/Boolean.d.mts +44 -0
- package/dist/primitives/Boolean.d.mts.map +1 -0
- package/dist/primitives/Boolean.mjs +94 -0
- package/dist/primitives/Boolean.mjs.map +1 -0
- package/dist/primitives/Either.cjs +200 -0
- package/dist/primitives/Either.d.cts +113 -0
- package/dist/primitives/Either.d.cts.map +1 -0
- package/dist/primitives/Either.d.mts +113 -0
- package/dist/primitives/Either.d.mts.map +1 -0
- package/dist/primitives/Either.mjs +199 -0
- package/dist/primitives/Either.mjs.map +1 -0
- package/dist/primitives/Lazy.cjs +46 -0
- package/dist/primitives/Lazy.d.cts +46 -0
- package/dist/primitives/Lazy.d.cts.map +1 -0
- package/dist/primitives/Lazy.d.mts +46 -0
- package/dist/primitives/Lazy.d.mts.map +1 -0
- package/dist/primitives/Lazy.mjs +46 -0
- package/dist/primitives/Lazy.mjs.map +1 -0
- package/dist/primitives/Literal.cjs +91 -0
- package/dist/primitives/Literal.d.cts +46 -0
- package/dist/primitives/Literal.d.cts.map +1 -0
- package/dist/primitives/Literal.d.mts +46 -0
- package/dist/primitives/Literal.d.mts.map +1 -0
- package/dist/primitives/Literal.mjs +90 -0
- package/dist/primitives/Literal.mjs.map +1 -0
- package/dist/primitives/Number.cjs +115 -0
- package/dist/primitives/Number.d.cts +54 -0
- package/dist/primitives/Number.d.cts.map +1 -0
- package/dist/primitives/Number.d.mts +54 -0
- package/dist/primitives/Number.d.mts.map +1 -0
- package/dist/primitives/Number.mjs +114 -0
- package/dist/primitives/Number.mjs.map +1 -0
- package/dist/primitives/String.cjs +127 -0
- package/dist/primitives/String.d.cts +56 -0
- package/dist/primitives/String.d.cts.map +1 -0
- package/dist/primitives/String.d.mts +56 -0
- package/dist/primitives/String.d.mts.map +1 -0
- package/dist/primitives/String.mjs +126 -0
- package/dist/primitives/String.mjs.map +1 -0
- package/dist/primitives/Struct.cjs +207 -0
- package/dist/primitives/Struct.d.cts +96 -0
- package/dist/primitives/Struct.d.cts.map +1 -0
- package/dist/primitives/Struct.d.mts +97 -0
- package/dist/primitives/Struct.d.mts.map +1 -0
- package/dist/primitives/Struct.mjs +206 -0
- package/dist/primitives/Struct.mjs.map +1 -0
- package/dist/primitives/Tree.cjs +575 -0
- package/dist/primitives/Tree.d.cts +185 -0
- package/dist/primitives/Tree.d.cts.map +1 -0
- package/dist/primitives/Tree.d.mts +185 -0
- package/dist/primitives/Tree.d.mts.map +1 -0
- package/dist/primitives/Tree.mjs +574 -0
- package/dist/primitives/Tree.mjs.map +1 -0
- package/dist/primitives/TreeNode.cjs +73 -0
- package/dist/primitives/TreeNode.d.cts +92 -0
- package/dist/primitives/TreeNode.d.cts.map +1 -0
- package/dist/primitives/TreeNode.d.mts +93 -0
- package/dist/primitives/TreeNode.d.mts.map +1 -0
- package/dist/primitives/TreeNode.mjs +72 -0
- package/dist/primitives/TreeNode.mjs.map +1 -0
- package/dist/primitives/Union.cjs +170 -0
- package/dist/primitives/Union.d.cts +81 -0
- package/dist/primitives/Union.d.cts.map +1 -0
- package/dist/primitives/Union.d.mts +81 -0
- package/dist/primitives/Union.d.mts.map +1 -0
- package/dist/primitives/Union.mjs +169 -0
- package/dist/primitives/Union.mjs.map +1 -0
- package/dist/primitives/shared.cjs +60 -0
- package/dist/primitives/shared.d.cts +147 -0
- package/dist/primitives/shared.d.cts.map +1 -0
- package/dist/primitives/shared.d.mts +147 -0
- package/dist/primitives/shared.d.mts.map +1 -0
- package/dist/primitives/shared.mjs +58 -0
- package/dist/primitives/shared.mjs.map +1 -0
- package/dist/server/ServerDocument.cjs +110 -0
- package/dist/server/ServerDocument.d.cts +98 -0
- package/dist/server/ServerDocument.d.cts.map +1 -0
- package/dist/server/ServerDocument.d.mts +99 -0
- package/dist/server/ServerDocument.d.mts.map +1 -0
- package/dist/server/ServerDocument.mjs +106 -0
- package/dist/server/ServerDocument.mjs.map +1 -0
- package/dist/server/errors.cjs +85 -0
- package/dist/server/errors.d.cts +53 -0
- package/dist/server/errors.d.cts.map +1 -0
- package/dist/server/errors.d.mts +53 -0
- package/dist/server/errors.d.mts.map +1 -0
- package/dist/server/errors.mjs +81 -0
- package/dist/server/errors.mjs.map +1 -0
- package/dist/server/index.cjs +9 -185
- package/dist/server/index.d.cts +3 -148
- package/dist/server/index.d.mts +3 -148
- package/dist/server/index.mjs +3 -181
- package/dist/types/index.cjs +16 -0
- package/dist/types/index.d.cts +16 -0
- package/dist/types/index.d.cts.map +1 -0
- package/dist/types/index.d.mts +16 -0
- package/dist/types/index.d.mts.map +1 -0
- package/dist/types/index.mjs +12 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/utils/tree-helpers.cjs +443 -0
- package/dist/utils/tree-helpers.d.cts +280 -0
- package/dist/utils/tree-helpers.d.cts.map +1 -0
- package/dist/utils/tree-helpers.d.mts +280 -0
- package/dist/utils/tree-helpers.d.mts.map +1 -0
- package/dist/utils/tree-helpers.mjs +439 -0
- package/dist/utils/tree-helpers.mjs.map +1 -0
- package/package.json +2 -2
- package/tsdown.config.ts +1 -1
- package/dist/Document-ChuFrTk1.cjs +0 -571
- package/dist/Document-CwiAFTIq.mjs +0 -438
- package/dist/Document-CwiAFTIq.mjs.map +0 -1
- package/dist/Presence-DKKP4v5X.d.cts.map +0 -1
- package/dist/Presence-DdMVKcOv.mjs.map +0 -1
- package/dist/Presence-N8u7Eppr.d.mts.map +0 -1
- package/dist/Primitive-DqQFc3Gu.d.mts +0 -1180
- package/dist/Primitive-DqQFc3Gu.d.mts.map +0 -1
- package/dist/Primitive-awpEjnKL.d.cts +0 -1180
- package/dist/Primitive-awpEjnKL.d.cts.map +0 -1
- package/dist/client/index.d.cts.map +0 -1
- package/dist/client/index.d.mts.map +0 -1
- package/dist/client/index.mjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/server/index.d.cts.map +0 -1
- package/dist/server/index.d.mts.map +0 -1
- package/dist/server/index.mjs.map +0 -1
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { OperationPath } from "../OperationPath.cjs";
|
|
2
|
+
import { OperationDefinition } from "../OperationDefinition.cjs";
|
|
3
|
+
import { Operation } from "../Operation.cjs";
|
|
4
|
+
import { ProxyEnvironment } from "../ProxyEnvironment.cjs";
|
|
5
|
+
import { TransformResult } from "../Transform.cjs";
|
|
6
|
+
|
|
7
|
+
//#region src/primitives/shared.d.ts
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base interface that all primitives must implement.
|
|
11
|
+
* Provides type inference helpers and internal operations.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam TState - The state type this primitive holds
|
|
14
|
+
* @typeParam TProxy - The proxy type for interacting with this primitive
|
|
15
|
+
* @typeParam TDefined - Whether the value is guaranteed to be defined (via required() or default())
|
|
16
|
+
* @typeParam THasDefault - Whether this primitive has a default value
|
|
17
|
+
*/
|
|
18
|
+
interface Primitive<TState, TProxy, TRequired extends boolean = false, THasDefault extends boolean = false, TSetInput = unknown, TUpdateInput = unknown> {
|
|
19
|
+
readonly _tag: string;
|
|
20
|
+
readonly _State: TState;
|
|
21
|
+
readonly _Proxy: TProxy;
|
|
22
|
+
readonly _TRequired: TRequired;
|
|
23
|
+
readonly _THasDefault: THasDefault;
|
|
24
|
+
readonly _internal: PrimitiveInternal<TState, TProxy>;
|
|
25
|
+
readonly TSetInput: TSetInput;
|
|
26
|
+
readonly TUpdateInput: TUpdateInput;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Internal operations that each primitive must provide.
|
|
30
|
+
*/
|
|
31
|
+
interface PrimitiveInternal<TState, TProxy> {
|
|
32
|
+
/** Creates a proxy for generating operations */
|
|
33
|
+
readonly createProxy: (env: ProxyEnvironment, path: OperationPath) => TProxy;
|
|
34
|
+
/** Applies an operation to the current state, returning the new state */
|
|
35
|
+
readonly applyOperation: (state: TState | undefined, operation: Operation<any, any, any>) => TState;
|
|
36
|
+
/** Returns the initial/default state for this primitive */
|
|
37
|
+
readonly getInitialState: () => TState | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Transforms a client operation against a server operation.
|
|
40
|
+
* Used for operational transformation (OT) conflict resolution.
|
|
41
|
+
*
|
|
42
|
+
* @param clientOp - The client's operation to transform
|
|
43
|
+
* @param serverOp - The server's operation that has already been applied
|
|
44
|
+
* @returns TransformResult indicating how the client operation should be handled
|
|
45
|
+
*/
|
|
46
|
+
readonly transformOperation: (clientOp: Operation<any, any, any>, serverOp: Operation<any, any, any>) => TransformResult;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Any primitive type - used for generic constraints.
|
|
50
|
+
*/
|
|
51
|
+
type AnyPrimitive = Primitive<any, any, any, any>;
|
|
52
|
+
/**
|
|
53
|
+
* Infer the state type from a primitive.
|
|
54
|
+
*/
|
|
55
|
+
type InferState<T> = T extends Primitive<infer S, any, any, any> ? S : never;
|
|
56
|
+
/**
|
|
57
|
+
* Infer the proxy type from a primitive.
|
|
58
|
+
*/
|
|
59
|
+
type InferProxy<T> = T extends Primitive<any, infer P, any, any> ? P : never;
|
|
60
|
+
/**
|
|
61
|
+
* Infer the SetInput type from a primitive.
|
|
62
|
+
* Works with both Primitive interface types and types with a TSetInput property (like TreeNodePrimitive).
|
|
63
|
+
*/
|
|
64
|
+
type InferSetInput<T> = T extends Primitive<any, any, any, any, infer S, any> ? S : T extends {
|
|
65
|
+
TSetInput: infer S;
|
|
66
|
+
} ? S : never;
|
|
67
|
+
/**
|
|
68
|
+
* Infer the UpdateInput type from a primitive.
|
|
69
|
+
* Works with both Primitive interface types and types with a TUpdateInput property (like TreeNodePrimitive).
|
|
70
|
+
*/
|
|
71
|
+
type InferUpdateInput<T> = T extends Primitive<any, any, any, any, any, infer U> ? U : T extends {
|
|
72
|
+
TUpdateInput: infer U;
|
|
73
|
+
} ? U : never;
|
|
74
|
+
/**
|
|
75
|
+
* Helper type to conditionally add undefined based on TRequired and THasDefault.
|
|
76
|
+
* When TRequired is false and THasDefault is false, the value may be undefined.
|
|
77
|
+
* Otherwise, the value is guaranteed to be defined.
|
|
78
|
+
*/
|
|
79
|
+
type MaybeUndefined<T, TRequired extends boolean, THasDefault extends boolean> = TRequired extends false ? THasDefault extends false ? Optional<T> : T : T;
|
|
80
|
+
type Optional<T> = T | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Helper type to conditionally add undefined based on TRequired and THasDefault.
|
|
83
|
+
* When TRequired is true and THasDefault is false, the value must be provided.
|
|
84
|
+
* Otherwise, the value may be undefined.
|
|
85
|
+
*/
|
|
86
|
+
type NeedsValue<T, TRequired extends boolean, THasDefault extends boolean> = TRequired extends true ? THasDefault extends false ? T : Optional<T> : Optional<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Infer the snapshot type from a primitive.
|
|
89
|
+
* The snapshot is a readonly, type-safe structure suitable for rendering.
|
|
90
|
+
*/
|
|
91
|
+
type InferSnapshot<T> = T extends Primitive<any, infer P, any, any> ? P extends {
|
|
92
|
+
toSnapshot(): infer S;
|
|
93
|
+
} ? S : never : never;
|
|
94
|
+
/**
|
|
95
|
+
* Extract THasDefault from a primitive.
|
|
96
|
+
*/
|
|
97
|
+
type HasDefault<T> = T extends Primitive<any, any, any, infer H> ? H : false;
|
|
98
|
+
/**
|
|
99
|
+
* Extract TDefined from a primitive.
|
|
100
|
+
*/
|
|
101
|
+
type IsDefined<T> = T extends Primitive<any, any, infer D, any> ? D : false;
|
|
102
|
+
/**
|
|
103
|
+
* Infer whether a primitive is required.
|
|
104
|
+
* Alias for IsDefined for clarity.
|
|
105
|
+
*/
|
|
106
|
+
type IsRequired<T> = IsDefined<T>;
|
|
107
|
+
declare class ValidationError extends Error {
|
|
108
|
+
readonly _tag = "ValidationError";
|
|
109
|
+
constructor(message: string);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A validator that checks a value and returns whether it's valid.
|
|
113
|
+
*/
|
|
114
|
+
interface Validator<T> {
|
|
115
|
+
readonly validate: (value: T) => boolean;
|
|
116
|
+
readonly message: string;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Runs all validators against a value, throwing ValidationError if any fail.
|
|
120
|
+
*/
|
|
121
|
+
declare function runValidators<T>(value: T, validators: readonly {
|
|
122
|
+
validate: (value: T) => boolean;
|
|
123
|
+
message: string;
|
|
124
|
+
}[]): void;
|
|
125
|
+
/**
|
|
126
|
+
* Checks if an operation is compatible with the given operation definitions.
|
|
127
|
+
* @param operation - The operation to check.
|
|
128
|
+
* @param operationDefinitions - The operation definitions to check against.
|
|
129
|
+
* @returns True if the operation is compatible, false otherwise.
|
|
130
|
+
*/
|
|
131
|
+
declare function isCompatibleOperation(operation: Operation<any, any, any>, operationDefinitions: Record<string, OperationDefinition<any, any, any>>): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Applies default values to a partial input, recursively handling nested structs.
|
|
134
|
+
*
|
|
135
|
+
* Uses a two-layer approach:
|
|
136
|
+
* 1. First, get the struct's initial state (which includes struct-level defaults)
|
|
137
|
+
* 2. Then, layer the provided values on top
|
|
138
|
+
* 3. Finally, ensure nested structs are recursively processed
|
|
139
|
+
*
|
|
140
|
+
* @param primitive - The primitive definition containing field information
|
|
141
|
+
* @param value - The partial value provided by the user
|
|
142
|
+
* @returns The value with defaults applied for missing fields
|
|
143
|
+
*/
|
|
144
|
+
declare function applyDefaults<T extends AnyPrimitive>(primitive: T, value: Partial<InferState<T>>): InferState<T>;
|
|
145
|
+
//#endregion
|
|
146
|
+
export { AnyPrimitive, HasDefault, InferProxy, InferSetInput, InferSnapshot, InferState, InferUpdateInput, IsDefined, IsRequired, MaybeUndefined, NeedsValue, Optional, Primitive, PrimitiveInternal, ValidationError, Validator, applyDefaults, isCompatibleOperation, runValidators };
|
|
147
|
+
//# sourceMappingURL=shared.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.cts","names":[],"sources":["../../src/primitives/shared.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AAmBA;;;;;;;AAMwB,UANP,SAMO,CAAA,MAAA,EAAA,MAAA,EAAA,kBAAA,OAAA,GAAA,KAAA,EAAA,oBAAA,OAAA,GAAA,KAAA,EAAA,YAAA,OAAA,EAAA,eAAA,OAAA,CAAA,CAAA;EACA,SAAA,IAAA,EAAA,MAAA;EACG,SAAA,MAAA,EANN,MAMM;EAAY,SAAA,MAAA,EALlB,MAKkB;EAMpB,SAAA,UAAA,EAVM,SAUW;EAEJ,SAAA,YAAA,EAXL,WAWK;EAAyC,SAAA,SAAA,EAVjD,iBAUiD,CAV/B,MAU+B,EAVvB,MAUuB,CAAA;EAAgC,SAAA,SAAA,EATjF,SASiF;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AAA,CAAA,GA1BjK,CA0BiK,SAAA;EAMvJ,SAAA,EAAA,KAAa,EAAA;AAOzB,CAAA,GAAY,CAAA,GAAA,KAAA;AAKZ;AAMA;AAOA;AAeA;AASc,KA1EF,gBA0Ee,CAAA,CAAA,CAAA,GAzEzB,CAyEoC,SAzE1B,SAyEuE,CAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EAAA,GAAA,EAAA,KAAA,EAAA,CAAA,GAAA,CAAA,GAxEjF,CAwEiF,SAAA;EAcrE,YAAA,EAAA,KAAA,EAAA;CAAiC,GAAA,CAAA,GAAA,KAAA;;;;AAqBjD;;AACa,KApGC,cAoGD,CAAA,CAAA,EAAA,kBAAA,OAAA,EAAA,oBAAA,OAAA,CAAA,GApG6E,SAoG7E,SAAA,KAAA,GApGuG,WAoGvG,SAAA,KAAA,GApGmI,QAoGnI,CApG4I,CAoG5I,CAAA,GApGiJ,CAoGjJ,GApGqJ,CAoGrJ;AACe,KAnGd,QAmGc,CAAA,CAAA,CAAA,GAnGA,CAmGA,GAAA,SAAA;;;;;;KA5Fd,wEAAwE,yBAAyB,4BAA4B,IAAI,SAAS,KAAK,SAAS;;;;;KAMxJ,mBAAmB,UAAU;;;;;;KAO7B,gBAAgB,UAAU;;;;KAK1B,eAAe,UAAU;;;;;KAMzB,gBAAgB,UAAU;cAOzB,eAAA,SAAwB,KAAA;;;;;;;UAepB;6BACY;;;;;;iBAQf,wBAAwB;oBAA4C;;;;;;;;;iBAcpE,qBAAA,YAAiC,gDAA0D,eAAe;;;;;;;;;;;;;iBAqB1G,wBAAwB,yBAC3B,UACJ,QAAQ,WAAW,MACzB,WAAW"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { OperationPath } from "../OperationPath.mjs";
|
|
2
|
+
import { OperationDefinition } from "../OperationDefinition.mjs";
|
|
3
|
+
import { Operation } from "../Operation.mjs";
|
|
4
|
+
import { ProxyEnvironment } from "../ProxyEnvironment.mjs";
|
|
5
|
+
import { TransformResult } from "../Transform.mjs";
|
|
6
|
+
|
|
7
|
+
//#region src/primitives/shared.d.ts
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base interface that all primitives must implement.
|
|
11
|
+
* Provides type inference helpers and internal operations.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam TState - The state type this primitive holds
|
|
14
|
+
* @typeParam TProxy - The proxy type for interacting with this primitive
|
|
15
|
+
* @typeParam TDefined - Whether the value is guaranteed to be defined (via required() or default())
|
|
16
|
+
* @typeParam THasDefault - Whether this primitive has a default value
|
|
17
|
+
*/
|
|
18
|
+
interface Primitive<TState, TProxy, TRequired extends boolean = false, THasDefault extends boolean = false, TSetInput = unknown, TUpdateInput = unknown> {
|
|
19
|
+
readonly _tag: string;
|
|
20
|
+
readonly _State: TState;
|
|
21
|
+
readonly _Proxy: TProxy;
|
|
22
|
+
readonly _TRequired: TRequired;
|
|
23
|
+
readonly _THasDefault: THasDefault;
|
|
24
|
+
readonly _internal: PrimitiveInternal<TState, TProxy>;
|
|
25
|
+
readonly TSetInput: TSetInput;
|
|
26
|
+
readonly TUpdateInput: TUpdateInput;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Internal operations that each primitive must provide.
|
|
30
|
+
*/
|
|
31
|
+
interface PrimitiveInternal<TState, TProxy> {
|
|
32
|
+
/** Creates a proxy for generating operations */
|
|
33
|
+
readonly createProxy: (env: ProxyEnvironment, path: OperationPath) => TProxy;
|
|
34
|
+
/** Applies an operation to the current state, returning the new state */
|
|
35
|
+
readonly applyOperation: (state: TState | undefined, operation: Operation<any, any, any>) => TState;
|
|
36
|
+
/** Returns the initial/default state for this primitive */
|
|
37
|
+
readonly getInitialState: () => TState | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Transforms a client operation against a server operation.
|
|
40
|
+
* Used for operational transformation (OT) conflict resolution.
|
|
41
|
+
*
|
|
42
|
+
* @param clientOp - The client's operation to transform
|
|
43
|
+
* @param serverOp - The server's operation that has already been applied
|
|
44
|
+
* @returns TransformResult indicating how the client operation should be handled
|
|
45
|
+
*/
|
|
46
|
+
readonly transformOperation: (clientOp: Operation<any, any, any>, serverOp: Operation<any, any, any>) => TransformResult;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Any primitive type - used for generic constraints.
|
|
50
|
+
*/
|
|
51
|
+
type AnyPrimitive = Primitive<any, any, any, any>;
|
|
52
|
+
/**
|
|
53
|
+
* Infer the state type from a primitive.
|
|
54
|
+
*/
|
|
55
|
+
type InferState<T> = T extends Primitive<infer S, any, any, any> ? S : never;
|
|
56
|
+
/**
|
|
57
|
+
* Infer the proxy type from a primitive.
|
|
58
|
+
*/
|
|
59
|
+
type InferProxy<T> = T extends Primitive<any, infer P, any, any> ? P : never;
|
|
60
|
+
/**
|
|
61
|
+
* Infer the SetInput type from a primitive.
|
|
62
|
+
* Works with both Primitive interface types and types with a TSetInput property (like TreeNodePrimitive).
|
|
63
|
+
*/
|
|
64
|
+
type InferSetInput<T> = T extends Primitive<any, any, any, any, infer S, any> ? S : T extends {
|
|
65
|
+
TSetInput: infer S;
|
|
66
|
+
} ? S : never;
|
|
67
|
+
/**
|
|
68
|
+
* Infer the UpdateInput type from a primitive.
|
|
69
|
+
* Works with both Primitive interface types and types with a TUpdateInput property (like TreeNodePrimitive).
|
|
70
|
+
*/
|
|
71
|
+
type InferUpdateInput<T> = T extends Primitive<any, any, any, any, any, infer U> ? U : T extends {
|
|
72
|
+
TUpdateInput: infer U;
|
|
73
|
+
} ? U : never;
|
|
74
|
+
/**
|
|
75
|
+
* Helper type to conditionally add undefined based on TRequired and THasDefault.
|
|
76
|
+
* When TRequired is false and THasDefault is false, the value may be undefined.
|
|
77
|
+
* Otherwise, the value is guaranteed to be defined.
|
|
78
|
+
*/
|
|
79
|
+
type MaybeUndefined<T, TRequired extends boolean, THasDefault extends boolean> = TRequired extends false ? THasDefault extends false ? Optional<T> : T : T;
|
|
80
|
+
type Optional<T> = T | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Helper type to conditionally add undefined based on TRequired and THasDefault.
|
|
83
|
+
* When TRequired is true and THasDefault is false, the value must be provided.
|
|
84
|
+
* Otherwise, the value may be undefined.
|
|
85
|
+
*/
|
|
86
|
+
type NeedsValue<T, TRequired extends boolean, THasDefault extends boolean> = TRequired extends true ? THasDefault extends false ? T : Optional<T> : Optional<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Infer the snapshot type from a primitive.
|
|
89
|
+
* The snapshot is a readonly, type-safe structure suitable for rendering.
|
|
90
|
+
*/
|
|
91
|
+
type InferSnapshot<T> = T extends Primitive<any, infer P, any, any> ? P extends {
|
|
92
|
+
toSnapshot(): infer S;
|
|
93
|
+
} ? S : never : never;
|
|
94
|
+
/**
|
|
95
|
+
* Extract THasDefault from a primitive.
|
|
96
|
+
*/
|
|
97
|
+
type HasDefault<T> = T extends Primitive<any, any, any, infer H> ? H : false;
|
|
98
|
+
/**
|
|
99
|
+
* Extract TDefined from a primitive.
|
|
100
|
+
*/
|
|
101
|
+
type IsDefined<T> = T extends Primitive<any, any, infer D, any> ? D : false;
|
|
102
|
+
/**
|
|
103
|
+
* Infer whether a primitive is required.
|
|
104
|
+
* Alias for IsDefined for clarity.
|
|
105
|
+
*/
|
|
106
|
+
type IsRequired<T> = IsDefined<T>;
|
|
107
|
+
declare class ValidationError extends Error {
|
|
108
|
+
readonly _tag = "ValidationError";
|
|
109
|
+
constructor(message: string);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A validator that checks a value and returns whether it's valid.
|
|
113
|
+
*/
|
|
114
|
+
interface Validator<T> {
|
|
115
|
+
readonly validate: (value: T) => boolean;
|
|
116
|
+
readonly message: string;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Runs all validators against a value, throwing ValidationError if any fail.
|
|
120
|
+
*/
|
|
121
|
+
declare function runValidators<T>(value: T, validators: readonly {
|
|
122
|
+
validate: (value: T) => boolean;
|
|
123
|
+
message: string;
|
|
124
|
+
}[]): void;
|
|
125
|
+
/**
|
|
126
|
+
* Checks if an operation is compatible with the given operation definitions.
|
|
127
|
+
* @param operation - The operation to check.
|
|
128
|
+
* @param operationDefinitions - The operation definitions to check against.
|
|
129
|
+
* @returns True if the operation is compatible, false otherwise.
|
|
130
|
+
*/
|
|
131
|
+
declare function isCompatibleOperation(operation: Operation<any, any, any>, operationDefinitions: Record<string, OperationDefinition<any, any, any>>): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Applies default values to a partial input, recursively handling nested structs.
|
|
134
|
+
*
|
|
135
|
+
* Uses a two-layer approach:
|
|
136
|
+
* 1. First, get the struct's initial state (which includes struct-level defaults)
|
|
137
|
+
* 2. Then, layer the provided values on top
|
|
138
|
+
* 3. Finally, ensure nested structs are recursively processed
|
|
139
|
+
*
|
|
140
|
+
* @param primitive - The primitive definition containing field information
|
|
141
|
+
* @param value - The partial value provided by the user
|
|
142
|
+
* @returns The value with defaults applied for missing fields
|
|
143
|
+
*/
|
|
144
|
+
declare function applyDefaults<T extends AnyPrimitive>(primitive: T, value: Partial<InferState<T>>): InferState<T>;
|
|
145
|
+
//#endregion
|
|
146
|
+
export { AnyPrimitive, HasDefault, InferProxy, InferSetInput, InferSnapshot, InferState, InferUpdateInput, IsDefined, IsRequired, MaybeUndefined, NeedsValue, Optional, Primitive, PrimitiveInternal, ValidationError, Validator, applyDefaults, isCompatibleOperation, runValidators };
|
|
147
|
+
//# sourceMappingURL=shared.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.mts","names":[],"sources":["../../src/primitives/shared.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AAmBA;;;;;;;AAMwB,UANP,SAMO,CAAA,MAAA,EAAA,MAAA,EAAA,kBAAA,OAAA,GAAA,KAAA,EAAA,oBAAA,OAAA,GAAA,KAAA,EAAA,YAAA,OAAA,EAAA,eAAA,OAAA,CAAA,CAAA;EACA,SAAA,IAAA,EAAA,MAAA;EACG,S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MACzB,WAAW"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { _defineProperty } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.mjs";
|
|
2
|
+
import { _objectSpread2 } from "../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/primitives/shared.ts
|
|
5
|
+
var ValidationError = class extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
_defineProperty(this, "_tag", "ValidationError");
|
|
9
|
+
this.name = "ValidationError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Runs all validators against a value, throwing ValidationError if any fail.
|
|
14
|
+
*/
|
|
15
|
+
function runValidators(value, validators) {
|
|
16
|
+
for (const validator of validators) if (!validator.validate(value)) throw new ValidationError(validator.message);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Checks if an operation is compatible with the given operation definitions.
|
|
20
|
+
* @param operation - The operation to check.
|
|
21
|
+
* @param operationDefinitions - The operation definitions to check against.
|
|
22
|
+
* @returns True if the operation is compatible, false otherwise.
|
|
23
|
+
*/
|
|
24
|
+
function isCompatibleOperation(operation, operationDefinitions) {
|
|
25
|
+
return Object.values(operationDefinitions).some((value) => value.kind === operation.kind);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Applies default values to a partial input, recursively handling nested structs.
|
|
29
|
+
*
|
|
30
|
+
* Uses a two-layer approach:
|
|
31
|
+
* 1. First, get the struct's initial state (which includes struct-level defaults)
|
|
32
|
+
* 2. Then, layer the provided values on top
|
|
33
|
+
* 3. Finally, ensure nested structs are recursively processed
|
|
34
|
+
*
|
|
35
|
+
* @param primitive - The primitive definition containing field information
|
|
36
|
+
* @param value - The partial value provided by the user
|
|
37
|
+
* @returns The value with defaults applied for missing fields
|
|
38
|
+
*/
|
|
39
|
+
function applyDefaults(primitive, value) {
|
|
40
|
+
if (primitive._tag === "StructPrimitive") {
|
|
41
|
+
var _structPrimitive$_int;
|
|
42
|
+
const structPrimitive = primitive;
|
|
43
|
+
const result = _objectSpread2(_objectSpread2({}, (_structPrimitive$_int = structPrimitive._internal.getInitialState()) !== null && _structPrimitive$_int !== void 0 ? _structPrimitive$_int : {}), value);
|
|
44
|
+
for (const key in structPrimitive.fields) {
|
|
45
|
+
const fieldPrimitive = structPrimitive.fields[key];
|
|
46
|
+
if (result[key] === void 0) {
|
|
47
|
+
const fieldDefault = fieldPrimitive._internal.getInitialState();
|
|
48
|
+
if (fieldDefault !== void 0) result[key] = fieldDefault;
|
|
49
|
+
} else if (fieldPrimitive._tag === "StructPrimitive" && typeof result[key] === "object" && result[key] !== null) result[key] = applyDefaults(fieldPrimitive, result[key]);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
return value;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
export { ValidationError, applyDefaults, isCompatibleOperation, runValidators };
|
|
58
|
+
//# sourceMappingURL=shared.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.mjs","names":["result: Record<string, unknown>"],"sources":["../../src/primitives/shared.ts"],"sourcesContent":["import * as Operation from \"../Operation\";\nimport * as OperationDefinition from \"../OperationDefinition\";\nimport * as ProxyEnvironment from \"../ProxyEnvironment\";\nimport * as OperationPath from \"../OperationPath\";\nimport * as Transform from \"../Transform\";\n\n// =============================================================================\n// Primitive Interface & Type Utilities\n// =============================================================================\n\n/**\n * Base interface that all primitives must implement.\n * Provides type inference helpers and internal operations.\n * \n * @typeParam TState - The state type this primitive holds\n * @typeParam TProxy - The proxy type for interacting with this primitive\n * @typeParam TDefined - Whether the value is guaranteed to be defined (via required() or default())\n * @typeParam THasDefault - Whether this primitive has a default value\n */\nexport interface Primitive<TState, TProxy, TRequired extends boolean = false, THasDefault extends boolean = false, TSetInput = unknown, TUpdateInput = unknown> {\n readonly _tag: string;\n readonly _State: TState;\n readonly _Proxy: TProxy;\n readonly _TRequired: TRequired;\n readonly _THasDefault: THasDefault;\n readonly _internal: PrimitiveInternal<TState, TProxy>;\n readonly TSetInput: TSetInput;\n readonly TUpdateInput: TUpdateInput;\n }\n \n /**\n * Internal operations that each primitive must provide.\n */\n export interface PrimitiveInternal<TState, TProxy> {\n /** Creates a proxy for generating operations */\n readonly createProxy: (env: ProxyEnvironment.ProxyEnvironment, path: OperationPath.OperationPath) => TProxy;\n /** Applies an operation to the current state, returning the new state */\n readonly applyOperation: (state: TState | undefined, operation: Operation.Operation<any, any, any>) => TState;\n /** Returns the initial/default state for this primitive */\n readonly getInitialState: () => TState | undefined;\n /**\n * Transforms a client operation against a server operation.\n * Used for operational transformation (OT) conflict resolution.\n * \n * @param clientOp - The client's operation to transform\n * @param serverOp - The server's operation that has already been applied\n * @returns TransformResult indicating how the client operation should be handled\n */\n readonly transformOperation: (\n clientOp: Operation.Operation<any, any, any>,\n serverOp: Operation.Operation<any, any, any>\n ) => Transform.TransformResult;\n }\n \n /**\n * Any primitive type - used for generic constraints.\n */\n export type AnyPrimitive = Primitive<any, any, any, any>;\n \n /**\n * Infer the state type from a primitive.\n */\n export type InferState<T> = T extends Primitive<infer S, any, any, any> ? S : never;\n \n /**\n * Infer the proxy type from a primitive.\n */\n export type InferProxy<T> = T extends Primitive<any, infer P, any, any> ? P : never;\n\n /**\n * Infer the SetInput type from a primitive.\n * Works with both Primitive interface types and types with a TSetInput property (like TreeNodePrimitive).\n */\n export type InferSetInput<T> = \n T extends Primitive<any, any, any, any, infer S, any> ? S : \n T extends { TSetInput: infer S } ? S : \n never;\n\n /**\n * Infer the UpdateInput type from a primitive.\n * Works with both Primitive interface types and types with a TUpdateInput property (like TreeNodePrimitive).\n */\n export type InferUpdateInput<T> = \n T extends Primitive<any, any, any, any, any, infer U> ? U : \n T extends { TUpdateInput: infer U } ? U : \n never;\n \n /**\n * Helper type to conditionally add undefined based on TRequired and THasDefault.\n * When TRequired is false and THasDefault is false, the value may be undefined.\n * Otherwise, the value is guaranteed to be defined.\n */\n export type MaybeUndefined<T, TRequired extends boolean, THasDefault extends boolean> = TRequired extends false ? THasDefault extends false ? Optional<T> : T : T;\n\n export type Optional<T> = T | undefined;\n\n /**\n * Helper type to conditionally add undefined based on TRequired and THasDefault.\n * When TRequired is true and THasDefault is false, the value must be provided.\n * Otherwise, the value may be undefined.\n */\n export type NeedsValue<T, TRequired extends boolean, THasDefault extends boolean> = TRequired extends true ? THasDefault extends false ? T : Optional<T> : Optional<T>;\n \n /**\n * Infer the snapshot type from a primitive.\n * The snapshot is a readonly, type-safe structure suitable for rendering.\n */\n export type InferSnapshot<T> = T extends Primitive<any, infer P, any, any>\n ? P extends { toSnapshot(): infer S } ? S : never\n : never;\n\n /**\n * Extract THasDefault from a primitive.\n */\n export type HasDefault<T> = T extends Primitive<any, any, any, infer H> ? H : false;\n\n /**\n * Extract TDefined from a primitive.\n */\n export type IsDefined<T> = T extends Primitive<any, any, infer D, any> ? D : false;\n\n /**\n * Infer whether a primitive is required.\n * Alias for IsDefined for clarity.\n */\n export type IsRequired<T> = IsDefined<T>;\n\n\n // =============================================================================\n // Validation Errors\n // =============================================================================\n \n export class ValidationError extends Error {\n readonly _tag = \"ValidationError\";\n constructor(message: string) {\n super(message);\n this.name = \"ValidationError\";\n }\n }\n \n // =============================================================================\n // Validation Infrastructure\n // =============================================================================\n \n /**\n * A validator that checks a value and returns whether it's valid.\n */\n export interface Validator<T> {\n readonly validate: (value: T) => boolean;\n readonly message: string;\n }\n \n\n/**\n * Runs all validators against a value, throwing ValidationError if any fail.\n */\nexport function runValidators<T>(value: T, validators: readonly { validate: (value: T) => boolean; message: string }[]): void {\n for (const validator of validators) {\n if (!validator.validate(value)) {\n throw new ValidationError(validator.message);\n }\n }\n}\n\n/**\n * Checks if an operation is compatible with the given operation definitions.\n * @param operation - The operation to check.\n * @param operationDefinitions - The operation definitions to check against.\n * @returns True if the operation is compatible, false otherwise.\n */\nexport function isCompatibleOperation(operation: Operation.Operation<any, any, any>, operationDefinitions: Record<string, OperationDefinition.OperationDefinition<any, any, any>>) {\n const values = Object.values(operationDefinitions);\n return values.some(value => value.kind === operation.kind);\n}\n\n// =============================================================================\n// Default Value Utilities\n// =============================================================================\n\n/**\n * Applies default values to a partial input, recursively handling nested structs.\n * \n * Uses a two-layer approach:\n * 1. First, get the struct's initial state (which includes struct-level defaults)\n * 2. Then, layer the provided values on top\n * 3. Finally, ensure nested structs are recursively processed\n * \n * @param primitive - The primitive definition containing field information\n * @param value - The partial value provided by the user\n * @returns The value with defaults applied for missing fields\n */\nexport function applyDefaults<T extends AnyPrimitive>(\n primitive: T,\n value: Partial<InferState<T>>\n): InferState<T> {\n // Only structs need default merging\n if (primitive._tag === \"StructPrimitive\") {\n const structPrimitive = primitive as unknown as { \n fields: Record<string, AnyPrimitive>;\n _internal: { getInitialState: () => Record<string, unknown> | undefined };\n };\n \n // Start with the struct's initial state (struct-level default or field defaults)\n const structInitialState = structPrimitive._internal.getInitialState() ?? {};\n \n // Layer the provided values on top of initial state\n const result: Record<string, unknown> = { ...structInitialState, ...value };\n \n for (const key in structPrimitive.fields) {\n const fieldPrimitive = structPrimitive.fields[key]!;\n \n if (result[key] === undefined) {\n // Field still not provided after merging - try individual field default\n const fieldDefault = fieldPrimitive._internal.getInitialState();\n if (fieldDefault !== undefined) {\n result[key] = fieldDefault;\n }\n } else if (fieldPrimitive._tag === \"StructPrimitive\" && typeof result[key] === \"object\" && result[key] !== null) {\n // Recursively apply defaults to nested structs\n result[key] = applyDefaults(fieldPrimitive, result[key] as Partial<InferState<typeof fieldPrimitive>>);\n }\n }\n \n return result as InferState<T>;\n }\n \n // For non-struct primitives, return the value as-is\n return value as InferState<T>;\n}\n\n"],"mappings":";;;;AAoIE,IAAa,kBAAb,cAAqC,MAAM;CAEzC,YAAY,SAAiB;AAC3B,QAAM,QAAQ;wBAFP,QAAO;AAGd,OAAK,OAAO;;;;;;AAoBlB,SAAgB,cAAiB,OAAU,YAAmF;AAC5H,MAAK,MAAM,aAAa,WACtB,KAAI,CAAC,UAAU,SAAS,MAAM,CAC5B,OAAM,IAAI,gBAAgB,UAAU,QAAQ;;;;;;;;AAWlD,SAAgB,sBAAsB,WAA+C,sBAA8F;AAEjL,QADe,OAAO,OAAO,qBAAqB,CACpC,MAAK,UAAS,MAAM,SAAS,UAAU,KAAK;;;;;;;;;;;;;;AAmB5D,SAAgB,cACd,WACA,OACe;AAEf,KAAI,UAAU,SAAS,mBAAmB;;EACxC,MAAM,kBAAkB;EASxB,MAAMA,oEAHqB,gBAAgB,UAAU,iBAAiB,yEAAI,EAAE,GAGR;AAEpE,OAAK,MAAM,OAAO,gBAAgB,QAAQ;GACxC,MAAM,iBAAiB,gBAAgB,OAAO;AAE9C,OAAI,OAAO,SAAS,QAAW;IAE7B,MAAM,eAAe,eAAe,UAAU,iBAAiB;AAC/D,QAAI,iBAAiB,OACnB,QAAO,OAAO;cAEP,eAAe,SAAS,qBAAqB,OAAO,OAAO,SAAS,YAAY,OAAO,SAAS,KAEzG,QAAO,OAAO,cAAc,gBAAgB,OAAO,KAAmD;;AAI1G,SAAO;;AAIT,QAAO"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_Transaction = require('../Transaction.cjs');
|
|
3
|
+
const require_Document = require('../Document.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/server/ServerDocument.ts
|
|
6
|
+
var ServerDocument_exports = /* @__PURE__ */ require_rolldown_runtime.__export({ make: () => make });
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new ServerDocument for the given schema.
|
|
9
|
+
*/
|
|
10
|
+
const make = (options) => {
|
|
11
|
+
const { schema, initialState, initialVersion = 0, onBroadcast, onRejection, maxTransactionHistory = 1e3 } = options;
|
|
12
|
+
let _document = require_Document.make(schema, { initial: initialState });
|
|
13
|
+
let _version = initialVersion;
|
|
14
|
+
const _processedTransactions = /* @__PURE__ */ new Set();
|
|
15
|
+
const _transactionOrder = [];
|
|
16
|
+
/**
|
|
17
|
+
* Records a transaction as processed, maintaining the history limit.
|
|
18
|
+
*/
|
|
19
|
+
const recordTransaction = (transactionId) => {
|
|
20
|
+
_processedTransactions.add(transactionId);
|
|
21
|
+
_transactionOrder.push(transactionId);
|
|
22
|
+
while (_transactionOrder.length > maxTransactionHistory) {
|
|
23
|
+
const oldest = _transactionOrder.shift();
|
|
24
|
+
if (oldest) _processedTransactions.delete(oldest);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Validates that the transaction can be applied to the current state.
|
|
29
|
+
* Creates a temporary document and attempts to apply the operations.
|
|
30
|
+
*/
|
|
31
|
+
const validateTransaction = (transaction) => {
|
|
32
|
+
if (require_Transaction.isEmpty(transaction)) return {
|
|
33
|
+
valid: false,
|
|
34
|
+
reason: "Transaction is empty"
|
|
35
|
+
};
|
|
36
|
+
if (_processedTransactions.has(transaction.id)) return {
|
|
37
|
+
valid: false,
|
|
38
|
+
reason: "Transaction has already been processed"
|
|
39
|
+
};
|
|
40
|
+
const currentState = _document.get();
|
|
41
|
+
const tempDoc = require_Document.make(schema, { initial: currentState });
|
|
42
|
+
try {
|
|
43
|
+
tempDoc.apply(transaction.ops);
|
|
44
|
+
return { valid: true };
|
|
45
|
+
} catch (error) {
|
|
46
|
+
return {
|
|
47
|
+
valid: false,
|
|
48
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
schema,
|
|
54
|
+
get: () => {
|
|
55
|
+
return _document.get();
|
|
56
|
+
},
|
|
57
|
+
getVersion: () => {
|
|
58
|
+
return _version;
|
|
59
|
+
},
|
|
60
|
+
submit: (transaction) => {
|
|
61
|
+
const validation = validateTransaction(transaction);
|
|
62
|
+
if (!validation.valid) {
|
|
63
|
+
onRejection === null || onRejection === void 0 || onRejection(transaction.id, validation.reason);
|
|
64
|
+
return {
|
|
65
|
+
success: false,
|
|
66
|
+
reason: validation.reason
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
_document.apply(transaction.ops);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
73
|
+
onRejection === null || onRejection === void 0 || onRejection(transaction.id, reason);
|
|
74
|
+
return {
|
|
75
|
+
success: false,
|
|
76
|
+
reason
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
_version += 1;
|
|
80
|
+
recordTransaction(transaction.id);
|
|
81
|
+
onBroadcast({
|
|
82
|
+
type: "transaction",
|
|
83
|
+
transaction,
|
|
84
|
+
version: _version
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
success: true,
|
|
88
|
+
version: _version
|
|
89
|
+
};
|
|
90
|
+
},
|
|
91
|
+
getSnapshot: () => {
|
|
92
|
+
return {
|
|
93
|
+
type: "snapshot",
|
|
94
|
+
state: _document.get(),
|
|
95
|
+
version: _version
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
hasProcessed: (transactionId) => {
|
|
99
|
+
return _processedTransactions.has(transactionId);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
//#endregion
|
|
105
|
+
Object.defineProperty(exports, 'ServerDocument_exports', {
|
|
106
|
+
enumerable: true,
|
|
107
|
+
get: function () {
|
|
108
|
+
return ServerDocument_exports;
|
|
109
|
+
}
|
|
110
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Transaction } from "../Transaction.cjs";
|
|
2
|
+
import { AnyPrimitive, InferState } from "../primitives/shared.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/server/ServerDocument.d.ts
|
|
5
|
+
declare namespace ServerDocument_d_exports {
|
|
6
|
+
export { ErrorMessage, ServerDocument, ServerDocumentOptions, ServerMessage, SnapshotMessage, SubmitResult, TransactionMessage, make };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Message sent when broadcasting a committed transaction.
|
|
10
|
+
*/
|
|
11
|
+
interface TransactionMessage {
|
|
12
|
+
readonly type: "transaction";
|
|
13
|
+
readonly transaction: Transaction;
|
|
14
|
+
/** Server-assigned version number for ordering */
|
|
15
|
+
readonly version: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Message sent when a transaction is rejected.
|
|
19
|
+
*/
|
|
20
|
+
interface ErrorMessage {
|
|
21
|
+
readonly type: "error";
|
|
22
|
+
readonly transactionId: string;
|
|
23
|
+
readonly reason: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Message sent as a full state snapshot.
|
|
27
|
+
*/
|
|
28
|
+
interface SnapshotMessage {
|
|
29
|
+
readonly type: "snapshot";
|
|
30
|
+
readonly state: unknown;
|
|
31
|
+
readonly version: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Union of all server messages that can be broadcast.
|
|
35
|
+
*/
|
|
36
|
+
type ServerMessage = TransactionMessage | ErrorMessage | SnapshotMessage;
|
|
37
|
+
/**
|
|
38
|
+
* Result of submitting a transaction to the server.
|
|
39
|
+
*/
|
|
40
|
+
type SubmitResult = {
|
|
41
|
+
readonly success: true;
|
|
42
|
+
readonly version: number;
|
|
43
|
+
} | {
|
|
44
|
+
readonly success: false;
|
|
45
|
+
readonly reason: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Options for creating a ServerDocument.
|
|
49
|
+
*/
|
|
50
|
+
interface ServerDocumentOptions<TSchema extends AnyPrimitive> {
|
|
51
|
+
/** The schema defining the document structure */
|
|
52
|
+
readonly schema: TSchema;
|
|
53
|
+
/** Initial state (optional, will use schema defaults if not provided) */
|
|
54
|
+
readonly initialState?: InferState<TSchema>;
|
|
55
|
+
/** Initial version number (optional, defaults to 0) */
|
|
56
|
+
readonly initialVersion?: number;
|
|
57
|
+
/** Called when a transaction is successfully applied and should be broadcast */
|
|
58
|
+
readonly onBroadcast: (message: TransactionMessage) => void;
|
|
59
|
+
/** Called when a transaction is rejected (optional, for logging/metrics) */
|
|
60
|
+
readonly onRejection?: (transactionId: string, reason: string) => void;
|
|
61
|
+
/** Maximum number of processed transaction IDs to track for deduplication */
|
|
62
|
+
readonly maxTransactionHistory?: number;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A ServerDocument maintains the authoritative state and processes client transactions.
|
|
66
|
+
*/
|
|
67
|
+
interface ServerDocument<TSchema extends AnyPrimitive> {
|
|
68
|
+
/** The schema defining this document's structure */
|
|
69
|
+
readonly schema: TSchema;
|
|
70
|
+
/** Returns the current authoritative state */
|
|
71
|
+
get(): InferState<TSchema> | undefined;
|
|
72
|
+
/** Returns the current version number */
|
|
73
|
+
getVersion(): number;
|
|
74
|
+
/**
|
|
75
|
+
* Submits a transaction for processing.
|
|
76
|
+
* Validates and applies the transaction if valid, or rejects it with a reason.
|
|
77
|
+
* @param transaction - The transaction to process
|
|
78
|
+
* @returns SubmitResult indicating success with version or failure with reason
|
|
79
|
+
*/
|
|
80
|
+
submit(transaction: Transaction): SubmitResult;
|
|
81
|
+
/**
|
|
82
|
+
* Returns a snapshot of the current state and version.
|
|
83
|
+
* Used to initialize new clients or resync after drift.
|
|
84
|
+
*/
|
|
85
|
+
getSnapshot(): SnapshotMessage;
|
|
86
|
+
/**
|
|
87
|
+
* Checks if a transaction has already been processed.
|
|
88
|
+
* @param transactionId - The transaction ID to check
|
|
89
|
+
*/
|
|
90
|
+
hasProcessed(transactionId: string): boolean;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Creates a new ServerDocument for the given schema.
|
|
94
|
+
*/
|
|
95
|
+
declare const make: <TSchema extends AnyPrimitive>(options: ServerDocumentOptions<TSchema>) => ServerDocument<TSchema>;
|
|
96
|
+
//#endregion
|
|
97
|
+
export { ErrorMessage, ServerDocumentOptions, ServerDocument_d_exports, ServerMessage, SnapshotMessage, SubmitResult, TransactionMessage };
|
|
98
|
+
//# sourceMappingURL=ServerDocument.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ServerDocument.d.cts","names":[],"sources":["../../src/server/ServerDocument.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;UAWiB,kBAAA;;wBAEO;;;;;;;UAQP,YAAA;EAVA,SAAA,IAAA,EAAA,OAAkB;EAUlB,SAAA,aAAY,EAAA,MAAA;EASZ,SAAA,MAAA,EAAA,MAAe;AAShC;;;;AAA+E,UAT9D,eAAA,CAS8D;EASnE,SAAA,IAAA,EAAY,UAAA;EAWP,SAAA,KAAA,EAAA,OAAA;EAAsC,SAAA,OAAA,EAAA,MAAA;;;;;AAQH,KA5BxC,aAAA,GAAgB,kBA4BwB,GA5BH,YA4BG,GA5BY,eA4BZ;AAUpD;;;AAK8B,KAlClB,YAAA,GAkCkB;EAArB,SAAA,OAAA,EAAA,IAAA;EAWa,SAAA,OAAA,EAAA,MAAA;CAA0B,GAAA;EAM/B,SAAA,OAAA,EAAA,KAAA;EAAe,SAAA,MAAA,EAAA,MAAA;AAgBhC,CAAA;;;;AAEkB,UA1DD,qBA0DC,CAAA,gBA1DqC,YA0DrC,CAAA,CAAA;EAAf;EAAc,SAAA,MAAA,EAxDE,OAwDF;;0BAtDS,WAAqB;;;;kCAIb;;;;;;;;;UAUjB,+BAA+B;;mBAE7B;;SAGV,WAAqB;;;;;;;;;sBAWR,cAA0B;;;;;iBAM/B;;;;;;;;;;cAgBJ,uBAAwB,uBAC1B,sBAAsB,aAC9B,eAAe"}
|