@dxos/effect 0.8.3 → 0.8.4-main.1068cf700f
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/dist/lib/browser/chunk-CGS2ULMK.mjs +11 -0
- package/dist/lib/browser/chunk-CGS2ULMK.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +458 -87
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing.mjs +39 -0
- package/dist/lib/browser/testing.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +458 -87
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing.mjs +39 -0
- package/dist/lib/node-esm/testing.mjs.map +7 -0
- package/dist/types/src/RuntimeProvider.d.ts +21 -0
- package/dist/types/src/RuntimeProvider.d.ts.map +1 -0
- package/dist/types/src/ast.d.ts +36 -18
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/atom-kvs.d.ts +19 -0
- package/dist/types/src/atom-kvs.d.ts.map +1 -0
- package/dist/types/src/context.d.ts +5 -0
- package/dist/types/src/context.d.ts.map +1 -0
- package/dist/types/src/dynamic-runtime.d.ts +56 -0
- package/dist/types/src/dynamic-runtime.d.ts.map +1 -0
- package/dist/types/src/dynamic-runtime.test.d.ts +2 -0
- package/dist/types/src/dynamic-runtime.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +57 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- package/dist/types/src/errors.test.d.ts +2 -0
- package/dist/types/src/errors.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +7 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/interrupt.test.d.ts +2 -0
- package/dist/types/src/interrupt.test.d.ts.map +1 -0
- package/dist/types/src/{jsonPath.d.ts → json-path.d.ts} +12 -4
- package/dist/types/src/json-path.d.ts.map +1 -0
- package/dist/types/src/json-path.test.d.ts +2 -0
- package/dist/types/src/json-path.test.d.ts.map +1 -0
- package/dist/types/src/layers.test.d.ts +2 -0
- package/dist/types/src/layers.test.d.ts.map +1 -0
- package/dist/types/src/otel.d.ts +17 -0
- package/dist/types/src/otel.d.ts.map +1 -0
- package/dist/types/src/otel.test.d.ts +2 -0
- package/dist/types/src/otel.test.d.ts.map +1 -0
- package/dist/types/src/resource.d.ts +8 -0
- package/dist/types/src/resource.d.ts.map +1 -0
- package/dist/types/src/resource.test.d.ts +2 -0
- package/dist/types/src/resource.test.d.ts.map +1 -0
- package/dist/types/src/testing.d.ts +68 -0
- package/dist/types/src/testing.d.ts.map +1 -0
- package/dist/types/src/url.d.ts +3 -1
- package/dist/types/src/url.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +37 -8
- package/src/RuntimeProvider.ts +35 -0
- package/src/ast.test.ts +55 -10
- package/src/ast.ts +123 -80
- package/src/atom-kvs.ts +35 -0
- package/src/context.ts +16 -0
- package/src/dynamic-runtime.test.ts +465 -0
- package/src/dynamic-runtime.ts +195 -0
- package/src/errors.test.ts +22 -0
- package/src/errors.ts +252 -0
- package/src/index.ts +7 -1
- package/src/interrupt.test.ts +35 -0
- package/src/{jsonPath.test.ts → json-path.test.ts} +47 -8
- package/src/{jsonPath.ts → json-path.ts} +29 -4
- package/src/layers.test.ts +112 -0
- package/src/otel.test.ts +126 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +32 -0
- package/src/resource.ts +30 -0
- package/src/sanity.test.ts +30 -15
- package/src/testing.ts +112 -0
- package/src/url.test.ts +1 -1
- package/src/url.ts +5 -2
- package/dist/lib/node/index.cjs +0 -487
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/types/src/jsonPath.d.ts.map +0 -1
- package/dist/types/src/jsonPath.test.d.ts +0 -2
- package/dist/types/src/jsonPath.test.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,39 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4-main.1068cf700f",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"type": "module",
|
|
10
14
|
"exports": {
|
|
11
15
|
".": {
|
|
16
|
+
"source": "./src/index.ts",
|
|
12
17
|
"types": "./dist/types/src/index.d.ts",
|
|
13
18
|
"browser": "./dist/lib/browser/index.mjs",
|
|
14
19
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
20
|
+
},
|
|
21
|
+
"./testing": {
|
|
22
|
+
"source": "./src/testing.ts",
|
|
23
|
+
"types": "./dist/types/src/testing.d.ts",
|
|
24
|
+
"browser": "./dist/lib/browser/testing.mjs",
|
|
25
|
+
"node": "./dist/lib/node-esm/testing.mjs"
|
|
15
26
|
}
|
|
16
27
|
},
|
|
17
28
|
"types": "dist/types/src/index.d.ts",
|
|
18
29
|
"typesVersions": {
|
|
19
|
-
"*": {
|
|
30
|
+
"*": {
|
|
31
|
+
"testing": [
|
|
32
|
+
"./dist/types/src/testing.d.ts"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
20
35
|
},
|
|
21
36
|
"files": [
|
|
22
37
|
"dist",
|
|
23
38
|
"src"
|
|
24
39
|
],
|
|
25
40
|
"dependencies": {
|
|
41
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
42
|
+
"@effect/opentelemetry": "^0.61.0",
|
|
43
|
+
"@opentelemetry/api": "^1.9.0",
|
|
26
44
|
"jsonpath-plus": "10.2.0",
|
|
27
|
-
"@dxos/
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/util": "0.8.
|
|
45
|
+
"@dxos/context": "0.8.4-main.1068cf700f",
|
|
46
|
+
"@dxos/invariant": "0.8.4-main.1068cf700f",
|
|
47
|
+
"@dxos/util": "0.8.4-main.1068cf700f",
|
|
48
|
+
"@dxos/node-std": "0.8.4-main.1068cf700f"
|
|
30
49
|
},
|
|
31
50
|
"devDependencies": {
|
|
32
|
-
"effect": "
|
|
33
|
-
"@
|
|
51
|
+
"@effect/platform": "0.94.4",
|
|
52
|
+
"@effect/platform-browser": "0.74.0",
|
|
53
|
+
"@opentelemetry/api-logs": "^0.203.0",
|
|
54
|
+
"@opentelemetry/resources": "^2.1.0",
|
|
55
|
+
"@opentelemetry/sdk-logs": "^0.203.0",
|
|
56
|
+
"@opentelemetry/sdk-node": "^0.203.0",
|
|
57
|
+
"@opentelemetry/sdk-trace-node": "^2.1.0",
|
|
58
|
+
"@opentelemetry/semantic-conventions": "^1.37.0",
|
|
59
|
+
"effect": "3.19.16",
|
|
60
|
+
"@dxos/log": "0.8.4-main.1068cf700f"
|
|
34
61
|
},
|
|
35
62
|
"peerDependencies": {
|
|
36
|
-
"effect": "
|
|
63
|
+
"@effect/platform": "0.94.4",
|
|
64
|
+
"@effect/platform-browser": "0.74.0",
|
|
65
|
+
"effect": "3.19.16"
|
|
37
66
|
},
|
|
38
67
|
"publishConfig": {
|
|
39
68
|
"access": "public"
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Runtime from 'effect/Runtime';
|
|
7
|
+
|
|
8
|
+
import { runAndForwardErrors, unwrapExit } from './errors';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Provides effect runtime with services to run effects.
|
|
12
|
+
*/
|
|
13
|
+
export type RuntimeProvider<R> = Effect.Effect<Runtime.Runtime<R>>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @returns Runtime provider from the current context.
|
|
17
|
+
*/
|
|
18
|
+
export const currentRuntime = <R = never>() => Effect.runtime<R>().pipe(Effect.map(Effect.succeed));
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Run effect, within runitme, clean errors and fix stack-traces.
|
|
22
|
+
*/
|
|
23
|
+
export const runPromise =
|
|
24
|
+
<R>(provider: RuntimeProvider<R>) =>
|
|
25
|
+
async <A>(effect: Effect.Effect<A, any, R>): Promise<A> => {
|
|
26
|
+
const runtime = await runAndForwardErrors(provider);
|
|
27
|
+
return unwrapExit(await effect.pipe(Runtime.runPromiseExit(runtime)));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Provide services from runtime provider to effect.
|
|
32
|
+
*/
|
|
33
|
+
export const provide: {
|
|
34
|
+
<R2>(runtime: RuntimeProvider<R2>): <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, R2>>;
|
|
35
|
+
} = (runtimeProvider) => (effect) => Effect.flatMap(runtimeProvider, (runtime) => Effect.provide(effect, runtime));
|
package/src/ast.test.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
7
|
import { describe, test } from 'vitest';
|
|
7
8
|
|
|
8
9
|
import { invariant } from '@dxos/invariant';
|
|
@@ -12,14 +13,15 @@ import {
|
|
|
12
13
|
findNode,
|
|
13
14
|
findProperty,
|
|
14
15
|
getAnnotation,
|
|
15
|
-
getDiscriminatingProps,
|
|
16
16
|
getDiscriminatedType,
|
|
17
|
-
|
|
17
|
+
getDiscriminatingProps,
|
|
18
|
+
getProperties,
|
|
19
|
+
isArrayType,
|
|
20
|
+
isDiscriminatedUnion,
|
|
18
21
|
isOption,
|
|
19
|
-
isSimpleType,
|
|
20
22
|
visit,
|
|
21
23
|
} from './ast';
|
|
22
|
-
import { type JsonPath, type JsonProp } from './
|
|
24
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
23
25
|
|
|
24
26
|
const ZipCode = Schema.String.pipe(
|
|
25
27
|
Schema.pattern(/^\d{5}$/, {
|
|
@@ -63,10 +65,9 @@ describe('AST', () => {
|
|
|
63
65
|
|
|
64
66
|
const prop = findProperty(TestSchema, 'name' as JsonProp);
|
|
65
67
|
invariant(prop);
|
|
66
|
-
const node = findNode(prop,
|
|
68
|
+
const node = findNode(prop, (node) => node._tag === 'StringKeyword');
|
|
67
69
|
invariant(node);
|
|
68
|
-
|
|
69
|
-
expect(type).to.eq('string');
|
|
70
|
+
expect(node._tag).to.eq('StringKeyword');
|
|
70
71
|
});
|
|
71
72
|
|
|
72
73
|
test('findProperty', ({ expect }) => {
|
|
@@ -90,6 +91,30 @@ describe('AST', () => {
|
|
|
90
91
|
}
|
|
91
92
|
});
|
|
92
93
|
|
|
94
|
+
test('getProperties preserves annotation on property type after refinements', ({ expect }) => {
|
|
95
|
+
// When a property is e.g. Format.Text.pipe(nonEmptyString(), maxLength(), Schema.annotations({ title, description })),
|
|
96
|
+
// the form uses getProperties(schema.ast) and then Format.FormatAnnotation.getFromAst(property.type).
|
|
97
|
+
// Custom title and description from the outer Schema.annotations() must not be lost.
|
|
98
|
+
const WithRefinements = Schema.Struct({
|
|
99
|
+
message: Schema.String.annotations({ title: 'Feedback' }).pipe(
|
|
100
|
+
Schema.minLength(1),
|
|
101
|
+
Schema.maxLength(4096),
|
|
102
|
+
Schema.annotations({
|
|
103
|
+
title: 'Feedback label',
|
|
104
|
+
description: 'Feedback placeholder',
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
});
|
|
108
|
+
const properties = getProperties(WithRefinements.ast);
|
|
109
|
+
const messageProp = properties.find((p) => p.name === 'message');
|
|
110
|
+
invariant(messageProp);
|
|
111
|
+
const title = findAnnotation(messageProp.type, SchemaAST.TitleAnnotationId);
|
|
112
|
+
const description = findAnnotation(messageProp.type, SchemaAST.DescriptionAnnotationId);
|
|
113
|
+
// Outer Schema.annotations() wins so form labels/placeholders are preserved.
|
|
114
|
+
expect(title).to.eq('Feedback label');
|
|
115
|
+
expect(description).to.eq('Feedback placeholder');
|
|
116
|
+
});
|
|
117
|
+
|
|
93
118
|
test('findAnnotation', ({ expect }) => {
|
|
94
119
|
const TestSchema = Schema.NonEmptyString.pipe(Schema.pattern(/^\d{5}$/)).annotations({
|
|
95
120
|
title: 'original title',
|
|
@@ -135,7 +160,11 @@ describe('AST', () => {
|
|
|
135
160
|
});
|
|
136
161
|
|
|
137
162
|
const props: string[] = [];
|
|
138
|
-
visit(
|
|
163
|
+
visit(
|
|
164
|
+
TestSchema.ast,
|
|
165
|
+
(_, path) => props.push(path.join('.')),
|
|
166
|
+
(node, path, depth) => depth < 3,
|
|
167
|
+
);
|
|
139
168
|
});
|
|
140
169
|
|
|
141
170
|
test('discriminated unions', ({ expect }) => {
|
|
@@ -150,7 +179,7 @@ describe('AST', () => {
|
|
|
150
179
|
expect(isOption(TestUnionSchema.ast)).to.be.false;
|
|
151
180
|
expect(getDiscriminatingProps(TestUnionSchema.ast)).to.deep.eq(['kind']);
|
|
152
181
|
|
|
153
|
-
const node = findNode(TestUnionSchema.ast,
|
|
182
|
+
const node = findNode(TestUnionSchema.ast, isDiscriminatedUnion);
|
|
154
183
|
expect(node).to.eq(TestUnionSchema.ast);
|
|
155
184
|
}
|
|
156
185
|
|
|
@@ -178,4 +207,20 @@ describe('AST', () => {
|
|
|
178
207
|
);
|
|
179
208
|
}
|
|
180
209
|
});
|
|
210
|
+
|
|
211
|
+
test('Schema.pluck', ({ expect }) => {
|
|
212
|
+
const TestSchema = Schema.Struct({
|
|
213
|
+
name: Schema.String,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
expect(TestSchema.pipe(Schema.pluck('name'), Schema.typeSchema).ast).toEqual(SchemaAST.stringKeyword);
|
|
217
|
+
expect(() => TestSchema.pipe(Schema.pluck('missing' as any), Schema.typeSchema)).to.throw();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('isArray', ({ expect }) => {
|
|
221
|
+
expect(isArrayType(Schema.String.ast)).to.be.false;
|
|
222
|
+
expect(isArrayType(Schema.Array(Schema.String).ast)).to.be.true;
|
|
223
|
+
expect(isArrayType(findProperty(Schema.Struct({ a: Schema.Array(Schema.String) }), 'a' as JsonPath)!)).to.be.true;
|
|
224
|
+
expect(isArrayType(Schema.Union(Schema.String, Schema.Array(Schema.String)).ast)).to.be.false;
|
|
225
|
+
});
|
|
181
226
|
});
|
package/src/ast.ts
CHANGED
|
@@ -2,12 +2,15 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Function from 'effect/Function';
|
|
6
|
+
import * as Option from 'effect/Option';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
6
9
|
|
|
7
10
|
import { invariant } from '@dxos/invariant';
|
|
8
11
|
import { isNonNullable } from '@dxos/util';
|
|
9
12
|
|
|
10
|
-
import { type JsonPath, type JsonProp } from './
|
|
13
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
11
14
|
|
|
12
15
|
//
|
|
13
16
|
// Refs
|
|
@@ -16,67 +19,56 @@ import { type JsonPath, type JsonProp } from './jsonPath';
|
|
|
16
19
|
// https://effect-ts.github.io/effect/schema/SchemaAST.ts.html
|
|
17
20
|
//
|
|
18
21
|
|
|
19
|
-
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
20
|
-
|
|
21
22
|
/**
|
|
22
|
-
*
|
|
23
|
+
* Unwraps and collects refinement filters.
|
|
23
24
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return 'string';
|
|
36
|
-
}
|
|
37
|
-
if (SchemaAST.isNumberKeyword(node)) {
|
|
38
|
-
return 'number';
|
|
39
|
-
}
|
|
40
|
-
if (SchemaAST.isBooleanKeyword(node)) {
|
|
41
|
-
return 'boolean';
|
|
25
|
+
const reduceRefinements = (
|
|
26
|
+
type: SchemaAST.AST,
|
|
27
|
+
refinements: SchemaAST.Refinement['filter'][] = [],
|
|
28
|
+
): { type: SchemaAST.AST; refinements: SchemaAST.Refinement['filter'][] } => {
|
|
29
|
+
if (SchemaAST.isRefinement(type)) {
|
|
30
|
+
const filter = type.filter;
|
|
31
|
+
const nextType = {
|
|
32
|
+
...type.from,
|
|
33
|
+
annotations: { ...type.from.annotations, ...type.annotations },
|
|
34
|
+
} as SchemaAST.AST;
|
|
35
|
+
return reduceRefinements(nextType, [...refinements, filter]);
|
|
42
36
|
}
|
|
43
37
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
38
|
+
return { type, refinements };
|
|
39
|
+
};
|
|
47
40
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Get the base type of a property.
|
|
43
|
+
*
|
|
44
|
+
* Unwraps refinements and optional unions.
|
|
45
|
+
*/
|
|
46
|
+
export const getBaseType = (
|
|
47
|
+
prop: SchemaAST.PropertySignature | SchemaProperty,
|
|
48
|
+
): { type: SchemaAST.AST; refinements: SchemaAST.Refinement['filter'][] } => {
|
|
49
|
+
const encoded = SchemaAST.encodedBoundAST(prop.type);
|
|
50
|
+
// Extract property ast from optional union.
|
|
51
|
+
const unwrapped = prop.isOptional && encoded._tag === 'Union' ? encoded.types[0] : encoded;
|
|
52
|
+
return reduceRefinements(unwrapped);
|
|
51
53
|
};
|
|
52
54
|
|
|
53
|
-
export
|
|
55
|
+
export type SchemaProperty = Pick<SchemaAST.PropertySignature, 'name' | 'type' | 'isOptional' | 'isReadonly'> & {
|
|
56
|
+
/** Can be used to validate the property to the spec of the initial AST. */
|
|
57
|
+
refinements: SchemaAST.Refinement['filter'][];
|
|
58
|
+
};
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
case 'boolean': {
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
case 'object': {
|
|
72
|
-
return {};
|
|
73
|
-
}
|
|
74
|
-
default: {
|
|
75
|
-
throw new Error(`Unsupported type for default value: ${type}`);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the property types of an AST.
|
|
62
|
+
*/
|
|
63
|
+
export const getProperties = (ast: SchemaAST.AST): SchemaProperty[] => {
|
|
64
|
+
const properties = SchemaAST.getPropertySignatures(ast);
|
|
65
|
+
return properties.map((prop) => ({
|
|
66
|
+
...getBaseType(prop),
|
|
67
|
+
name: prop.name,
|
|
68
|
+
isOptional: prop.isOptional,
|
|
69
|
+
isReadonly: prop.isReadonly,
|
|
70
|
+
}));
|
|
71
|
+
};
|
|
80
72
|
|
|
81
73
|
//
|
|
82
74
|
// Branded types
|
|
@@ -100,23 +92,14 @@ export type TestFn = (node: SchemaAST.AST, path: Path, depth: number) => VisitRe
|
|
|
100
92
|
|
|
101
93
|
export type VisitorFn = (node: SchemaAST.AST, path: Path, depth: number) => void;
|
|
102
94
|
|
|
103
|
-
const defaultTest: TestFn = isSimpleType;
|
|
104
|
-
|
|
105
95
|
/**
|
|
106
96
|
* Visit leaf nodes.
|
|
107
97
|
* Refs:
|
|
108
98
|
* - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor
|
|
109
99
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
110
100
|
*/
|
|
111
|
-
export const visit: {
|
|
112
|
-
(node
|
|
113
|
-
(node: SchemaAST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
114
|
-
} = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {
|
|
115
|
-
if (!visitor) {
|
|
116
|
-
visitNode(node, defaultTest, testOrVisitor);
|
|
117
|
-
} else {
|
|
118
|
-
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
119
|
-
}
|
|
101
|
+
export const visit = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor: VisitorFn): void => {
|
|
102
|
+
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
120
103
|
};
|
|
121
104
|
|
|
122
105
|
const visitNode = (
|
|
@@ -126,15 +109,15 @@ const visitNode = (
|
|
|
126
109
|
path: Path = [],
|
|
127
110
|
depth = 0,
|
|
128
111
|
): VisitResult | undefined => {
|
|
129
|
-
const
|
|
112
|
+
const $result = test?.(node, path, depth);
|
|
130
113
|
const result: VisitResult =
|
|
131
|
-
|
|
114
|
+
$result === undefined
|
|
132
115
|
? VisitResult.CONTINUE
|
|
133
|
-
: typeof
|
|
134
|
-
?
|
|
116
|
+
: typeof $result === 'boolean'
|
|
117
|
+
? $result
|
|
135
118
|
? VisitResult.CONTINUE
|
|
136
119
|
: VisitResult.SKIP
|
|
137
|
-
:
|
|
120
|
+
: $result;
|
|
138
121
|
|
|
139
122
|
if (result === VisitResult.EXIT) {
|
|
140
123
|
return result;
|
|
@@ -203,6 +186,12 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
203
186
|
return child;
|
|
204
187
|
}
|
|
205
188
|
}
|
|
189
|
+
for (const prop of getIndexSignatures(node)) {
|
|
190
|
+
const child = findNode(prop.type, test);
|
|
191
|
+
if (child) {
|
|
192
|
+
return child;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
206
195
|
}
|
|
207
196
|
|
|
208
197
|
// Tuple.
|
|
@@ -217,12 +206,14 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
217
206
|
|
|
218
207
|
// Branching union (e.g., optional, discriminated unions).
|
|
219
208
|
else if (SchemaAST.isUnion(node)) {
|
|
220
|
-
if (
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
209
|
+
if (isLiteralUnion(node)) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
for (const type of node.types) {
|
|
214
|
+
const child = findNode(type, test);
|
|
215
|
+
if (child) {
|
|
216
|
+
return child;
|
|
226
217
|
}
|
|
227
218
|
}
|
|
228
219
|
}
|
|
@@ -278,8 +269,8 @@ export const getAnnotation =
|
|
|
278
269
|
<T>(annotationId: symbol, noDefault = true) =>
|
|
279
270
|
(node: SchemaAST.AST): T | undefined => {
|
|
280
271
|
// Title fallback seems to be the identifier.
|
|
281
|
-
const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
282
|
-
const value = pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
272
|
+
const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
273
|
+
const value = Function.pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
283
274
|
if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
|
|
284
275
|
return undefined;
|
|
285
276
|
}
|
|
@@ -325,10 +316,31 @@ export const isOption = (node: SchemaAST.AST): boolean => {
|
|
|
325
316
|
/**
|
|
326
317
|
* Determines if the node is a union of literal types.
|
|
327
318
|
*/
|
|
328
|
-
export const isLiteralUnion = (node: SchemaAST.AST):
|
|
319
|
+
export const isLiteralUnion = (node: SchemaAST.AST): node is SchemaAST.Union<SchemaAST.Literal> => {
|
|
329
320
|
return SchemaAST.isUnion(node) && node.types.every(SchemaAST.isLiteral);
|
|
330
321
|
};
|
|
331
322
|
|
|
323
|
+
/**
|
|
324
|
+
* Determines if the node is an array type.
|
|
325
|
+
*/
|
|
326
|
+
export const isArrayType = (node: SchemaAST.AST): node is SchemaAST.TupleType => {
|
|
327
|
+
return SchemaAST.isTupleType(node) && node.elements.length === 0 && node.rest.length === 1;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Get the type of the array elements.
|
|
332
|
+
*/
|
|
333
|
+
export const getArrayElementType = (node: SchemaAST.AST): SchemaAST.AST | undefined => {
|
|
334
|
+
return isArrayType(node) ? node.rest.at(0)?.type : undefined;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Determines if the node is a tuple type.
|
|
339
|
+
*/
|
|
340
|
+
export const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
341
|
+
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
342
|
+
};
|
|
343
|
+
|
|
332
344
|
/**
|
|
333
345
|
* Determines if the node is a discriminated union.
|
|
334
346
|
*/
|
|
@@ -408,6 +420,20 @@ export const getDiscriminatedType = (
|
|
|
408
420
|
return schema.ast;
|
|
409
421
|
};
|
|
410
422
|
|
|
423
|
+
/**
|
|
424
|
+
* Determines if the node is a nested object type.
|
|
425
|
+
*/
|
|
426
|
+
export const isNestedType = (node: SchemaAST.AST): boolean => {
|
|
427
|
+
return (
|
|
428
|
+
SchemaAST.isDeclaration(node) ||
|
|
429
|
+
SchemaAST.isObjectKeyword(node) ||
|
|
430
|
+
SchemaAST.isTypeLiteral(node) ||
|
|
431
|
+
// TODO(wittjosiah): Tuples are actually arrays.
|
|
432
|
+
isTupleType(node) ||
|
|
433
|
+
isDiscriminatedUnion(node)
|
|
434
|
+
);
|
|
435
|
+
};
|
|
436
|
+
|
|
411
437
|
/**
|
|
412
438
|
* Maps AST nodes.
|
|
413
439
|
* The user is responsible for recursively calling {@link mapAst} on the SchemaAST.
|
|
@@ -431,6 +457,7 @@ export const mapAst = (
|
|
|
431
457
|
),
|
|
432
458
|
),
|
|
433
459
|
ast.indexSignatures,
|
|
460
|
+
ast.annotations,
|
|
434
461
|
);
|
|
435
462
|
}
|
|
436
463
|
case 'Union': {
|
|
@@ -454,3 +481,19 @@ export const mapAst = (
|
|
|
454
481
|
}
|
|
455
482
|
}
|
|
456
483
|
};
|
|
484
|
+
|
|
485
|
+
const getIndexSignatures = (ast: SchemaAST.AST): Array<SchemaAST.IndexSignature> => {
|
|
486
|
+
const annotation = SchemaAST.getSurrogateAnnotation(ast);
|
|
487
|
+
if (Option.isSome(annotation)) {
|
|
488
|
+
return getIndexSignatures(annotation.value);
|
|
489
|
+
}
|
|
490
|
+
switch (ast._tag) {
|
|
491
|
+
case 'TypeLiteral':
|
|
492
|
+
return ast.indexSignatures.slice();
|
|
493
|
+
case 'Suspend':
|
|
494
|
+
return getIndexSignatures(ast.f());
|
|
495
|
+
case 'Refinement':
|
|
496
|
+
return getIndexSignatures(ast.from);
|
|
497
|
+
}
|
|
498
|
+
return [];
|
|
499
|
+
};
|
package/src/atom-kvs.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as BrowserKeyValueStore from '@effect/platform-browser/BrowserKeyValueStore';
|
|
6
|
+
import { Atom } from '@effect-atom/atom-react';
|
|
7
|
+
import type * as Schema from 'effect/Schema';
|
|
8
|
+
|
|
9
|
+
// TODO(wittjosiah): This is currently provided for convenience but maybe should be removed.
|
|
10
|
+
const defaultRuntime = Atom.runtime(BrowserKeyValueStore.layerLocalStorage);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Creates a KVS-backed atom for structured settings using Atom.kvs.
|
|
14
|
+
* The entire object is stored as a single localStorage key with JSON serialization.
|
|
15
|
+
*
|
|
16
|
+
* @param options.key - The localStorage key to store the value under.
|
|
17
|
+
* @param options.schema - Effect Schema for the value type.
|
|
18
|
+
* @param options.defaultValue - Function returning the default value.
|
|
19
|
+
* @param options.runtime - Optional custom Atom runtime (defaults to localStorage).
|
|
20
|
+
* @returns A writable atom that persists to localStorage.
|
|
21
|
+
*/
|
|
22
|
+
export const createKvsStore = <T extends Record<string, any>>(options: {
|
|
23
|
+
key: string;
|
|
24
|
+
schema: Schema.Schema<T>;
|
|
25
|
+
defaultValue: () => T;
|
|
26
|
+
runtime?: ReturnType<typeof Atom.runtime>;
|
|
27
|
+
}): Atom.Writable<T> => {
|
|
28
|
+
const runtime = options.runtime ?? defaultRuntime;
|
|
29
|
+
return Atom.kvs({
|
|
30
|
+
runtime,
|
|
31
|
+
key: options.key,
|
|
32
|
+
schema: options.schema,
|
|
33
|
+
defaultValue: options.defaultValue,
|
|
34
|
+
}).pipe(Atom.keepAlive);
|
|
35
|
+
};
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import type * as Scope from 'effect/Scope';
|
|
7
|
+
|
|
8
|
+
import { Context } from '@dxos/context';
|
|
9
|
+
|
|
10
|
+
// TODO(dmaretskyi): Error handling.
|
|
11
|
+
export const contextFromScope = (): Effect.Effect<Context, never, Scope.Scope> =>
|
|
12
|
+
Effect.gen(function* () {
|
|
13
|
+
const ctx = new Context();
|
|
14
|
+
yield* Effect.addFinalizer(() => Effect.promise(() => ctx.dispose()));
|
|
15
|
+
return ctx;
|
|
16
|
+
});
|