Skip to content

Commit 32f2212

Browse files
authored
Remove circular imports (#605)
* Enable import-x/no-cycle ESLint rule This will prevent circular dependencies in the codebase which cause issues like #567. * Fix circular import of `and` * Fix circular import of `followRef` * Fix circular import in compiler * Fix linting issues * Add changeset for circular import fix
1 parent 7bbe9bd commit 32f2212

File tree

13 files changed

+142
-20
lines changed

13 files changed

+142
-20
lines changed

.changeset/wise-snakes-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
Remove circular imports to fix compatibility with Metro bundler

eslint.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export default [
1919
stylistic: stylisticPlugin,
2020
prettier: prettierPlugin,
2121
},
22+
settings: {
23+
// import-x/* settings required for import/no-cycle.
24+
"import-x/resolver": { typescript: true },
25+
"import-x/extensions": [".ts", ".tsx", ".js", ".jsx", ".cjs", ".mjs"],
26+
},
2227
rules: {
2328
"prettier/prettier": `error`,
2429
"stylistic/quotes": [`error`, `backtick`],
@@ -42,6 +47,7 @@ export default [
4247
leadingUnderscore: `allow`,
4348
},
4449
],
50+
"import/no-cycle": `error`,
4551
},
4652
},
4753
]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@vitejs/plugin-react": "^4.7.0",
3737
"eslint": "^9.36.0",
3838
"eslint-config-prettier": "^10.1.8",
39+
"eslint-import-resolver-typescript": "^4.4.4",
3940
"eslint-plugin-prettier": "^5.5.4",
4041
"eslint-plugin-react": "^7.37.5",
4142
"husky": "^9.1.7",

packages/db-ivm/src/operators/filterBy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { map } from "./map.js"
22
import { innerJoin } from "./join.js"
33
import { consolidate } from "./consolidate.js"
4-
import type { KeyValue } from "../types.js"
5-
import type { IStreamBuilder, PipedOperator } from "../types"
4+
import type { IStreamBuilder, KeyValue, PipedOperator } from "../types.js"
65

76
/**
87
* Filters the elements of a keyed stream, by keys of another stream.

packages/db-ivm/src/operators/orderBy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { topKWithFractionalIndex } from "./topKWithFractionalIndex.js"
33
import { map } from "./map.js"
44
import { innerJoin } from "./join.js"
55
import { consolidate } from "./consolidate.js"
6-
import type { KeyValue } from "../types.js"
7-
import type { IStreamBuilder } from "../types"
6+
import type { IStreamBuilder, KeyValue } from "../types.js"
87

98
export interface OrderByOptions<Ve> {
109
comparator?: (a: Ve, b: Ve) => number

packages/db-ivm/src/operators/topK.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { MultiSet } from "../multiset.js"
22
import { reduce } from "./reduce.js"
3-
import type { IStreamBuilder, PipedOperator } from "../types"
4-
import type { KeyValue } from "../types.js"
3+
import type { IStreamBuilder, KeyValue, PipedOperator } from "../types"
54

65
interface TopKOptions {
76
limit?: number

packages/db/src/collection/subscription.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ensureIndexForExpression } from "../indexes/auto-index.js"
2-
import { and } from "../query/index.js"
2+
import { and } from "../query/builder/functions.js"
33
import {
44
createFilterFunctionFromExpression,
55
createFilteredCallback,

packages/db/src/query/compiler/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export function compileQuery(
127127
callbacks,
128128
lazyCollections,
129129
optimizableOrderByCollections,
130-
rawQuery
130+
rawQuery,
131+
compileQuery
131132
)
132133
}
133134

@@ -512,3 +513,5 @@ export function followRef(
512513
}
513514
}
514515
}
516+
517+
export type CompileQueryFn = typeof compileQuery

packages/db/src/query/compiler/joins.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import {
1717
UnsupportedJoinTypeError,
1818
} from "../../errors.js"
1919
import { ensureIndexForField } from "../../indexes/auto-index.js"
20-
import { PropRef } from "../ir.js"
20+
import { PropRef, followRef } from "../ir.js"
2121
import { inArray } from "../builder/functions.js"
2222
import { compileExpression } from "./evaluators.js"
23-
import { compileQuery, followRef } from "./index.js"
23+
import type { CompileQueryFn } from "./index.js"
2424
import type { OrderByOptimizationInfo } from "./order-by.js"
2525
import type {
2626
BasicExpression,
@@ -62,7 +62,8 @@ export function processJoins(
6262
callbacks: Record<string, LazyCollectionCallbacks>,
6363
lazyCollections: Set<string>,
6464
optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,
65-
rawQuery: QueryIR
65+
rawQuery: QueryIR,
66+
onCompileSubquery: CompileQueryFn
6667
): NamespacedAndKeyedStream {
6768
let resultPipeline = pipeline
6869

@@ -81,7 +82,8 @@ export function processJoins(
8182
callbacks,
8283
lazyCollections,
8384
optimizableOrderByCollections,
84-
rawQuery
85+
rawQuery,
86+
onCompileSubquery
8587
)
8688
}
8789

@@ -105,7 +107,8 @@ function processJoin(
105107
callbacks: Record<string, LazyCollectionCallbacks>,
106108
lazyCollections: Set<string>,
107109
optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,
108-
rawQuery: QueryIR
110+
rawQuery: QueryIR,
111+
onCompileSubquery: CompileQueryFn
109112
): NamespacedAndKeyedStream {
110113
// Get the joined table alias and input stream
111114
const {
@@ -121,7 +124,8 @@ function processJoin(
121124
lazyCollections,
122125
optimizableOrderByCollections,
123126
cache,
124-
queryMapping
127+
queryMapping,
128+
onCompileSubquery
125129
)
126130

127131
// Add the joined table to the tables map
@@ -392,7 +396,8 @@ function processJoinSource(
392396
lazyCollections: Set<string>,
393397
optimizableOrderByCollections: Record<string, OrderByOptimizationInfo>,
394398
cache: QueryCache,
395-
queryMapping: QueryMapping
399+
queryMapping: QueryMapping,
400+
onCompileSubquery: CompileQueryFn
396401
): { alias: string; input: KeyedStream; collectionId: string } {
397402
switch (from.type) {
398403
case `collectionRef`: {
@@ -407,7 +412,7 @@ function processJoinSource(
407412
const originalQuery = queryMapping.get(from.query) || from.query
408413

409414
// Recursively compile the sub-query with cache
410-
const subQueryResult = compileQuery(
415+
const subQueryResult = onCompileSubquery(
411416
originalQuery,
412417
allInputs,
413418
collections,

packages/db/src/query/compiler/order-by.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { orderByWithFractionalIndex } from "@tanstack/db-ivm"
22
import { defaultComparator, makeComparator } from "../../utils/comparison.js"
3-
import { PropRef } from "../ir.js"
3+
import { PropRef, followRef } from "../ir.js"
44
import { ensureIndexForField } from "../../indexes/auto-index.js"
55
import { findIndexForField } from "../../utils/index-optimization.js"
66
import { compileExpression } from "./evaluators.js"
77
import { replaceAggregatesByRefs } from "./group-by.js"
8-
import { followRef } from "./index.js"
98
import type { CompiledSingleRowExpression } from "./evaluators.js"
109
import type { OrderByClause, QueryIR, Select } from "../ir.js"
1110
import type { NamespacedAndKeyedStream, NamespacedRow } from "../../types.js"

0 commit comments

Comments
 (0)