Skip to content

Commit f472e6f

Browse files
authored
chore: Isolate link types from runtime imports (#2285)
1 parent 03197d2 commit f472e6f

File tree

15 files changed

+439
-376
lines changed

15 files changed

+439
-376
lines changed

packages/runner/src/builder/json-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { isRecord } from "@commontools/utils/types";
22
import { type LegacyAlias } from "../sigil-types.ts";
3-
import { isLegacyAlias, isLink } from "../link-utils.ts";
43
import {
54
isRecipe,
65
type JSONSchema,
@@ -17,7 +16,12 @@ import {
1716
import { getTopFrame } from "./recipe.ts";
1817
import { deepEqual } from "../path-utils.ts";
1918
import { Runtime } from "../runtime.ts";
20-
import { parseLink, sanitizeSchemaForLinks } from "../link-utils.ts";
19+
import {
20+
isCellLink,
21+
isLegacyAlias,
22+
parseLink,
23+
sanitizeSchemaForLinks,
24+
} from "../link-utils.ts";
2125
import {
2226
getCellOrThrow,
2327
isCellResultForDereferencing,
@@ -132,7 +136,7 @@ export function createJsonSchema(
132136
const seen = new Map<string, JSONSchemaMutable>();
133137

134138
function analyzeType(value: any): JSONSchema {
135-
if (isLink(value)) {
139+
if (isCellLink(value)) {
136140
const link = parseLink(value);
137141
const linkAsStr = JSON.stringify(link);
138142
if (seen.has(linkAsStr)) {

packages/runner/src/cell.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ import {
6161
type SigilWriteRedirectLink,
6262
type URI,
6363
} from "./sigil-types.ts";
64-
import { areLinksSame, isLink } from "./link-utils.ts";
6564
import type { Runtime } from "./runtime.ts";
6665
import {
66+
areLinksSame,
6767
createSigilLinkFromParsedLink,
6868
findAndInlineDataURILinks,
69+
isCellLink,
6970
type NormalizedFullLink,
7071
type NormalizedLink,
7172
} from "./link-utils.ts";
@@ -1400,7 +1401,7 @@ function recursivelyAddIDIfNeeded<T>(
14001401
if (!frame) return value;
14011402

14021403
// Not a record, no need to add IDs. Already a link, no need to add IDs.
1403-
if (!isRecord(value) || isLink(value)) return value;
1404+
if (!isRecord(value) || isCellLink(value)) return value;
14041405

14051406
// Already seen, return previously annotated result.
14061407
if (seen.has(value)) return seen.get(value) as T;
@@ -1415,7 +1416,7 @@ function recursivelyAddIDIfNeeded<T>(
14151416
const value = recursivelyAddIDIfNeeded(v, frame, seen);
14161417
// For objects on arrays only: Add ID if not already present.
14171418
if (
1418-
isObject(value) && !isLink(value) && !(ID in value)
1419+
isObject(value) && !isCellLink(value) && !(ID in value)
14191420
) {
14201421
return { [ID]: frame.generatedIdCounter++, ...value };
14211422
} else {

packages/runner/src/data-updating.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
areNormalizedLinksSame,
1111
createSigilLinkFromParsedLink,
1212
findAndInlineDataURILinks,
13-
isAnyCellLink,
14-
isLink,
13+
isCellLink,
14+
isPrimitiveCellLink,
1515
isWriteRedirectLink,
1616
type NormalizedFullLink,
1717
parseLink,
@@ -156,7 +156,7 @@ export function normalizeAndDiff(
156156
if (Array.isArray(parent)) {
157157
const base = runtime.getCellFromLink(link, undefined, tx);
158158
for (const v of parent) {
159-
if (isLink(v)) {
159+
if (isCellLink(v)) {
160160
const sibling = parseLink(v, base);
161161
const siblingId = tx.readValueOrThrow({
162162
...sibling,
@@ -224,7 +224,9 @@ export function normalizeAndDiff(
224224

225225
// Check for links that are data: URIs and inline them, by calling
226226
// normalizeAndDiff on the contents of the link.
227-
if (isLink(newValue) && parseLink(newValue, link).id?.startsWith("data:")) {
227+
if (
228+
isCellLink(newValue) && parseLink(newValue, link).id?.startsWith("data:")
229+
) {
228230
return normalizeAndDiff(
229231
runtime,
230232
tx,
@@ -296,7 +298,7 @@ export function normalizeAndDiff(
296298
);
297299
}
298300

299-
if (isAnyCellLink(newValue)) {
301+
if (isPrimitiveCellLink(newValue)) {
300302
diffLogger.debug(
301303
"diff",
302304
() =>
@@ -335,7 +337,7 @@ export function normalizeAndDiff(
335337
);
336338
}
337339
if (
338-
isAnyCellLink(currentValue) &&
340+
isPrimitiveCellLink(currentValue) &&
339341
areLinksSame(newValue, currentValue, link)
340342
) {
341343
diffLogger.debug(
@@ -487,7 +489,7 @@ export function normalizeAndDiff(
487489
);
488490
// If the current value is not a (regular) object, set it to an empty object
489491
// Note that the alias case is handled above
490-
if (!isRecord(currentValue) || isAnyCellLink(currentValue)) {
492+
if (!isRecord(currentValue) || isPrimitiveCellLink(currentValue)) {
491493
diffLogger.debug(
492494
"diff",
493495
() =>
@@ -610,7 +612,7 @@ export function addCommonIDfromObjectID(
610612
obj[ID_FIELD] = fieldName;
611613
}
612614

613-
if (isRecord(obj) && !isCell(obj) && !isAnyCellLink(obj)) {
615+
if (isRecord(obj) && !isCell(obj) && !isPrimitiveCellLink(obj)) {
614616
Object.values(obj).forEach((v) => traverse(v));
615617
}
616618
}

packages/runner/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type {
1010
export * from "./interface.ts";
1111
export { raw } from "./module.ts";
1212
export type { Cell, Stream } from "./cell.ts";
13-
export type { NormalizedLink } from "./link-utils.ts";
13+
export type { NormalizedLink } from "./link-types.ts";
1414
export type { SigilLink, URI } from "./sigil-types.ts";
1515
export { createRef, type EntityId, getEntityId } from "./create-ref.ts";
1616
export type { CellResult as QueryResult } from "./query-result-proxy.ts";
@@ -47,8 +47,7 @@ export { addCommonIDfromObjectID } from "./data-updating.ts";
4747
export { resolveLink } from "./link-resolution.ts";
4848
export {
4949
areLinksSame,
50-
isLegacyCellLink,
51-
isLink,
50+
isCellLink as isLink,
5251
isWriteRedirectLink,
5352
parseLink,
5453
parseLinkOrThrow,

0 commit comments

Comments
 (0)