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 2 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
14 changes: 14 additions & 0 deletions packages/lens/src/parseAtoms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,18 @@ test('should parse deep structures', () => {
;`👍` //?
})

test('circular references', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test has no "circular references" 🧐
Please, write test for the caching which you added. For example, you can use for "a" and "b" the same atom with an object and check that a snapshot contains equal references in "a" and "b".

const a = atom([1])
const aRec = { a }
const b = atom([2])
const bRec = { b }
const c = atom({ a: aRec.a, b: bRec.b })
const ctx = createTestCtx()

const snapshot = parseAtoms(ctx, c)
a(ctx, [10])
assert.is.not(snapshot, parseAtoms(ctx, c))
assert.is.not(snapshot.a, parseAtoms(ctx, c).a)
})

test.run()
48 changes: 21 additions & 27 deletions packages/lens/src/parseAtoms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Atom, Ctx, isAtom, Rec } from '@reatom/core'
import { atom, Atom, Ctx, isAtom, Rec } from '@reatom/core'
import { isRec } from '@reatom/utils'

export type ParseAtoms<T> = T extends Atom<infer T>
Expand All @@ -13,37 +13,31 @@ export type ParseAtoms<T> = T extends Atom<infer T>
}
: T

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

const unwrap = <T>(ctx: Ctx, 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)) {
res = {}
for (const k in value) res[k] = unwrap(ctx, value[k], cache)
} else if (Array.isArray(value)) {
res = []
for (const v of value) res.push(unwrap(ctx, v, cache))
} else if (value instanceof Map) {
res = new Map()
for (const [k, v] of value) res.set(k, unwrap(ctx, v, cache))
} else if (value instanceof Set) {
res = new Set()
for (const v of value) res.add(unwrap(ctx, v, cache))
}

return value as any
cache.set(value, res)
return res
}
Loading