Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lens): support circular references in parseAtoms #849

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/lens/src/parseAtoms.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTestCtx } from '@reatom/testing'
import { suite } from 'uvu'
import * as assert from 'uvu/assert'
import { atom } from '@reatom/core'
import { Atom, atom } from '@reatom/core'
import { parseAtoms } from './parseAtoms'

const test = suite('parseAtoms')
Expand Down Expand Up @@ -151,4 +151,24 @@ test('should parse deep structures', () => {
;`👍` //?
})

test('circular structures', () => {
const ctx = createTestCtx()

const dummy = atom(null)

const a = atom((ctx):{b: Atom<any>} => {
ctx.spy(dummy) // add a dependency to prevent it from restarting on every call
return {b}
})

const b = atom((ctx) => {
ctx.spy(dummy)
return {a}
})

const snapshot = parseAtoms(ctx, a)
assert.equal(snapshot, snapshot.b.a)
assert.equal(snapshot.b, snapshot.b.a.b)
})

test.run()
46 changes: 22 additions & 24 deletions packages/lens/src/parseAtoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,35 @@ export type ParseAtoms<T> = T extends Atom<infer T>
}
: T

export const parseAtoms = <Value>(
export const parseAtoms = <T>(ctx: Ctx, value: T) =>
unwrap(ctx, value, new WeakMap())

const unwrap = <T>(
ctx: Ctx,
value: Value,
): ParseAtoms<Value> => {
value: T,
cache: WeakMap<any, any>,
): ParseAtoms<T> => {
while (isAtom(value)) value = ctx.spy ? ctx.spy(value) : ctx.get(value)

if (typeof value !== 'object' || value === null) return value as any

if (isRec(value)) {
const res = {} as Rec
for (const k in value) res[k] = parseAtoms(ctx, value[k])
return res as any
}
if (cache.has(value)) return cache.get(value)

if (Array.isArray(value)) {
const res = []
for (const v of value) res.push(parseAtoms(ctx, v))
return res as any
}
let res: any = value

if (value instanceof Map) {
const res = new Map()
for (const [k, v] of value) res.set(k, parseAtoms(ctx, v))
return res as any
}

if (value instanceof Set) {
const res = new Set()
for (const v of value) res.add(parseAtoms(ctx, v))
return res as any
if (isRec(value)) {
cache.set(value, (res = {}))
for (const k in value) res[k] = unwrap(ctx, value[k], cache)
} else if (Array.isArray(value)) {
cache.set(value, (res = []))
for (const v of value) res.push(unwrap(ctx, v, cache))
} else if (value instanceof Map) {
cache.set(value, (res = new Map()))
for (const [k, v] of value) res.set(k, unwrap(ctx, v, cache))
} else if (value instanceof Set) {
cache.set(value, (res = new Set()))
for (const v of value) res.add(unwrap(ctx, v, cache))
}

return value as any
return res
}
Loading