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
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
new QueryBus(): QueryBus;Returns
QueryBus
Methods
clear()
clear(): void;Defined in: src/wirestate-core/queries/query-bus.ts:243
Internal
Removes all registered query handlers from the bus.
Returns
void
has()
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
| Parameter | Type | Description |
|---|---|---|
type | QueryType | Unique query identifier. |
Returns
boolean
true if a handler is available, false otherwise.
query()
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 Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the expected query result. |
D | unknown | Type of the data (payload) passed to the query. |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Unique query identifier. |
data? | D | Optional 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
const user: User = queryBus.query<User, string>("FIND_USER", "user-id-123");queryAsync()
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 Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the expected query result. |
D | unknown | Type of the data (payload) passed to the query. |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Unique query identifier. |
data? | D | Optional 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()
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 Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the expected query result. |
D | unknown | Type of the data (payload) passed to the query. |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Unique query identifier. |
data? | D | Optional 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()
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 Parameter | Default type | Description |
|---|---|---|
R | unknown | Type of the expected query result. |
D | unknown | Type of the data (payload) passed to the query. |
T extends QueryType | QueryType | Type of the query identifier. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | T | Unique query identifier. |
data? | D | Optional input data for the handler. |
Returns
Promise<Optional<R>>
A Promise resolving to the query result, or null if no handler is found.
register()
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 Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the query input data. |
R | unknown | Type of the query result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | QueryType | Query token. |
handler | QueryHandler<D, R> | Query handler. |
Returns
Function that unregisters this handler.
Remarks
Multiple handlers for one type form a stack. The newest handler answers.
Example
const unregister: QueryUnregister = queryBus.register("GET_NOW", () => Date.now());unregister()
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 Parameter | Default type | Description |
|---|---|---|
D | unknown | Type of the query input data. |
R | unknown | Type of the query result. |
Parameters
| Parameter | Type | Description |
|---|---|---|
type | QueryType | Unique query identifier. |
handler | QueryHandler<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.