@dxos/effect 0.8.4-main.5ea62a8 → 0.8.4-main.60689f5b1c
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/LICENSE +102 -5
- package/README.md +1 -1
- 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 +492 -312
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing.mjs +31 -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 +492 -312
- 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 +31 -0
- package/dist/lib/node-esm/testing.mjs.map +7 -0
- package/dist/types/src/Performance.d.ts +25 -0
- package/dist/types/src/Performance.d.ts.map +1 -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 +42 -22
- package/dist/types/src/ast.d.ts.map +1 -1
- package/dist/types/src/async-task-tagging.d.ts +6 -0
- package/dist/types/src/async-task-tagging.d.ts.map +1 -0
- 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 +2 -1
- package/dist/types/src/context.d.ts.map +1 -1
- 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 +15 -1
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +8 -3
- package/dist/types/src/index.d.ts.map +1 -1
- 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/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 +6 -2
- package/dist/types/src/resource.d.ts.map +1 -1
- package/dist/types/src/testing.d.ts +23 -9
- package/dist/types/src/testing.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +8 -0
- package/dist/types/src/types.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 +33 -13
- package/src/Performance.ts +45 -0
- package/src/RuntimeProvider.ts +35 -0
- package/src/ast.test.ts +37 -9
- package/src/ast.ts +144 -99
- package/src/async-task-tagging.ts +51 -0
- package/src/atom-kvs.ts +35 -0
- package/src/context.ts +2 -1
- package/src/dynamic-runtime.test.ts +465 -0
- package/src/dynamic-runtime.ts +195 -0
- package/src/errors.test.ts +1 -1
- package/src/errors.ts +81 -15
- package/src/index.ts +8 -3
- package/src/interrupt.test.ts +7 -6
- 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 +10 -4
- package/src/otel.test.ts +126 -0
- package/src/otel.ts +45 -0
- package/src/resource.test.ts +3 -3
- package/src/resource.ts +10 -5
- package/src/sanity.test.ts +30 -15
- package/src/testing.ts +27 -23
- package/src/types.ts +11 -0
- package/src/url.test.ts +1 -1
- package/src/url.ts +5 -2
- 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,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/effect",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.60689f5b1c",
|
|
4
4
|
"description": "Effect utils.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"type": "module",
|
|
10
14
|
"exports": {
|
|
@@ -13,29 +17,45 @@
|
|
|
13
17
|
"types": "./dist/types/src/index.d.ts",
|
|
14
18
|
"browser": "./dist/lib/browser/index.mjs",
|
|
15
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"
|
|
16
26
|
}
|
|
17
27
|
},
|
|
18
28
|
"types": "dist/types/src/index.d.ts",
|
|
19
|
-
"typesVersions": {
|
|
20
|
-
"*": {}
|
|
21
|
-
},
|
|
22
29
|
"files": [
|
|
23
30
|
"dist",
|
|
24
31
|
"src"
|
|
25
32
|
],
|
|
26
33
|
"dependencies": {
|
|
27
|
-
"
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"
|
|
31
|
-
"@dxos/
|
|
34
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
35
|
+
"@effect/opentelemetry": "^0.61.0",
|
|
36
|
+
"@opentelemetry/api": "^1.9.1",
|
|
37
|
+
"jsonpath-plus": "^10.3.0",
|
|
38
|
+
"@dxos/context": "0.8.4-main.60689f5b1c",
|
|
39
|
+
"@dxos/invariant": "0.8.4-main.60689f5b1c",
|
|
40
|
+
"@dxos/node-std": "0.8.4-main.60689f5b1c",
|
|
41
|
+
"@dxos/util": "0.8.4-main.60689f5b1c"
|
|
32
42
|
},
|
|
33
43
|
"devDependencies": {
|
|
34
|
-
"effect": "
|
|
35
|
-
"@
|
|
44
|
+
"@effect/platform": "0.94.4",
|
|
45
|
+
"@effect/platform-browser": "0.74.0",
|
|
46
|
+
"@opentelemetry/api-logs": "^0.218.0",
|
|
47
|
+
"@opentelemetry/resources": "^2.7.1",
|
|
48
|
+
"@opentelemetry/sdk-logs": "^0.218.0",
|
|
49
|
+
"@opentelemetry/sdk-node": "^0.218.0",
|
|
50
|
+
"@opentelemetry/sdk-trace-node": "^2.7.1",
|
|
51
|
+
"@opentelemetry/semantic-conventions": "^1.41.1",
|
|
52
|
+
"effect": "3.20.0",
|
|
53
|
+
"@dxos/log": "0.8.4-main.60689f5b1c"
|
|
36
54
|
},
|
|
37
55
|
"peerDependencies": {
|
|
38
|
-
"effect": "
|
|
56
|
+
"@effect/platform": "0.94.4",
|
|
57
|
+
"@effect/platform-browser": "0.74.0",
|
|
58
|
+
"effect": "3.20.0"
|
|
39
59
|
},
|
|
40
60
|
"publishConfig": {
|
|
41
61
|
"access": "public"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import type * as Exit from 'effect/Exit';
|
|
7
|
+
|
|
8
|
+
export interface AddTrackEntryOptions {
|
|
9
|
+
name: string;
|
|
10
|
+
devtools?: {
|
|
11
|
+
/**
|
|
12
|
+
* @example 'track-entry'
|
|
13
|
+
*/
|
|
14
|
+
dataType: string;
|
|
15
|
+
track: string;
|
|
16
|
+
trackGroup: string;
|
|
17
|
+
/**
|
|
18
|
+
* @example 'tertiary-dark'
|
|
19
|
+
*/
|
|
20
|
+
color: string;
|
|
21
|
+
properties?: [string, any][];
|
|
22
|
+
tooltipText?: string;
|
|
23
|
+
};
|
|
24
|
+
detail?: Record<string, unknown>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Puts the effect span on the performance timeline in DevTools.
|
|
29
|
+
*/
|
|
30
|
+
export const addTrackEntry =
|
|
31
|
+
<A, E>(options: AddTrackEntryOptions | ((exit: Exit.Exit<A, E>) => AddTrackEntryOptions)) =>
|
|
32
|
+
<R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
|
|
33
|
+
Effect.gen(function* () {
|
|
34
|
+
const start = performance.now();
|
|
35
|
+
const exit = yield* Effect.exit(effect);
|
|
36
|
+
const resolvedOptions = typeof options === 'function' ? options(exit) : options;
|
|
37
|
+
performance.measure(resolvedOptions.name, {
|
|
38
|
+
start: start,
|
|
39
|
+
detail: {
|
|
40
|
+
...resolvedOptions.detail,
|
|
41
|
+
devtools: resolvedOptions.devtools,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
return yield* exit;
|
|
45
|
+
});
|
|
@@ -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';
|
|
@@ -14,13 +15,13 @@ import {
|
|
|
14
15
|
getAnnotation,
|
|
15
16
|
getDiscriminatedType,
|
|
16
17
|
getDiscriminatingProps,
|
|
17
|
-
|
|
18
|
+
getProperties,
|
|
18
19
|
isArrayType,
|
|
20
|
+
isDiscriminatedUnion,
|
|
19
21
|
isOption,
|
|
20
|
-
isSimpleType,
|
|
21
22
|
visit,
|
|
22
23
|
} from './ast';
|
|
23
|
-
import { type JsonPath, type JsonProp } from './
|
|
24
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
24
25
|
|
|
25
26
|
const ZipCode = Schema.String.pipe(
|
|
26
27
|
Schema.pattern(/^\d{5}$/, {
|
|
@@ -64,10 +65,9 @@ describe('AST', () => {
|
|
|
64
65
|
|
|
65
66
|
const prop = findProperty(TestSchema, 'name' as JsonProp);
|
|
66
67
|
invariant(prop);
|
|
67
|
-
const node = findNode(prop,
|
|
68
|
+
const node = findNode(prop, (node) => node._tag === 'StringKeyword');
|
|
68
69
|
invariant(node);
|
|
69
|
-
|
|
70
|
-
expect(type).to.eq('string');
|
|
70
|
+
expect(node._tag).to.eq('StringKeyword');
|
|
71
71
|
});
|
|
72
72
|
|
|
73
73
|
test('findProperty', ({ expect }) => {
|
|
@@ -91,6 +91,30 @@ describe('AST', () => {
|
|
|
91
91
|
}
|
|
92
92
|
});
|
|
93
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
|
+
|
|
94
118
|
test('findAnnotation', ({ expect }) => {
|
|
95
119
|
const TestSchema = Schema.NonEmptyString.pipe(Schema.pattern(/^\d{5}$/)).annotations({
|
|
96
120
|
title: 'original title',
|
|
@@ -136,7 +160,11 @@ describe('AST', () => {
|
|
|
136
160
|
});
|
|
137
161
|
|
|
138
162
|
const props: string[] = [];
|
|
139
|
-
visit(
|
|
163
|
+
visit(
|
|
164
|
+
TestSchema.ast,
|
|
165
|
+
(_, path) => props.push(path.join('.')),
|
|
166
|
+
(node, path, depth) => depth < 3,
|
|
167
|
+
);
|
|
140
168
|
});
|
|
141
169
|
|
|
142
170
|
test('discriminated unions', ({ expect }) => {
|
|
@@ -151,7 +179,7 @@ describe('AST', () => {
|
|
|
151
179
|
expect(isOption(TestUnionSchema.ast)).to.be.false;
|
|
152
180
|
expect(getDiscriminatingProps(TestUnionSchema.ast)).to.deep.eq(['kind']);
|
|
153
181
|
|
|
154
|
-
const node = findNode(TestUnionSchema.ast,
|
|
182
|
+
const node = findNode(TestUnionSchema.ast, isDiscriminatedUnion);
|
|
155
183
|
expect(node).to.eq(TestUnionSchema.ast);
|
|
156
184
|
}
|
|
157
185
|
|
package/src/ast.ts
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
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';
|
|
7
9
|
|
|
8
10
|
import { invariant } from '@dxos/invariant';
|
|
9
11
|
import { isNonNullable } from '@dxos/util';
|
|
10
12
|
|
|
11
|
-
import { type JsonPath, type JsonProp } from './
|
|
13
|
+
import { type JsonPath, type JsonProp } from './json-path';
|
|
12
14
|
|
|
13
15
|
//
|
|
14
16
|
// Refs
|
|
@@ -17,75 +19,66 @@ import { type JsonPath, type JsonProp } from './jsonPath';
|
|
|
17
19
|
// https://effect-ts.github.io/effect/schema/SchemaAST.ts.html
|
|
18
20
|
//
|
|
19
21
|
|
|
20
|
-
// TODO(wittjosiah): What is a "simple type"?
|
|
21
|
-
export type SimpleType = 'object' | 'string' | 'number' | 'boolean' | 'enum' | 'literal';
|
|
22
|
-
|
|
23
|
-
const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
24
|
-
// NOTE: Arrays are represented as tuples with no elements and a rest part.
|
|
25
|
-
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
22
|
/**
|
|
29
|
-
*
|
|
23
|
+
* Unwraps and collects refinement filters.
|
|
30
24
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
if (SchemaAST.isStringKeyword(node)) {
|
|
44
|
-
return 'string';
|
|
45
|
-
}
|
|
46
|
-
if (SchemaAST.isNumberKeyword(node)) {
|
|
47
|
-
return 'number';
|
|
48
|
-
}
|
|
49
|
-
if (SchemaAST.isBooleanKeyword(node)) {
|
|
50
|
-
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]);
|
|
51
36
|
}
|
|
52
37
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
38
|
+
return { type, refinements };
|
|
39
|
+
};
|
|
56
40
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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);
|
|
60
53
|
};
|
|
61
54
|
|
|
62
|
-
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
|
+
};
|
|
63
59
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
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
|
+
const { type, refinements } = getBaseType(prop);
|
|
67
|
+
// Merge PropertySignature-level annotations (e.g., title, description set via .annotations())
|
|
68
|
+
// onto the unwrapped base type so downstream consumers see them.
|
|
69
|
+
const mergedType =
|
|
70
|
+
prop.annotations && Reflect.ownKeys(prop.annotations).length > 0
|
|
71
|
+
? ({ ...type, annotations: { ...type.annotations, ...prop.annotations } } as SchemaAST.AST)
|
|
72
|
+
: type;
|
|
73
|
+
return {
|
|
74
|
+
type: mergedType,
|
|
75
|
+
refinements,
|
|
76
|
+
name: prop.name,
|
|
77
|
+
isOptional: prop.isOptional,
|
|
78
|
+
isReadonly: prop.isReadonly,
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
};
|
|
89
82
|
|
|
90
83
|
//
|
|
91
84
|
// Branded types
|
|
@@ -109,23 +102,14 @@ export type TestFn = (node: SchemaAST.AST, path: Path, depth: number) => VisitRe
|
|
|
109
102
|
|
|
110
103
|
export type VisitorFn = (node: SchemaAST.AST, path: Path, depth: number) => void;
|
|
111
104
|
|
|
112
|
-
const defaultTest: TestFn = isSimpleType;
|
|
113
|
-
|
|
114
105
|
/**
|
|
115
106
|
* Visit leaf nodes.
|
|
116
107
|
* Refs:
|
|
117
108
|
* - https://github.com/syntax-tree/unist-util-visit?tab=readme-ov-file#visitor
|
|
118
109
|
* - https://github.com/syntax-tree/unist-util-is?tab=readme-ov-file#test
|
|
119
110
|
*/
|
|
120
|
-
export const visit: {
|
|
121
|
-
(node
|
|
122
|
-
(node: SchemaAST.AST, test: TestFn, visitor: VisitorFn): void;
|
|
123
|
-
} = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor?: VisitorFn): void => {
|
|
124
|
-
if (!visitor) {
|
|
125
|
-
visitNode(node, defaultTest, testOrVisitor);
|
|
126
|
-
} else {
|
|
127
|
-
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
128
|
-
}
|
|
111
|
+
export const visit = (node: SchemaAST.AST, testOrVisitor: TestFn | VisitorFn, visitor: VisitorFn): void => {
|
|
112
|
+
visitNode(node, testOrVisitor as TestFn, visitor);
|
|
129
113
|
};
|
|
130
114
|
|
|
131
115
|
const visitNode = (
|
|
@@ -212,6 +196,12 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
212
196
|
return child;
|
|
213
197
|
}
|
|
214
198
|
}
|
|
199
|
+
for (const prop of getIndexSignatures(node)) {
|
|
200
|
+
const child = findNode(prop.type, test);
|
|
201
|
+
if (child) {
|
|
202
|
+
return child;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
215
205
|
}
|
|
216
206
|
|
|
217
207
|
// Tuple.
|
|
@@ -226,12 +216,14 @@ export const findNode = (node: SchemaAST.AST, test: (node: SchemaAST.AST) => boo
|
|
|
226
216
|
|
|
227
217
|
// Branching union (e.g., optional, discriminated unions).
|
|
228
218
|
else if (SchemaAST.isUnion(node)) {
|
|
229
|
-
if (
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
219
|
+
if (isLiteralUnion(node)) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
for (const type of node.types) {
|
|
224
|
+
const child = findNode(type, test);
|
|
225
|
+
if (child) {
|
|
226
|
+
return child;
|
|
235
227
|
}
|
|
236
228
|
}
|
|
237
229
|
}
|
|
@@ -272,10 +264,10 @@ export const findProperty = (
|
|
|
272
264
|
//
|
|
273
265
|
|
|
274
266
|
const defaultAnnotations: Record<string, SchemaAST.Annotated> = {
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
267
|
+
ObjectKeyword: SchemaAST.objectKeyword,
|
|
268
|
+
StringKeyword: SchemaAST.stringKeyword,
|
|
269
|
+
NumberKeyword: SchemaAST.numberKeyword,
|
|
270
|
+
BooleanKeyword: SchemaAST.booleanKeyword,
|
|
279
271
|
};
|
|
280
272
|
|
|
281
273
|
/**
|
|
@@ -287,8 +279,8 @@ export const getAnnotation =
|
|
|
287
279
|
<T>(annotationId: symbol, noDefault = true) =>
|
|
288
280
|
(node: SchemaAST.AST): T | undefined => {
|
|
289
281
|
// Title fallback seems to be the identifier.
|
|
290
|
-
const id = pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
291
|
-
const value = pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
282
|
+
const id = Function.pipe(SchemaAST.getIdentifierAnnotation(node), Option.getOrUndefined);
|
|
283
|
+
const value = Function.pipe(SchemaAST.getAnnotation<T>(annotationId)(node), Option.getOrUndefined);
|
|
292
284
|
if (noDefault && (value === defaultAnnotations[node._tag]?.annotations[annotationId] || value === id)) {
|
|
293
285
|
return undefined;
|
|
294
286
|
}
|
|
@@ -334,10 +326,45 @@ export const isOption = (node: SchemaAST.AST): boolean => {
|
|
|
334
326
|
/**
|
|
335
327
|
* Determines if the node is a union of literal types.
|
|
336
328
|
*/
|
|
337
|
-
export const isLiteralUnion = (node: SchemaAST.AST):
|
|
329
|
+
export const isLiteralUnion = (node: SchemaAST.AST): node is SchemaAST.Union<SchemaAST.Literal> => {
|
|
338
330
|
return SchemaAST.isUnion(node) && node.types.every(SchemaAST.isLiteral);
|
|
339
331
|
};
|
|
340
332
|
|
|
333
|
+
/**
|
|
334
|
+
* Extracts the literal values from a schema that is a union of literals
|
|
335
|
+
* (e.g. `Schema.Literal('a', 'b')` or `Schema.Union(Schema.Literal('a'), Schema.Literal('b'))`).
|
|
336
|
+
* Returns an empty array if the schema is not a literal union.
|
|
337
|
+
*/
|
|
338
|
+
export const getLiteralValues = <S extends Schema.Schema<any, any, any>>(
|
|
339
|
+
schema: S,
|
|
340
|
+
): ReadonlyArray<Schema.Schema.Type<S>> => {
|
|
341
|
+
if (!isLiteralUnion(schema.ast)) {
|
|
342
|
+
return [];
|
|
343
|
+
}
|
|
344
|
+
return schema.ast.types.map((node) => node.literal as Schema.Schema.Type<S>);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Determines if the node is an array type.
|
|
349
|
+
*/
|
|
350
|
+
export const isArrayType = (node: SchemaAST.AST): node is SchemaAST.TupleType => {
|
|
351
|
+
return SchemaAST.isTupleType(node) && node.elements.length === 0 && node.rest.length === 1;
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Get the type of the array elements.
|
|
356
|
+
*/
|
|
357
|
+
export const getArrayElementType = (node: SchemaAST.AST): SchemaAST.AST | undefined => {
|
|
358
|
+
return isArrayType(node) ? node.rest.at(0)?.type : undefined;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Determines if the node is a tuple type.
|
|
363
|
+
*/
|
|
364
|
+
export const isTupleType = (node: SchemaAST.AST): boolean => {
|
|
365
|
+
return SchemaAST.isTupleType(node) && node.elements.length > 0;
|
|
366
|
+
};
|
|
367
|
+
|
|
341
368
|
/**
|
|
342
369
|
* Determines if the node is a discriminated union.
|
|
343
370
|
*/
|
|
@@ -417,6 +444,20 @@ export const getDiscriminatedType = (
|
|
|
417
444
|
return schema.ast;
|
|
418
445
|
};
|
|
419
446
|
|
|
447
|
+
/**
|
|
448
|
+
* Determines if the node is a nested object type.
|
|
449
|
+
*/
|
|
450
|
+
export const isNestedType = (node: SchemaAST.AST): boolean => {
|
|
451
|
+
return (
|
|
452
|
+
SchemaAST.isDeclaration(node) ||
|
|
453
|
+
SchemaAST.isObjectKeyword(node) ||
|
|
454
|
+
SchemaAST.isTypeLiteral(node) ||
|
|
455
|
+
// TODO(wittjosiah): Tuples are actually arrays.
|
|
456
|
+
isTupleType(node) ||
|
|
457
|
+
isDiscriminatedUnion(node)
|
|
458
|
+
);
|
|
459
|
+
};
|
|
460
|
+
|
|
420
461
|
/**
|
|
421
462
|
* Maps AST nodes.
|
|
422
463
|
* The user is responsible for recursively calling {@link mapAst} on the SchemaAST.
|
|
@@ -440,6 +481,7 @@ export const mapAst = (
|
|
|
440
481
|
),
|
|
441
482
|
),
|
|
442
483
|
ast.indexSignatures,
|
|
484
|
+
ast.annotations,
|
|
443
485
|
);
|
|
444
486
|
}
|
|
445
487
|
case 'Union': {
|
|
@@ -464,15 +506,18 @@ export const mapAst = (
|
|
|
464
506
|
}
|
|
465
507
|
};
|
|
466
508
|
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
509
|
+
const getIndexSignatures = (ast: SchemaAST.AST): Array<SchemaAST.IndexSignature> => {
|
|
510
|
+
const annotation = SchemaAST.getSurrogateAnnotation(ast);
|
|
511
|
+
if (Option.isSome(annotation)) {
|
|
512
|
+
return getIndexSignatures(annotation.value);
|
|
513
|
+
}
|
|
514
|
+
switch (ast._tag) {
|
|
515
|
+
case 'TypeLiteral':
|
|
516
|
+
return ast.indexSignatures.slice();
|
|
517
|
+
case 'Suspend':
|
|
518
|
+
return getIndexSignatures(ast.f());
|
|
519
|
+
case 'Refinement':
|
|
520
|
+
return getIndexSignatures(ast.from);
|
|
521
|
+
}
|
|
522
|
+
return [];
|
|
478
523
|
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import { pipe } from 'effect/Function';
|
|
8
|
+
import * as Layer from 'effect/Layer';
|
|
9
|
+
import * as Predicate from 'effect/Predicate';
|
|
10
|
+
import * as Tracer from 'effect/Tracer';
|
|
11
|
+
|
|
12
|
+
const runInTask = Symbol('runInTask');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Traces effect frames using console.createTask so that the proper stack-trace is visible in Chrome Devtools debugger.
|
|
16
|
+
*/
|
|
17
|
+
export const asyncTaskTaggingLayer = () => {
|
|
18
|
+
if (Predicate.hasProperty(console, 'createTask') === false) {
|
|
19
|
+
return Layer.empty;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const makeTracer = Effect.gen(function* () {
|
|
23
|
+
const oldTracer = yield* Effect.tracer;
|
|
24
|
+
return Tracer.make({
|
|
25
|
+
span: (name, ...args) => {
|
|
26
|
+
const span = oldTracer.span(name, ...args);
|
|
27
|
+
const trace = (console as any).createTask(name);
|
|
28
|
+
(span as any)[runInTask] = (f: any) => trace.run(f);
|
|
29
|
+
return span;
|
|
30
|
+
},
|
|
31
|
+
context: (f, fiber) => {
|
|
32
|
+
const maybeParentSpan = Context.getOption(Tracer.ParentSpan)(fiber.currentContext);
|
|
33
|
+
|
|
34
|
+
if (maybeParentSpan._tag === 'None') {
|
|
35
|
+
return oldTracer.context(f, fiber);
|
|
36
|
+
}
|
|
37
|
+
const parentSpan = maybeParentSpan.value;
|
|
38
|
+
if (parentSpan._tag === 'ExternalSpan') {
|
|
39
|
+
return oldTracer.context(f, fiber);
|
|
40
|
+
}
|
|
41
|
+
const span = parentSpan;
|
|
42
|
+
if (runInTask in span && typeof span[runInTask] === 'function') {
|
|
43
|
+
return span[runInTask](() => oldTracer.context(f, fiber));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return oldTracer.context(f, fiber);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
return pipe(makeTracer, Effect.map(Layer.setTracer), Layer.unwrapEffect);
|
|
51
|
+
};
|
package/src/atom-kvs.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Atom } from '@effect-atom/atom-react';
|
|
6
|
+
import * as BrowserKeyValueStore from '@effect/platform-browser/BrowserKeyValueStore';
|
|
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
|
+
};
|