@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,301 @@
|
|
|
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
|
+
import { ValidationError, applyDefaults, runValidators } from "./shared.mjs";
|
|
4
|
+
import { make } from "../OperationDefinition.mjs";
|
|
5
|
+
import { isPrefix, pathsEqual, pathsOverlap } from "../OperationPath.mjs";
|
|
6
|
+
import { fromDefinition } from "../Operation.mjs";
|
|
7
|
+
import { base62CharSet, generateKeyBetween } from "../FractionalIndex.mjs";
|
|
8
|
+
import "../Primitive.mjs";
|
|
9
|
+
import { Effect, Schema } from "effect";
|
|
10
|
+
|
|
11
|
+
//#region src/primitives/Array.ts
|
|
12
|
+
/**
|
|
13
|
+
* Sort array entries by their fractional position
|
|
14
|
+
*/
|
|
15
|
+
const sortByPos = (entries) => [...entries].sort((a, b) => a.pos < b.pos ? -1 : a.pos > b.pos ? 1 : 0);
|
|
16
|
+
/**
|
|
17
|
+
* Generate a fractional position between two positions
|
|
18
|
+
*/
|
|
19
|
+
const generatePosBetween = (left, right) => {
|
|
20
|
+
const charSet = base62CharSet();
|
|
21
|
+
return Effect.runSync(generateKeyBetween(left, right, charSet));
|
|
22
|
+
};
|
|
23
|
+
var ArrayPrimitive = class ArrayPrimitive {
|
|
24
|
+
constructor(schema) {
|
|
25
|
+
_defineProperty(this, "_tag", "ArrayPrimitive");
|
|
26
|
+
_defineProperty(this, "_State", void 0);
|
|
27
|
+
_defineProperty(this, "_Proxy", void 0);
|
|
28
|
+
_defineProperty(this, "_TRequired", void 0);
|
|
29
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
30
|
+
_defineProperty(this, "TSetInput", void 0);
|
|
31
|
+
_defineProperty(this, "TUpdateInput", void 0);
|
|
32
|
+
_defineProperty(this, "_schema", void 0);
|
|
33
|
+
_defineProperty(this, "_opDefinitions", {
|
|
34
|
+
set: make({
|
|
35
|
+
kind: "array.set",
|
|
36
|
+
payload: Schema.Unknown,
|
|
37
|
+
target: Schema.Unknown,
|
|
38
|
+
apply: (payload) => payload
|
|
39
|
+
}),
|
|
40
|
+
insert: make({
|
|
41
|
+
kind: "array.insert",
|
|
42
|
+
payload: Schema.Unknown,
|
|
43
|
+
target: Schema.Unknown,
|
|
44
|
+
apply: (payload) => payload
|
|
45
|
+
}),
|
|
46
|
+
remove: make({
|
|
47
|
+
kind: "array.remove",
|
|
48
|
+
payload: Schema.Unknown,
|
|
49
|
+
target: Schema.Unknown,
|
|
50
|
+
apply: (payload) => payload
|
|
51
|
+
}),
|
|
52
|
+
move: make({
|
|
53
|
+
kind: "array.move",
|
|
54
|
+
payload: Schema.Unknown,
|
|
55
|
+
target: Schema.Unknown,
|
|
56
|
+
apply: (payload) => payload
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
_defineProperty(this, "_internal", {
|
|
60
|
+
createProxy: (env, operationPath) => {
|
|
61
|
+
const elementPrimitive = this._schema.element;
|
|
62
|
+
const getCurrentState = () => {
|
|
63
|
+
const state = env.getState(operationPath);
|
|
64
|
+
if (!state || !globalThis.Array.isArray(state)) return [];
|
|
65
|
+
return sortByPos(state);
|
|
66
|
+
};
|
|
67
|
+
const applyElementDefaults = (value) => {
|
|
68
|
+
return applyDefaults(elementPrimitive, value);
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
get: () => {
|
|
72
|
+
return getCurrentState();
|
|
73
|
+
},
|
|
74
|
+
set: (values) => {
|
|
75
|
+
const entries = [];
|
|
76
|
+
let prevPos = null;
|
|
77
|
+
for (const value of values) {
|
|
78
|
+
const id = env.generateId();
|
|
79
|
+
const pos = generatePosBetween(prevPos, null);
|
|
80
|
+
const mergedValue = applyElementDefaults(value);
|
|
81
|
+
entries.push({
|
|
82
|
+
id,
|
|
83
|
+
pos,
|
|
84
|
+
value: mergedValue
|
|
85
|
+
});
|
|
86
|
+
prevPos = pos;
|
|
87
|
+
}
|
|
88
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, entries));
|
|
89
|
+
},
|
|
90
|
+
push: (value) => {
|
|
91
|
+
const sorted = getCurrentState();
|
|
92
|
+
const lastPos = sorted.length > 0 ? sorted[sorted.length - 1].pos : null;
|
|
93
|
+
const id = env.generateId();
|
|
94
|
+
const pos = generatePosBetween(lastPos, null);
|
|
95
|
+
const mergedValue = applyElementDefaults(value);
|
|
96
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
97
|
+
id,
|
|
98
|
+
pos,
|
|
99
|
+
value: mergedValue
|
|
100
|
+
}));
|
|
101
|
+
},
|
|
102
|
+
insertAt: (index, value) => {
|
|
103
|
+
const sorted = getCurrentState();
|
|
104
|
+
const leftPos = index > 0 && sorted[index - 1] ? sorted[index - 1].pos : null;
|
|
105
|
+
const rightPos = index < sorted.length && sorted[index] ? sorted[index].pos : null;
|
|
106
|
+
const id = env.generateId();
|
|
107
|
+
const pos = generatePosBetween(leftPos, rightPos);
|
|
108
|
+
const mergedValue = applyElementDefaults(value);
|
|
109
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.insert, {
|
|
110
|
+
id,
|
|
111
|
+
pos,
|
|
112
|
+
value: mergedValue
|
|
113
|
+
}));
|
|
114
|
+
},
|
|
115
|
+
remove: (id) => {
|
|
116
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.remove, { id }));
|
|
117
|
+
},
|
|
118
|
+
move: (id, toIndex) => {
|
|
119
|
+
const without = getCurrentState().filter((e) => e.id !== id);
|
|
120
|
+
const clampedIndex = Math.max(0, Math.min(toIndex, without.length));
|
|
121
|
+
const pos = generatePosBetween(clampedIndex > 0 && without[clampedIndex - 1] ? without[clampedIndex - 1].pos : null, clampedIndex < without.length && without[clampedIndex] ? without[clampedIndex].pos : null);
|
|
122
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.move, {
|
|
123
|
+
id,
|
|
124
|
+
pos
|
|
125
|
+
}));
|
|
126
|
+
},
|
|
127
|
+
at: (id) => {
|
|
128
|
+
const elementPath = operationPath.append(id);
|
|
129
|
+
return elementPrimitive._internal.createProxy(env, elementPath);
|
|
130
|
+
},
|
|
131
|
+
find: (predicate) => {
|
|
132
|
+
const found = getCurrentState().find((entry) => predicate(entry.value, entry.id));
|
|
133
|
+
if (!found) return void 0;
|
|
134
|
+
const elementPath = operationPath.append(found.id);
|
|
135
|
+
return elementPrimitive._internal.createProxy(env, elementPath);
|
|
136
|
+
},
|
|
137
|
+
toSnapshot: () => {
|
|
138
|
+
return getCurrentState().map((entry) => {
|
|
139
|
+
const elementPath = operationPath.append(entry.id);
|
|
140
|
+
const elementProxy = elementPrimitive._internal.createProxy(env, elementPath);
|
|
141
|
+
return {
|
|
142
|
+
id: entry.id,
|
|
143
|
+
value: elementProxy.toSnapshot()
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
},
|
|
149
|
+
applyOperation: (state, operation) => {
|
|
150
|
+
const path = operation.path;
|
|
151
|
+
const tokens = path.toTokens().filter((t) => t !== "");
|
|
152
|
+
const currentState = state !== null && state !== void 0 ? state : [];
|
|
153
|
+
let newState;
|
|
154
|
+
if (tokens.length === 0) switch (operation.kind) {
|
|
155
|
+
case "array.set": {
|
|
156
|
+
const payload = operation.payload;
|
|
157
|
+
if (!globalThis.Array.isArray(payload)) throw new ValidationError(`ArrayPrimitive.set requires an array payload`);
|
|
158
|
+
newState = payload;
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case "array.insert": {
|
|
162
|
+
const { id, pos, value } = operation.payload;
|
|
163
|
+
newState = [...currentState, {
|
|
164
|
+
id,
|
|
165
|
+
pos,
|
|
166
|
+
value
|
|
167
|
+
}];
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "array.remove": {
|
|
171
|
+
const { id } = operation.payload;
|
|
172
|
+
newState = currentState.filter((entry) => entry.id !== id);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case "array.move": {
|
|
176
|
+
const { id, pos } = operation.payload;
|
|
177
|
+
newState = currentState.map((entry) => entry.id === id ? _objectSpread2(_objectSpread2({}, entry), {}, { pos }) : entry);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
default: throw new ValidationError(`ArrayPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
const elementId = tokens[0];
|
|
184
|
+
const entryIndex = currentState.findIndex((entry) => entry.id === elementId);
|
|
185
|
+
if (entryIndex === -1) throw new ValidationError(`Array element not found with ID: ${elementId}`);
|
|
186
|
+
const elementPrimitive = this._schema.element;
|
|
187
|
+
const remainingPath = path.shift();
|
|
188
|
+
const elementOperation = _objectSpread2(_objectSpread2({}, operation), {}, { path: remainingPath });
|
|
189
|
+
const currentEntry = currentState[entryIndex];
|
|
190
|
+
const newValue = elementPrimitive._internal.applyOperation(currentEntry.value, elementOperation);
|
|
191
|
+
const mutableState = [...currentState];
|
|
192
|
+
mutableState[entryIndex] = _objectSpread2(_objectSpread2({}, currentEntry), {}, { value: newValue });
|
|
193
|
+
newState = mutableState;
|
|
194
|
+
}
|
|
195
|
+
runValidators(newState, this._schema.validators);
|
|
196
|
+
return newState;
|
|
197
|
+
},
|
|
198
|
+
getInitialState: () => {
|
|
199
|
+
return this._schema.defaultValue;
|
|
200
|
+
},
|
|
201
|
+
transformOperation: (clientOp, serverOp) => {
|
|
202
|
+
const clientPath = clientOp.path;
|
|
203
|
+
const serverPath = serverOp.path;
|
|
204
|
+
if (!pathsOverlap(clientPath, serverPath)) return {
|
|
205
|
+
type: "transformed",
|
|
206
|
+
operation: clientOp
|
|
207
|
+
};
|
|
208
|
+
if (serverOp.kind === "array.remove") {
|
|
209
|
+
const removedId = serverOp.payload.id;
|
|
210
|
+
const clientTokens$1 = clientPath.toTokens().filter((t) => t !== "");
|
|
211
|
+
const serverTokens$1 = serverPath.toTokens().filter((t) => t !== "");
|
|
212
|
+
if (clientTokens$1.length > serverTokens$1.length) {
|
|
213
|
+
if (clientTokens$1[serverTokens$1.length] === removedId) return { type: "noop" };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (serverOp.kind === "array.insert" && clientOp.kind === "array.insert") return {
|
|
217
|
+
type: "transformed",
|
|
218
|
+
operation: clientOp
|
|
219
|
+
};
|
|
220
|
+
if (serverOp.kind === "array.move" && clientOp.kind === "array.move") {
|
|
221
|
+
if (serverOp.payload.id === clientOp.payload.id) return {
|
|
222
|
+
type: "transformed",
|
|
223
|
+
operation: clientOp
|
|
224
|
+
};
|
|
225
|
+
return {
|
|
226
|
+
type: "transformed",
|
|
227
|
+
operation: clientOp
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
if (pathsEqual(clientPath, serverPath)) return {
|
|
231
|
+
type: "transformed",
|
|
232
|
+
operation: clientOp
|
|
233
|
+
};
|
|
234
|
+
if (serverOp.kind === "array.set" && isPrefix(serverPath, clientPath)) return {
|
|
235
|
+
type: "transformed",
|
|
236
|
+
operation: clientOp
|
|
237
|
+
};
|
|
238
|
+
const clientTokens = clientPath.toTokens().filter((t) => t !== "");
|
|
239
|
+
const serverTokens = serverPath.toTokens().filter((t) => t !== "");
|
|
240
|
+
if (clientTokens.length > 0 && serverTokens.length > 0) {
|
|
241
|
+
if (clientTokens[0] !== serverTokens[0]) return {
|
|
242
|
+
type: "transformed",
|
|
243
|
+
operation: clientOp
|
|
244
|
+
};
|
|
245
|
+
const elementPrimitive = this._schema.element;
|
|
246
|
+
const clientOpForElement = _objectSpread2(_objectSpread2({}, clientOp), {}, { path: clientOp.path.shift() });
|
|
247
|
+
const serverOpForElement = _objectSpread2(_objectSpread2({}, serverOp), {}, { path: serverOp.path.shift() });
|
|
248
|
+
const result = elementPrimitive._internal.transformOperation(clientOpForElement, serverOpForElement);
|
|
249
|
+
if (result.type === "transformed") return {
|
|
250
|
+
type: "transformed",
|
|
251
|
+
operation: _objectSpread2(_objectSpread2({}, result.operation), {}, { path: clientOp.path })
|
|
252
|
+
};
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
type: "transformed",
|
|
257
|
+
operation: clientOp
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
this._schema = schema;
|
|
262
|
+
}
|
|
263
|
+
/** Mark this array as required */
|
|
264
|
+
required() {
|
|
265
|
+
return new ArrayPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { required: true }));
|
|
266
|
+
}
|
|
267
|
+
/** Set a default value for this array */
|
|
268
|
+
default(defaultValue) {
|
|
269
|
+
return new ArrayPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue }));
|
|
270
|
+
}
|
|
271
|
+
/** Get the element primitive */
|
|
272
|
+
get element() {
|
|
273
|
+
return this._schema.element;
|
|
274
|
+
}
|
|
275
|
+
/** Add a custom validation rule */
|
|
276
|
+
refine(fn, message) {
|
|
277
|
+
return new ArrayPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { validators: [...this._schema.validators, {
|
|
278
|
+
validate: fn,
|
|
279
|
+
message
|
|
280
|
+
}] }));
|
|
281
|
+
}
|
|
282
|
+
/** Minimum array length */
|
|
283
|
+
minLength(length) {
|
|
284
|
+
return this.refine((v) => v.length >= length, `Array must have at least ${length} elements`);
|
|
285
|
+
}
|
|
286
|
+
/** Maximum array length */
|
|
287
|
+
maxLength(length) {
|
|
288
|
+
return this.refine((v) => v.length <= length, `Array must have at most ${length} elements`);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
/** Creates a new ArrayPrimitive with the given element type */
|
|
292
|
+
const Array = (element) => new ArrayPrimitive({
|
|
293
|
+
required: false,
|
|
294
|
+
defaultValue: void 0,
|
|
295
|
+
element,
|
|
296
|
+
validators: []
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
//#endregion
|
|
300
|
+
export { Array, ArrayPrimitive };
|
|
301
|
+
//# sourceMappingURL=Array.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Array.mjs","names":["FractionalIndex.base62CharSet","FractionalIndex.generateKeyBetween","OperationDefinition.make","entries: ArrayEntry<InferState<TElement>>[]","prevPos: string | null","Operation.fromDefinition","newState: ArrayState<TElement>","OperationPath.pathsOverlap","clientTokens","serverTokens","OperationPath.pathsEqual","OperationPath.isPrefix"],"sources":["../../src/primitives/Array.ts"],"sourcesContent":["import { Effect, Schema } from \"effect\";\nimport * as OperationDefinition from \"../OperationDefinition\";\nimport * as Operation from \"../Operation\";\nimport * as OperationPath from \"../OperationPath\";\nimport * as ProxyEnvironment from \"../ProxyEnvironment\";\nimport * as Transform from \"../Transform\";\nimport * as FractionalIndex from \"../FractionalIndex\";\nimport type { Primitive, PrimitiveInternal, MaybeUndefined, AnyPrimitive, Validator, InferState, InferProxy, InferSnapshot, InferSetInput } from \"../Primitive\";\nimport { ValidationError } from \"../Primitive\";\nimport { runValidators, applyDefaults } from \"./shared\";\nimport { StructPrimitive, StructSetInput } from \"./Struct\";\n\n\n/**\n * Entry in an ordered array with ID and fractional position\n */\nexport interface ArrayEntry<T> {\n readonly id: string; // Unique element identifier (UUID)\n readonly pos: string; // Fractional index for ordering\n readonly value: T; // The element value\n}\n\n/**\n * Sort array entries by their fractional position\n */\nconst sortByPos = <T,>(entries: readonly ArrayEntry<T>[]): ArrayEntry<T>[] =>\n [...entries].sort((a, b) => a.pos < b.pos ? -1 : a.pos > b.pos ? 1 : 0);\n\n/**\n * Generate a fractional position between two positions\n */\nconst generatePosBetween = (left: string | null, right: string | null): string => {\n const charSet = FractionalIndex.base62CharSet();\n return Effect.runSync(FractionalIndex.generateKeyBetween(left, right, charSet));\n};\n\n/**\n * Entry in an array snapshot with ID and value snapshot\n */\nexport interface ArrayEntrySnapshot<TElement extends AnyPrimitive> {\n readonly id: string;\n readonly value: InferSnapshot<TElement>;\n}\n\n/**\n * Snapshot type for arrays - always an array (never undefined)\n */\nexport type ArraySnapshot<TElement extends AnyPrimitive> = readonly ArrayEntrySnapshot<TElement>[];\n\n/**\n * Compute the input type for array element values.\n * Uses StructSetInput directly for struct elements so that:\n * - Fields that are required AND have no default must be provided\n * - Fields that are optional OR have defaults can be omitted\n * \n * For non-struct elements, falls back to InferSetInput.\n */\nexport type ArrayElementSetInput<TElement extends AnyPrimitive> = \n TElement extends StructPrimitive<infer TFields, any, any>\n ? StructSetInput<TFields>\n : InferSetInput<TElement>;\n\nexport interface ArrayProxy<TElement extends AnyPrimitive> {\n /** Gets the current array entries (sorted by position) */\n get(): ArrayState<TElement>;\n /** Replaces the entire array with new values (generates new IDs and positions, applies defaults) */\n set(values: readonly ArrayElementSetInput<TElement>[]): void;\n /** Appends a value to the end of the array (applies defaults for struct elements) */\n push(value: ArrayElementSetInput<TElement>): void;\n /** Inserts a value at the specified visual index (applies defaults for struct elements) */\n insertAt(index: number, value: ArrayElementSetInput<TElement>): void;\n /** Removes the element with the specified ID */\n remove(id: string): void;\n /** Moves an element to a new visual index */\n move(id: string, toIndex: number): void;\n /** Returns a proxy for the element with the specified ID */\n at(id: string): InferProxy<TElement>;\n /** Finds an element by predicate and returns its proxy */\n find(predicate: (value: InferState<TElement>, id: string) => boolean): InferProxy<TElement> | undefined;\n /** Returns a readonly snapshot of the array for rendering (always returns an array, never undefined) */\n toSnapshot(): ArraySnapshot<TElement>;\n}\n\n/** The state type for arrays - an array of entries */\nexport type ArrayState<TElement extends AnyPrimitive> = readonly ArrayEntry<InferState<TElement>>[];\n\ninterface ArrayPrimitiveSchema<TElement extends AnyPrimitive> {\n readonly required: boolean;\n readonly defaultValue: ArrayState<TElement> | undefined;\n readonly element: TElement;\n readonly validators: readonly Validator<ArrayState<TElement>>[];\n}\n\n/** Input type for array set() - an array of element set inputs */\nexport type ArraySetInput<TElement extends AnyPrimitive> = readonly ArrayElementSetInput<TElement>[];\n\n/** Input type for array update() - same as set() for arrays */\nexport type ArrayUpdateInput<TElement extends AnyPrimitive> = readonly ArrayElementSetInput<TElement>[];\n\nexport class ArrayPrimitive<TElement extends AnyPrimitive, TRequired extends boolean = false, THasDefault extends boolean = false>\n implements Primitive<ArrayState<TElement>, ArrayProxy<TElement>, TRequired, THasDefault, ArraySetInput<TElement>, ArrayUpdateInput<TElement>>\n{\n readonly _tag = \"ArrayPrimitive\" as const;\n readonly _State!: ArrayState<TElement>;\n readonly _Proxy!: ArrayProxy<TElement>;\n readonly _TRequired!: TRequired;\n readonly _THasDefault!: THasDefault;\n readonly TSetInput!: ArraySetInput<TElement>;\n readonly TUpdateInput!: ArrayUpdateInput<TElement>;\n\n private readonly _schema: ArrayPrimitiveSchema<TElement>;\n\n private readonly _opDefinitions = {\n set: OperationDefinition.make({\n kind: \"array.set\" as const,\n payload: Schema.Unknown,\n target: Schema.Unknown,\n apply: (payload) => payload,\n }),\n insert: OperationDefinition.make({\n kind: \"array.insert\" as const,\n payload: Schema.Unknown,\n target: Schema.Unknown,\n apply: (payload) => payload,\n }),\n remove: OperationDefinition.make({\n kind: \"array.remove\" as const,\n payload: Schema.Unknown,\n target: Schema.Unknown,\n apply: (payload) => payload,\n }),\n move: OperationDefinition.make({\n kind: \"array.move\" as const,\n payload: Schema.Unknown,\n target: Schema.Unknown,\n apply: (payload) => payload,\n }),\n };\n\n constructor(schema: ArrayPrimitiveSchema<TElement>) {\n this._schema = schema;\n }\n\n /** Mark this array as required */\n required(): ArrayPrimitive<TElement, true, THasDefault> {\n return new ArrayPrimitive({\n ...this._schema,\n required: true,\n });\n }\n\n /** Set a default value for this array */\n default(defaultValue: ArrayState<TElement>): ArrayPrimitive<TElement, TRequired, true> {\n return new ArrayPrimitive({\n ...this._schema,\n defaultValue,\n });\n }\n\n /** Get the element primitive */\n get element(): TElement {\n return this._schema.element;\n }\n\n /** Add a custom validation rule */\n refine(fn: (value: ArrayState<TElement>) => boolean, message: string): ArrayPrimitive<TElement, TRequired, THasDefault> {\n return new ArrayPrimitive({\n ...this._schema,\n validators: [...this._schema.validators, { validate: fn, message }],\n });\n }\n\n /** Minimum array length */\n minLength(length: number): ArrayPrimitive<TElement, TRequired, THasDefault> {\n return this.refine(\n (v) => v.length >= length,\n `Array must have at least ${length} elements`\n );\n }\n\n /** Maximum array length */\n maxLength(length: number): ArrayPrimitive<TElement, TRequired, THasDefault> {\n return this.refine(\n (v) => v.length <= length,\n `Array must have at most ${length} elements`\n );\n }\n\n readonly _internal: PrimitiveInternal<ArrayState<TElement>, ArrayProxy<TElement>> = {\n createProxy: (env: ProxyEnvironment.ProxyEnvironment, operationPath: OperationPath.OperationPath): ArrayProxy<TElement> => {\n const elementPrimitive = this._schema.element;\n\n // Helper to get current state (sorted)\n const getCurrentState = (): ArrayEntry<InferState<TElement>>[] => {\n const state = env.getState(operationPath) as ArrayState<TElement> | undefined;\n if (!state || !globalThis.Array.isArray(state)) return [];\n return sortByPos(state);\n };\n\n // Helper to apply defaults for element values\n const applyElementDefaults = (value: ArrayElementSetInput<TElement>): InferState<TElement> => {\n return applyDefaults(elementPrimitive, value as Partial<InferState<TElement>>) as InferState<TElement>;\n };\n\n return {\n get: (): ArrayState<TElement> => {\n return getCurrentState();\n },\n\n set: (values: readonly ArrayElementSetInput<TElement>[]) => {\n // Generate entries with new IDs and sequential positions\n const entries: ArrayEntry<InferState<TElement>>[] = [];\n let prevPos: string | null = null;\n \n for (const value of values) {\n const id = env.generateId();\n const pos = generatePosBetween(prevPos, null);\n // Apply defaults to element value\n const mergedValue = applyElementDefaults(value);\n entries.push({ id, pos, value: mergedValue });\n prevPos = pos;\n }\n \n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.set, entries)\n );\n },\n\n push: (value: ArrayElementSetInput<TElement>) => {\n const sorted = getCurrentState();\n const lastPos = sorted.length > 0 ? sorted[sorted.length - 1]!.pos : null;\n const id = env.generateId();\n const pos = generatePosBetween(lastPos, null);\n // Apply defaults to element value\n const mergedValue = applyElementDefaults(value);\n \n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.insert, { id, pos, value: mergedValue })\n );\n },\n\n insertAt: (index: number, value: ArrayElementSetInput<TElement>) => {\n const sorted = getCurrentState();\n const leftPos = index > 0 && sorted[index - 1] ? sorted[index - 1]!.pos : null;\n const rightPos = index < sorted.length && sorted[index] ? sorted[index]!.pos : null;\n \n const id = env.generateId();\n const pos = generatePosBetween(leftPos, rightPos);\n // Apply defaults to element value\n const mergedValue = applyElementDefaults(value);\n \n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.insert, { id, pos, value: mergedValue })\n );\n },\n\n remove: (id: string) => {\n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.remove, { id })\n );\n },\n\n move: (id: string, toIndex: number) => {\n const sorted = getCurrentState();\n // Filter out the element being moved\n const without = sorted.filter(e => e.id !== id);\n \n const clampedIndex = Math.max(0, Math.min(toIndex, without.length));\n const leftPos = clampedIndex > 0 && without[clampedIndex - 1] ? without[clampedIndex - 1]!.pos : null;\n const rightPos = clampedIndex < without.length && without[clampedIndex] ? without[clampedIndex]!.pos : null;\n \n const pos = generatePosBetween(leftPos, rightPos);\n \n env.addOperation(\n Operation.fromDefinition(operationPath, this._opDefinitions.move, { id, pos })\n );\n },\n\n at: (id: string): InferProxy<TElement> => {\n // Use ID in path for element access\n const elementPath = operationPath.append(id);\n return elementPrimitive._internal.createProxy(env, elementPath) as InferProxy<TElement>;\n },\n\n find: (predicate: (value: InferState<TElement>, id: string) => boolean): InferProxy<TElement> | undefined => {\n const sorted = getCurrentState();\n const found = sorted.find(entry => predicate(entry.value, entry.id));\n if (!found) return undefined;\n \n const elementPath = operationPath.append(found.id);\n return elementPrimitive._internal.createProxy(env, elementPath) as InferProxy<TElement>;\n },\n\n toSnapshot: (): ArraySnapshot<TElement> => {\n const sorted = getCurrentState();\n return sorted.map(entry => {\n const elementPath = operationPath.append(entry.id);\n const elementProxy = elementPrimitive._internal.createProxy(env, elementPath);\n return {\n id: entry.id,\n value: (elementProxy as { toSnapshot(): InferSnapshot<TElement> }).toSnapshot(),\n };\n });\n },\n };\n },\n\n applyOperation: (\n state: ArrayState<TElement> | undefined,\n operation: Operation.Operation<any, any, any>\n ): ArrayState<TElement> => {\n const path = operation.path;\n const tokens = path.toTokens().filter((t: string) => t !== \"\");\n const currentState = state ?? [];\n\n let newState: ArrayState<TElement>;\n\n // If path is empty, this is an array-level operation\n if (tokens.length === 0) {\n switch (operation.kind) {\n case \"array.set\": {\n const payload = operation.payload;\n if (!globalThis.Array.isArray(payload)) {\n throw new ValidationError(`ArrayPrimitive.set requires an array payload`);\n }\n newState = payload as ArrayState<TElement>;\n break;\n }\n case \"array.insert\": {\n const { id, pos, value } = operation.payload as { id: string; pos: string; value: InferState<TElement> };\n newState = [...currentState, { id, pos, value }];\n break;\n }\n case \"array.remove\": {\n const { id } = operation.payload as { id: string };\n newState = currentState.filter(entry => entry.id !== id);\n break;\n }\n case \"array.move\": {\n const { id, pos } = operation.payload as { id: string; pos: string };\n newState = currentState.map(entry => \n entry.id === id ? { ...entry, pos } : entry\n );\n break;\n }\n default:\n throw new ValidationError(`ArrayPrimitive cannot apply operation of kind: ${operation.kind}`);\n }\n } else {\n // Otherwise, delegate to the element with the specified ID\n const elementId = tokens[0]!;\n const entryIndex = currentState.findIndex(entry => entry.id === elementId);\n \n if (entryIndex === -1) {\n throw new ValidationError(`Array element not found with ID: ${elementId}`);\n }\n\n const elementPrimitive = this._schema.element;\n const remainingPath = path.shift();\n const elementOperation = {\n ...operation,\n path: remainingPath,\n };\n\n const currentEntry = currentState[entryIndex]!;\n const newValue = elementPrimitive._internal.applyOperation(currentEntry.value, elementOperation);\n\n const mutableState = [...currentState];\n mutableState[entryIndex] = { ...currentEntry, value: newValue };\n newState = mutableState;\n }\n\n // Run validators on the new state\n runValidators(newState, this._schema.validators);\n\n return newState;\n },\n\n getInitialState: (): ArrayState<TElement> | undefined => {\n return this._schema.defaultValue;\n },\n\n transformOperation: (\n clientOp: Operation.Operation<any, any, any>,\n serverOp: Operation.Operation<any, any, any>\n ): Transform.TransformResult => {\n const clientPath = clientOp.path;\n const serverPath = serverOp.path;\n\n // If paths don't overlap at all, no transformation needed\n if (!OperationPath.pathsOverlap(clientPath, serverPath)) {\n return { type: \"transformed\", operation: clientOp };\n }\n\n // Handle array.remove from server - check if client is operating on removed element\n if (serverOp.kind === \"array.remove\") {\n const removedId = (serverOp.payload as { id: string }).id;\n const clientTokens = clientPath.toTokens().filter((t: string) => t !== \"\");\n const serverTokens = serverPath.toTokens().filter((t: string) => t !== \"\");\n\n // Check if client is operating on the removed element or its children\n if (clientTokens.length > serverTokens.length) {\n const elementId = clientTokens[serverTokens.length];\n if (elementId === removedId) {\n // Client operation targets a removed element - becomes noop\n return { type: \"noop\" };\n }\n }\n }\n\n // Both inserting into same array - no conflict (fractional indexing handles ordering)\n if (serverOp.kind === \"array.insert\" && clientOp.kind === \"array.insert\") {\n return { type: \"transformed\", operation: clientOp };\n }\n\n // Both moving elements in same array\n if (serverOp.kind === \"array.move\" && clientOp.kind === \"array.move\") {\n const serverMoveId = (serverOp.payload as { id: string }).id;\n const clientMoveId = (clientOp.payload as { id: string }).id;\n\n if (serverMoveId === clientMoveId) {\n // Client's move supersedes server's move (last-write-wins for position)\n return { type: \"transformed\", operation: clientOp };\n }\n // Different elements - no conflict\n return { type: \"transformed\", operation: clientOp };\n }\n\n // For operations on same exact path: client wins (last-write-wins)\n if (OperationPath.pathsEqual(clientPath, serverPath)) {\n return { type: \"transformed\", operation: clientOp };\n }\n\n // If server set entire array and client is operating on an element\n if (serverOp.kind === \"array.set\" && OperationPath.isPrefix(serverPath, clientPath)) {\n // Client's element operation may be invalid after array replacement\n // However, for optimistic updates, we let the client op proceed\n // and the server will validate/reject if needed\n return { type: \"transformed\", operation: clientOp };\n }\n\n // Delegate to element primitive for nested operations\n const clientTokens = clientPath.toTokens().filter((t: string) => t !== \"\");\n const serverTokens = serverPath.toTokens().filter((t: string) => t !== \"\");\n\n // Both operations target children of this array\n if (clientTokens.length > 0 && serverTokens.length > 0) {\n const clientElementId = clientTokens[0];\n const serverElementId = serverTokens[0];\n\n // If operating on different elements, no conflict\n if (clientElementId !== serverElementId) {\n return { type: \"transformed\", operation: clientOp };\n }\n\n // Same element - delegate to element primitive\n const elementPrimitive = this._schema.element;\n const clientOpForElement = {\n ...clientOp,\n path: clientOp.path.shift(),\n };\n const serverOpForElement = {\n ...serverOp,\n path: serverOp.path.shift(),\n };\n\n const result = elementPrimitive._internal.transformOperation(clientOpForElement, serverOpForElement);\n\n if (result.type === \"transformed\") {\n // Restore the original path prefix\n return {\n type: \"transformed\",\n operation: {\n ...result.operation,\n path: clientOp.path,\n },\n };\n }\n\n return result;\n }\n\n // Default: no transformation needed\n return { type: \"transformed\", operation: clientOp };\n },\n };\n}\n\n/** Creates a new ArrayPrimitive with the given element type */\nexport const Array = <TElement extends AnyPrimitive>(element: TElement): ArrayPrimitive<TElement, false, false> =>\n new ArrayPrimitive({ required: false, defaultValue: undefined, element, validators: [] });\n\n"],"mappings":";;;;;;;;;;;;;;AAyBA,MAAM,aAAiB,YACrB,CAAC,GAAG,QAAQ,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,EAAE;;;;AAKzE,MAAM,sBAAsB,MAAqB,UAAiC;CAChF,MAAM,UAAUA,eAA+B;AAC/C,QAAO,OAAO,QAAQC,mBAAmC,MAAM,OAAO,QAAQ,CAAC;;AAkEjF,IAAa,iBAAb,MAAa,eAEb;CAsCE,YAAY,QAAwC;wBArC3C,QAAO;wBACP;wBACA;wBACA;wBACA;wBACA;wBACA;wBAEQ;wBAEA,kBAAiB;GAChC,KAAKC,KAAyB;IAC5B,MAAM;IACN,SAAS,OAAO;IAChB,QAAQ,OAAO;IACf,QAAQ,YAAY;IACrB,CAAC;GACF,QAAQA,KAAyB;IAC/B,MAAM;IACN,SAAS,OAAO;IAChB,QAAQ,OAAO;IACf,QAAQ,YAAY;IACrB,CAAC;GACF,QAAQA,KAAyB;IAC/B,MAAM;IACN,SAAS,OAAO;IAChB,QAAQ,OAAO;IACf,QAAQ,YAAY;IACrB,CAAC;GACF,MAAMA,KAAyB;IAC7B,MAAM;IACN,SAAS,OAAO;IAChB,QAAQ,OAAO;IACf,QAAQ,YAAY;IACrB,CAAC;GACH;wBAmDQ,aAA2E;GAClF,cAAc,KAAwC,kBAAqE;IACzH,MAAM,mBAAmB,KAAK,QAAQ;IAGtC,MAAM,wBAA4D;KAChE,MAAM,QAAQ,IAAI,SAAS,cAAc;AACzC,SAAI,CAAC,SAAS,CAAC,WAAW,MAAM,QAAQ,MAAM,CAAE,QA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S;MAElB;AAGH,YAAO;;AAIT,WAAO;KAAE,MAAM;KAAe,WAAW;KAAU;;GAEtD;AAzVC,OAAK,UAAU;;;CAIjB,WAAwD;AACtD,SAAO,IAAI,iDACN,KAAK,gBACR,UAAU,QACV;;;CAIJ,QAAQ,cAA+E;AACrF,SAAO,IAAI,iDACN,KAAK,gBACR,gBACA;;;CAIJ,IAAI,UAAoB;AACtB,SAAO,KAAK,QAAQ;;;CAItB,OAAO,IAA8C,SAAmE;AACtH,SAAO,IAAI,iDACN,KAAK,gBACR,YAAY,CAAC,GAAG,KAAK,QAAQ,YAAY;GAAE,UAAU;GAAI;GAAS,CAAC,IACnE;;;CAIJ,UAAU,QAAkE;AAC1E,SAAO,KAAK,QACT,MAAM,EAAE,UAAU,QACnB,4BAA4B,OAAO,WACpC;;;CAIH,UAAU,QAAkE;AAC1E,SAAO,KAAK,QACT,MAAM,EAAE,UAAU,QACnB,2BAA2B,OAAO,WACnC;;;;AAgTL,MAAa,SAAwC,YACnD,IAAI,eAAe;CAAE,UAAU;CAAO,cAAc;CAAW;CAAS,YAAY,EAAE;CAAE,CAAC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_defineProperty = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/defineProperty.cjs');
|
|
3
|
+
const require_objectSpread2 = require('../_virtual/_@oxc-project_runtime@0.103.0/helpers/objectSpread2.cjs');
|
|
4
|
+
const require_shared = require('./shared.cjs');
|
|
5
|
+
const require_OperationDefinition = require('../OperationDefinition.cjs');
|
|
6
|
+
const require_OperationPath = require('../OperationPath.cjs');
|
|
7
|
+
const require_Operation = require('../Operation.cjs');
|
|
8
|
+
let effect = require("effect");
|
|
9
|
+
|
|
10
|
+
//#region src/primitives/Boolean.ts
|
|
11
|
+
var BooleanPrimitive = class BooleanPrimitive {
|
|
12
|
+
constructor(schema) {
|
|
13
|
+
require_defineProperty._defineProperty(this, "_tag", "BooleanPrimitive");
|
|
14
|
+
require_defineProperty._defineProperty(this, "_State", void 0);
|
|
15
|
+
require_defineProperty._defineProperty(this, "_Proxy", void 0);
|
|
16
|
+
require_defineProperty._defineProperty(this, "_TRequired", void 0);
|
|
17
|
+
require_defineProperty._defineProperty(this, "_THasDefault", void 0);
|
|
18
|
+
require_defineProperty._defineProperty(this, "TUpdateInput", void 0);
|
|
19
|
+
require_defineProperty._defineProperty(this, "TSetInput", void 0);
|
|
20
|
+
require_defineProperty._defineProperty(this, "_schema", void 0);
|
|
21
|
+
require_defineProperty._defineProperty(this, "_opDefinitions", { set: require_OperationDefinition.make({
|
|
22
|
+
kind: "boolean.set",
|
|
23
|
+
payload: effect.Schema.Boolean,
|
|
24
|
+
target: effect.Schema.Boolean,
|
|
25
|
+
apply: (payload) => payload
|
|
26
|
+
}) });
|
|
27
|
+
require_defineProperty._defineProperty(this, "_internal", {
|
|
28
|
+
createProxy: (env, operationPath) => {
|
|
29
|
+
const defaultValue = this._schema.defaultValue;
|
|
30
|
+
return {
|
|
31
|
+
get: () => {
|
|
32
|
+
const state = env.getState(operationPath);
|
|
33
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
34
|
+
},
|
|
35
|
+
set: (value) => {
|
|
36
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
37
|
+
},
|
|
38
|
+
update: (value) => {
|
|
39
|
+
env.addOperation(require_Operation.fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
40
|
+
},
|
|
41
|
+
toSnapshot: () => {
|
|
42
|
+
const state = env.getState(operationPath);
|
|
43
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
applyOperation: (_state, operation) => {
|
|
48
|
+
if (operation.kind !== "boolean.set") throw new require_shared.ValidationError(`BooleanPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
49
|
+
const payload = operation.payload;
|
|
50
|
+
if (typeof payload !== "boolean") throw new require_shared.ValidationError(`BooleanPrimitive.set requires a boolean payload, got: ${typeof payload}`);
|
|
51
|
+
require_shared.runValidators(payload, this._schema.validators);
|
|
52
|
+
return payload;
|
|
53
|
+
},
|
|
54
|
+
getInitialState: () => {
|
|
55
|
+
return this._schema.defaultValue;
|
|
56
|
+
},
|
|
57
|
+
transformOperation: (clientOp, serverOp) => {
|
|
58
|
+
if (!require_OperationPath.pathsOverlap(clientOp.path, serverOp.path)) return {
|
|
59
|
+
type: "transformed",
|
|
60
|
+
operation: clientOp
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
type: "transformed",
|
|
64
|
+
operation: clientOp
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
this._schema = schema;
|
|
69
|
+
}
|
|
70
|
+
/** Mark this boolean as required */
|
|
71
|
+
required() {
|
|
72
|
+
return new BooleanPrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { required: true }));
|
|
73
|
+
}
|
|
74
|
+
/** Set a default value for this boolean */
|
|
75
|
+
default(defaultValue) {
|
|
76
|
+
return new BooleanPrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { defaultValue }));
|
|
77
|
+
}
|
|
78
|
+
/** Add a custom validation rule */
|
|
79
|
+
refine(fn, message) {
|
|
80
|
+
return new BooleanPrimitive(require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, this._schema), {}, { validators: [...this._schema.validators, {
|
|
81
|
+
validate: fn,
|
|
82
|
+
message
|
|
83
|
+
}] }));
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/** Creates a new BooleanPrimitive */
|
|
87
|
+
const Boolean = () => new BooleanPrimitive({
|
|
88
|
+
required: false,
|
|
89
|
+
defaultValue: void 0,
|
|
90
|
+
validators: []
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
//#endregion
|
|
94
|
+
exports.Boolean = Boolean;
|
|
95
|
+
exports.BooleanPrimitive = BooleanPrimitive;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { MaybeUndefined, NeedsValue, Primitive, PrimitiveInternal, Validator } from "./shared.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/primitives/Boolean.d.ts
|
|
4
|
+
type InferSetInput<TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<boolean, TRequired, THasDefault>;
|
|
5
|
+
type InferUpdateInput<TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<boolean, TRequired, THasDefault>;
|
|
6
|
+
interface BooleanProxy<TRequired extends boolean = false, THasDefault extends boolean = false> {
|
|
7
|
+
/** Gets the current boolean value */
|
|
8
|
+
get(): MaybeUndefined<boolean, TRequired, THasDefault>;
|
|
9
|
+
/** Sets the boolean value, generating a boolean.set operation */
|
|
10
|
+
set(value: InferSetInput<TRequired, THasDefault>): void;
|
|
11
|
+
/** This is the same as set. Updates the boolean value, generating a boolean.set operation */
|
|
12
|
+
update(value: InferUpdateInput<TRequired, THasDefault>): void;
|
|
13
|
+
/** Returns a readonly snapshot of the boolean value for rendering */
|
|
14
|
+
toSnapshot(): MaybeUndefined<boolean, TRequired, THasDefault>;
|
|
15
|
+
}
|
|
16
|
+
interface BooleanPrimitiveSchema {
|
|
17
|
+
readonly required: boolean;
|
|
18
|
+
readonly defaultValue: boolean | undefined;
|
|
19
|
+
readonly validators: readonly Validator<boolean>[];
|
|
20
|
+
}
|
|
21
|
+
declare class BooleanPrimitive<TRequired extends boolean = false, THasDefault extends boolean = false> implements Primitive<boolean, BooleanProxy<TRequired, THasDefault>, TRequired, THasDefault, InferSetInput<TRequired, THasDefault>, InferUpdateInput<TRequired, THasDefault>> {
|
|
22
|
+
readonly _tag: "BooleanPrimitive";
|
|
23
|
+
readonly _State: boolean;
|
|
24
|
+
readonly _Proxy: BooleanProxy<TRequired, THasDefault>;
|
|
25
|
+
readonly _TRequired: TRequired;
|
|
26
|
+
readonly _THasDefault: THasDefault;
|
|
27
|
+
readonly TUpdateInput: InferUpdateInput<TRequired, THasDefault>;
|
|
28
|
+
readonly TSetInput: InferSetInput<TRequired, THasDefault>;
|
|
29
|
+
private readonly _schema;
|
|
30
|
+
private readonly _opDefinitions;
|
|
31
|
+
constructor(schema: BooleanPrimitiveSchema);
|
|
32
|
+
/** Mark this boolean as required */
|
|
33
|
+
required(): BooleanPrimitive<true, THasDefault>;
|
|
34
|
+
/** Set a default value for this boolean */
|
|
35
|
+
default(defaultValue: boolean): BooleanPrimitive<TRequired, true>;
|
|
36
|
+
/** Add a custom validation rule */
|
|
37
|
+
refine(fn: (value: boolean) => boolean, message: string): BooleanPrimitive<TRequired, THasDefault>;
|
|
38
|
+
readonly _internal: PrimitiveInternal<boolean, BooleanProxy<TRequired, THasDefault>>;
|
|
39
|
+
}
|
|
40
|
+
/** Creates a new BooleanPrimitive */
|
|
41
|
+
declare const Boolean: () => BooleanPrimitive<false, false>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { Boolean, BooleanPrimitive, BooleanProxy };
|
|
44
|
+
//# sourceMappingURL=Boolean.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Boolean.d.cts","names":[],"sources":["../../src/primitives/Boolean.ts"],"sourcesContent":[],"mappings":";;;KAUK,wFAAwF,oBAAoB,WAAW;KACvH,2FAA2F,oBAAoB,WAAW;AAD1H,UAGY,YAHC,CAAA,kBAAA,OAAA,GAAA,KAAA,EAAA,oBAAA,OAAA,GAAA,KAAA,CAAA,CAAA;EAA+F;EAAW,GAAA,EAAA,EAKnH,cALmH,CAAA,OAAA,EAK3F,SAL2F,EAKhF,WALgF,CAAA;EAA/B;EAAU,GAAA,CAAA,KAAA,EAO1F,aAP0F,CAO5E,SAP4E,EAOjE,WAPiE,CAAA,CAAA,EAAA,IAAA;EAClG;EAA+G,MAAA,CAAA,KAAA,EAQpG,gBARoG,CAQnF,SARmF,EAQxE,WARwE,CAAA,CA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AA,GAAA,OAAA,EAAA,OAAA,EAAA,MAAA,CAAA,EAyCpJ,gBAzCoJ,CAyCnI,SAzCmI,EAyCxH,WAzCwH,CAAA;EAAW,SAAA,SAAA,EAgDrM,iBAhDqM,CAAA,OAAA,EAgD1K,YAhD0K,CAgD7J,SAhD6J,EAgDlJ,WAhDkJ,CAAA,CAAA;;;AAA0C,cA6GxP,OA7GwP,EAAA,GAAA,GA6G1O,gBA7G0O,CAAA,KAAA,EAAA,KAAA,CAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { MaybeUndefined, NeedsValue, Primitive, PrimitiveInternal, Validator } from "./shared.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/primitives/Boolean.d.ts
|
|
4
|
+
type InferSetInput<TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<boolean, TRequired, THasDefault>;
|
|
5
|
+
type InferUpdateInput<TRequired extends boolean = false, THasDefault extends boolean = false> = NeedsValue<boolean, TRequired, THasDefault>;
|
|
6
|
+
interface BooleanProxy<TRequired extends boolean = false, THasDefault extends boolean = false> {
|
|
7
|
+
/** Gets the current boolean value */
|
|
8
|
+
get(): MaybeUndefined<boolean, TRequired, THasDefault>;
|
|
9
|
+
/** Sets the boolean value, generating a boolean.set operation */
|
|
10
|
+
set(value: InferSetInput<TRequired, THasDefault>): void;
|
|
11
|
+
/** This is the same as set. Updates the boolean value, generating a boolean.set operation */
|
|
12
|
+
update(value: InferUpdateInput<TRequired, THasDefault>): void;
|
|
13
|
+
/** Returns a readonly snapshot of the boolean value for rendering */
|
|
14
|
+
toSnapshot(): MaybeUndefined<boolean, TRequired, THasDefault>;
|
|
15
|
+
}
|
|
16
|
+
interface BooleanPrimitiveSchema {
|
|
17
|
+
readonly required: boolean;
|
|
18
|
+
readonly defaultValue: boolean | undefined;
|
|
19
|
+
readonly validators: readonly Validator<boolean>[];
|
|
20
|
+
}
|
|
21
|
+
declare class BooleanPrimitive<TRequired extends boolean = false, THasDefault extends boolean = false> implements Primitive<boolean, BooleanProxy<TRequired, THasDefault>, TRequired, THasDefault, InferSetInput<TRequired, THasDefault>, InferUpdateInput<TRequired, THasDefault>> {
|
|
22
|
+
readonly _tag: "BooleanPrimitive";
|
|
23
|
+
readonly _State: boolean;
|
|
24
|
+
readonly _Proxy: BooleanProxy<TRequired, THasDefault>;
|
|
25
|
+
readonly _TRequired: TRequired;
|
|
26
|
+
readonly _THasDefault: THasDefault;
|
|
27
|
+
readonly TUpdateInput: InferUpdateInput<TRequired, THasDefault>;
|
|
28
|
+
readonly TSetInput: InferSetInput<TRequired, THasDefault>;
|
|
29
|
+
private readonly _schema;
|
|
30
|
+
private readonly _opDefinitions;
|
|
31
|
+
constructor(schema: BooleanPrimitiveSchema);
|
|
32
|
+
/** Mark this boolean as required */
|
|
33
|
+
required(): BooleanPrimitive<true, THasDefault>;
|
|
34
|
+
/** Set a default value for this boolean */
|
|
35
|
+
default(defaultValue: boolean): BooleanPrimitive<TRequired, true>;
|
|
36
|
+
/** Add a custom validation rule */
|
|
37
|
+
refine(fn: (value: boolean) => boolean, message: string): BooleanPrimitive<TRequired, THasDefault>;
|
|
38
|
+
readonly _internal: PrimitiveInternal<boolean, BooleanProxy<TRequired, THasDefault>>;
|
|
39
|
+
}
|
|
40
|
+
/** Creates a new BooleanPrimitive */
|
|
41
|
+
declare const Boolean: () => BooleanPrimitive<false, false>;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { Boolean, BooleanPrimitive, BooleanProxy };
|
|
44
|
+
//# sourceMappingURL=Boolean.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Boolean.d.mts","names":[],"sources":["../../src/primitives/Boolean.ts"],"sourcesContent":[],"mappings":";;;KAUK,wFAAwF,oBAAoB,WAAW;KACvH,2FAA2F,oBAAoB,WAAW;AAD1H,UAGY,YAHC,CAAA,kBAAA,OAAA,GAAA,KAAA,EAAA,oBAAA,OAAA,GAAA,KAAA,CAAA,CAAA;EAA+F;EA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O;EAAoH;EAAW,QAAA,CAAA,CAAA,EAyB9I,gBAzB8I,CAAA,IAAA,EAyBvH,WAzBuH,CAAA;EAAxB;EAAsC,OAAA,CAAA,YAAA,EAAA,OAAA,CAAA,EAiCxI,gBAjCwI,CAiCvH,SAjCuH,EAAA,IAAA,CAAA;EAAW;EAA2B,MAAA,CAAA,EAAA,EAAA,CAAA,KAAA,EAAA,OAAA,EAAA,GAAA,OAAA,EAAA,OAAA,EAAA,MAAA,CAAA,EAyCpJ,gBAzCoJ,CAyCnI,SAzCmI,EAyCxH,WAzCwH,CAAA;EAAW,SAAA,SAAA,EAgDrM,iBAhDqM,CAAA,OAAA,EAgD1K,YAhD0K,CAgD7J,SAhD6J,EAgDlJ,WAhDkJ,CAAA,CAAA;;;AAA0C,cA6GxP,OA7GwP,EAAA,GAAA,GA6G1O,gBA7G0O,CAAA,KAAA,EAAA,KAAA,CAAA"}
|
|
@@ -0,0 +1,94 @@
|
|
|
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
|
+
import { ValidationError, runValidators } from "./shared.mjs";
|
|
4
|
+
import { make } from "../OperationDefinition.mjs";
|
|
5
|
+
import { pathsOverlap } from "../OperationPath.mjs";
|
|
6
|
+
import { fromDefinition } from "../Operation.mjs";
|
|
7
|
+
import { Schema } from "effect";
|
|
8
|
+
|
|
9
|
+
//#region src/primitives/Boolean.ts
|
|
10
|
+
var BooleanPrimitive = class BooleanPrimitive {
|
|
11
|
+
constructor(schema) {
|
|
12
|
+
_defineProperty(this, "_tag", "BooleanPrimitive");
|
|
13
|
+
_defineProperty(this, "_State", void 0);
|
|
14
|
+
_defineProperty(this, "_Proxy", void 0);
|
|
15
|
+
_defineProperty(this, "_TRequired", void 0);
|
|
16
|
+
_defineProperty(this, "_THasDefault", void 0);
|
|
17
|
+
_defineProperty(this, "TUpdateInput", void 0);
|
|
18
|
+
_defineProperty(this, "TSetInput", void 0);
|
|
19
|
+
_defineProperty(this, "_schema", void 0);
|
|
20
|
+
_defineProperty(this, "_opDefinitions", { set: make({
|
|
21
|
+
kind: "boolean.set",
|
|
22
|
+
payload: Schema.Boolean,
|
|
23
|
+
target: Schema.Boolean,
|
|
24
|
+
apply: (payload) => payload
|
|
25
|
+
}) });
|
|
26
|
+
_defineProperty(this, "_internal", {
|
|
27
|
+
createProxy: (env, operationPath) => {
|
|
28
|
+
const defaultValue = this._schema.defaultValue;
|
|
29
|
+
return {
|
|
30
|
+
get: () => {
|
|
31
|
+
const state = env.getState(operationPath);
|
|
32
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
33
|
+
},
|
|
34
|
+
set: (value) => {
|
|
35
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
36
|
+
},
|
|
37
|
+
update: (value) => {
|
|
38
|
+
env.addOperation(fromDefinition(operationPath, this._opDefinitions.set, value));
|
|
39
|
+
},
|
|
40
|
+
toSnapshot: () => {
|
|
41
|
+
const state = env.getState(operationPath);
|
|
42
|
+
return state !== null && state !== void 0 ? state : defaultValue;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
applyOperation: (_state, operation) => {
|
|
47
|
+
if (operation.kind !== "boolean.set") throw new ValidationError(`BooleanPrimitive cannot apply operation of kind: ${operation.kind}`);
|
|
48
|
+
const payload = operation.payload;
|
|
49
|
+
if (typeof payload !== "boolean") throw new ValidationError(`BooleanPrimitive.set requires a boolean payload, got: ${typeof payload}`);
|
|
50
|
+
runValidators(payload, this._schema.validators);
|
|
51
|
+
return payload;
|
|
52
|
+
},
|
|
53
|
+
getInitialState: () => {
|
|
54
|
+
return this._schema.defaultValue;
|
|
55
|
+
},
|
|
56
|
+
transformOperation: (clientOp, serverOp) => {
|
|
57
|
+
if (!pathsOverlap(clientOp.path, serverOp.path)) return {
|
|
58
|
+
type: "transformed",
|
|
59
|
+
operation: clientOp
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
type: "transformed",
|
|
63
|
+
operation: clientOp
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
this._schema = schema;
|
|
68
|
+
}
|
|
69
|
+
/** Mark this boolean as required */
|
|
70
|
+
required() {
|
|
71
|
+
return new BooleanPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { required: true }));
|
|
72
|
+
}
|
|
73
|
+
/** Set a default value for this boolean */
|
|
74
|
+
default(defaultValue) {
|
|
75
|
+
return new BooleanPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { defaultValue }));
|
|
76
|
+
}
|
|
77
|
+
/** Add a custom validation rule */
|
|
78
|
+
refine(fn, message) {
|
|
79
|
+
return new BooleanPrimitive(_objectSpread2(_objectSpread2({}, this._schema), {}, { validators: [...this._schema.validators, {
|
|
80
|
+
validate: fn,
|
|
81
|
+
message
|
|
82
|
+
}] }));
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
/** Creates a new BooleanPrimitive */
|
|
86
|
+
const Boolean = () => new BooleanPrimitive({
|
|
87
|
+
required: false,
|
|
88
|
+
defaultValue: void 0,
|
|
89
|
+
validators: []
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
export { Boolean, BooleanPrimitive };
|
|
94
|
+
//# sourceMappingURL=Boolean.mjs.map
|