Skip to content

@epikodelabs/streamix


@epikodelabs/streamix / src/public-api / lens

Function: lens() ​

Call Signature ​

lens<T, P>(source, path): Lens<LensValue<T, P>>

Defined in: projects/libraries/streamix/src/lib/factories/lens.ts:102

Creates a focused reactive atom for a property or nested path.

Reads are synchronous and dependency-tracked. The focused atom emits its current value and subsequent changes, comparing selections with Object.is. Unrelated source updates do not emit a new selected value. Missing paths read as undefined; an uninitialized source remains unavailable.

A writable source produces a writable lens. set()/next() write through to the source using immutable path copying. A read-only source produces a read-only atom. Disposing the lens never disposes its source.

Type Parameters ​

T ​

T extends object

P ​

P extends LensPath

Parameters ​

source ​

Writable<T>

path ​

P

Returns ​

Lens<LensValue<T, P>>

Example ​

ts
const profile = atom({ name: 'Alex', address: { city: 'Kyiv' } });
const name = lens(profile, 'name');
const city = lens(profile, ['address', 'city']);
console.log(city.value); // 'Kyiv'
city.set('Lviv');
console.log(profile.value.address.city); // 'Lviv'

Call Signature ​

lens<T, P>(source, path): Lens<LensValue<T, P>>

Defined in: projects/libraries/streamix/src/lib/factories/lens.ts:106

Composing writable lenses keeps the write-through API.

Type Parameters ​

T ​

T extends object

P ​

P extends LensPath

Parameters ​

source ​

Lens<T>

path ​

P

Returns ​

Lens<LensValue<T, P>>

Call Signature ​

lens<T, P>(source, path): Readable<LensValue<T, P>>

Defined in: projects/libraries/streamix/src/lib/factories/lens.ts:109

Creates a focused reactive atom for a property or nested path.

Reads are synchronous and dependency-tracked. The focused atom emits its current value and subsequent changes, comparing selections with Object.is. Unrelated source updates do not emit a new selected value. Missing paths read as undefined; an uninitialized source remains unavailable.

A writable source produces a writable lens. set()/next() write through to the source using immutable path copying. A read-only source produces a read-only atom. Disposing the lens never disposes its source.

Type Parameters ​

T ​

T extends object

P ​

P extends LensPath

Parameters ​

source ​

Readable<T>

path ​

P

Returns ​

Readable<LensValue<T, P>>

Example ​

ts
const profile = atom({ name: 'Alex', address: { city: 'Kyiv' } });
const name = lens(profile, 'name');
const city = lens(profile, ['address', 'city']);
console.log(city.value); // 'Kyiv'
city.set('Lviv');
console.log(profile.value.address.city); // 'Lviv'

Released under the MIT License.