Skip to content
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
4 changes: 4 additions & 0 deletions packages/signia/src/Computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export interface ComputedOptions<Value, Diff> {
* @returns
*/
isEqual?: (a: any, b: any) => boolean

isPush?: boolean
}

/**
Expand Down Expand Up @@ -140,6 +142,7 @@ export class _Computed<Value, Diff = unknown> implements Computed<Value, Diff> {
}

historyBuffer?: HistoryBuffer<Diff>
isPush: boolean

// The last-computed value of this signal.
private state: Value = UNINITIALIZED as unknown as Value
Expand Down Expand Up @@ -167,6 +170,7 @@ export class _Computed<Value, Diff = unknown> implements Computed<Value, Diff> {
}
this.computeDiff = options?.computeDiff
this.isEqual = options?.isEqual ?? null
this.isPush = options?.isPush ?? false
}

__unsafe__getWithoutCapture(): Value {
Expand Down
21 changes: 21 additions & 0 deletions packages/signia/src/__tests__/computed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,3 +587,24 @@ describe(getComputedInstance, () => {
expect(bInst).toBeInstanceOf(_Computed)
})
})

describe('push computeds', () => {
it('prevent traversal', () => {
const user = atom('', { id: 1, name: 'steve' })
const name = computed('', () => user.value.name, { isPush: true })
const nameLength = computed('', () => name.value.length)
let numNameLengthReactions = 0
const r = reactor('', () => {
numNameLengthReactions++
nameLength.value
})
r.start()
expect(numNameLengthReactions).toBe(1)
const lastTraversed = r.scheduler.lastTraversedEpoch

user.set({ id: 2, name: 'steve' })

expect(numNameLengthReactions).toBe(1)
expect(r.scheduler.lastTraversedEpoch).toBe(lastTraversed)
})
})
Loading