Skip to content

readFrom()

Function

readFrom<T>(target: Query<T> | BehaviorSubject<T>): Read<T>

Create a new Read containing the whole state of the Store using a Query or reading the state of a BehaviorSubject:

ts
const state$: Read\<StoreState\> = readFrom(query);
const subjectState$: Read\<string\> = readFrom(behaviorSubject);

Source: akita-read/src/lib/read-from.ts:21

Type parameters

ParameterDescription
Tstate used by the target

Parameters

ParameterTypeDescription
targetQuery<T> | BehaviorSubject<T>Query or BehaviorSubject to select from

Returns

Read<T>

Returns a new Read that emits the state of the Query or BehaviorSubject

readFrom<T, K>(query: Query<T>, key: K): Read<T[K]>

Create a new Read by selecting a key from the Store using a Query:

ts
const date$: Read\<Date\> = readFrom(query, 'date');

Source: akita-read/src/lib/read-from.ts:36

Type parameters

ParameterDescription
Tstate used by the query
K extends string | number | symbolkey of the state

Parameters

ParameterTypeDescription
queryQuery<T>Query to select from
keyKkey to select from the state

Returns

Read<T[K]>

Returns a new Read that emits the value of the selected field of the state

readFrom<T, P>(query: Query<T>, projection: Function): Read<P>

Create a new Read using a projection from the Store using a Query:

ts
const date$: Read\<Date\> = readFrom(query, state => state.date);

⚠️ Avoid doing heavy calculations inside the projection. ⚠️

> The result of the projection is automatically piped through a distinctUntilChanged > operator to avoid unnecessary recalculations/emissions. > ```ts

const sum$: Read<number> = readFrom(query, state => state.prices.reduce((sum, curr) => sum + curr, 0));

\> By doing this calculation inside the projection, this calculation is always redone when the state changes,
\> even though the prices did not change at all.

Source: akita-read/src/lib/read-from.ts:61

Type parameters

ParameterDescription
Tstate used by the query
Preturn type of the projection

Parameters

ParameterTypeDescription
queryQuery<T>Query to select from
projection(state: T) => Pprojection to pick from the state

Returns

Read<P>

Returns a new Read that emits the value returned by the projection

readFrom<T, K>(query: Query<T>, keys: readonly K[]): Read<Pick<T, K>>

Create a new Read by selecting a list of keys from the Store using a Query:

ts
const data$: Read\<{date: Date, count: number}\> = readFrom(query, ['date', 'count']);

Source: akita-read/src/lib/read-from.ts:76

Type parameters

ParameterDescription
Tstate used by the query
K extends string | number | symbolkey of the state

Parameters

ParameterTypeDescription
queryQuery<T>Query to select from
keysreadonly K[]array of keys to select from the state

Returns

Read<Pick<T, K>>

Returns a new Read that emits an object containing the values selected by the keys