Skip to content

Class: QueryBus

Defined in: src/wirestate-core/queries/query-bus.ts:31

Dispatches named queries to one active handler.

Remarks

Queries are reads: get current user, ask for a label, fetch cached state.

Handlers are stacked by type. The newest handler wins. That lets a subtree temporarily answer a query, then unregister and reveal the older answer.

Example

typescript
import { QueryBus, createContainer } from "@wirestate/core";

const container = createContainer();
const bus = container.get(QueryBus);
bus.register("CURRENT_USER", () => ({ id: "u1" }));

const user = bus.query<{ id: string }>("CURRENT_USER");

Constructors

Constructor

ts
new QueryBus(): QueryBus;

Returns

QueryBus

Methods

clear()

ts
clear(): void;

Defined in: src/wirestate-core/queries/query-bus.ts:243

Internal

Removes all registered query handlers from the bus.

Returns

void


has()

ts
has(type: QueryType): boolean;

Defined in: src/wirestate-core/queries/query-bus.ts:232

Checks if at least one handler is registered for the given query type.

Parameters

ParameterTypeDescription
typeQueryTypeUnique query identifier.

Returns

boolean

true if a handler is available, false otherwise.


query()

ts
query<R, D, T>(type: T, data?: D): R;

Defined in: src/wirestate-core/queries/query-bus.ts:134

Dispatches a query and returns the handler result as-is.

Type Parameters

Type ParameterDefault typeDescription
RunknownType of the expected query result.
DunknownType of the data (payload) passed to the query.
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTUnique query identifier.
data?DOptional input data for the handler.

Returns

R

The result of the query execution.

Remarks

If a handler returns a Promise, this method returns that Promise. Use queryAsync when the caller should always receive a Promise.

Throws

WirestateError If no handler is registered for the given type.

Example

typescript
const user: User = queryBus.query<User, string>("FIND_USER", "user-id-123");

queryAsync()

ts
queryAsync<R, D, T>(type: T, data?: D): Promise<R>;

Defined in: src/wirestate-core/queries/query-bus.ts:164

Dispatches a query and Promise-wraps the result.

Type Parameters

Type ParameterDefault typeDescription
RunknownType of the expected query result.
DunknownType of the data (payload) passed to the query.
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTUnique query identifier.
data?DOptional input data for the handler.

Returns

Promise<R>

A Promise resolving to the query result.

Remarks

Sync values are wrapped. Async values are passed through.

Throws

WirestateError If no handler is registered for the given type.


queryOptional()

ts
queryOptional<R, D, T>(type: T, data?: D): Optional<R>;

Defined in: src/wirestate-core/queries/query-bus.ts:192

Dispatches a query if a handler exists.

Type Parameters

Type ParameterDefault typeDescription
RunknownType of the expected query result.
DunknownType of the data (payload) passed to the query.
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTUnique query identifier.
data?DOptional input data for the handler.

Returns

Optional<R>

The synchronous query result, or null if no handler is found.

Remarks

Returns the handler result as-is. Use queryOptionalAsync when the caller should always receive a Promise.


queryOptionalAsync()

ts
queryOptionalAsync<R, D, T>(type: T, data?: D): Promise<Optional<R>>;

Defined in: src/wirestate-core/queries/query-bus.ts:213

Dispatches an optional query and Promise-wraps the result.

Type Parameters

Type ParameterDefault typeDescription
RunknownType of the expected query result.
DunknownType of the data (payload) passed to the query.
T extends QueryTypeQueryTypeType of the query identifier.

Parameters

ParameterTypeDescription
typeTUnique query identifier.
data?DOptional input data for the handler.

Returns

Promise<Optional<R>>

A Promise resolving to the query result, or null if no handler is found.


register()

ts
register<D, R>(type: QueryType, handler: QueryHandler<D, R>): QueryUnregister;

Defined in: src/wirestate-core/queries/query-bus.ts:56

Registers a query handler.

Type Parameters

Type ParameterDefault typeDescription
DunknownType of the query input data.
RunknownType of the query result.

Parameters

ParameterTypeDescription
typeQueryTypeQuery token.
handlerQueryHandler<D, R>Query handler.

Returns

QueryUnregister

Function that unregisters this handler.

Remarks

Multiple handlers for one type form a stack. The newest handler answers.

Example

typescript
const unregister: QueryUnregister = queryBus.register("GET_NOW", () => Date.now());

unregister()

ts
unregister<D, R>(type: QueryType, handler: QueryHandler<D, R>): void;

Defined in: src/wirestate-core/queries/query-bus.ts:87

Removes a previously registered query handler.

Type Parameters

Type ParameterDefault typeDescription
DunknownType of the query input data.
RunknownType of the query result.

Parameters

ParameterTypeDescription
typeQueryTypeUnique query identifier.
handlerQueryHandler<D, R>The handler function instance to remove.

Returns

void

Remarks

If the handler was not registered for the given type, this operation does nothing.