@nice-code/action 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -3310,7 +3310,7 @@ var err_nice_action = err_nice.createChildDomain({
3310
3310
  message: ({ context, clientStringId }) => `No runtime registered${context?.domain ? ` on domain "${context.domain}"` : ""} for client [${clientStringId}].`
3311
3311
  }),
3312
3312
  ["no_client_runtimes_registered" /* no_client_runtimes_registered */]: err({
3313
- message: ({ context }) => `No runtimes registered${context?.domain ? ` on domain "${context.domain}"` : ""}. Register at least one runtime via addActionRuntime() before executing actions.`
3313
+ message: ({ context }) => `No runtimes registered${context?.domain ? ` on domain "${context.domain}"` : ""}. Add handlers to a runtime via runtime.addHandlers([handler]) before executing actions.`
3314
3314
  }),
3315
3315
  ["action_input_validation_failed" /* action_input_validation_failed */]: err({
3316
3316
  message: ({ domain, actionId, validationMessage }) => `Input validation failed for action "${actionId}" in domain "${domain}":
@@ -3618,6 +3618,7 @@ class ActionRuntime {
3618
3618
  actionRouter;
3619
3619
  _pendingRunningActions = new Map;
3620
3620
  _registeredExternalHandlers = [];
3621
+ _applied = false;
3621
3622
  static getDefault() {
3622
3623
  return getDefaultActionRuntime();
3623
3624
  }
@@ -3639,6 +3640,7 @@ class ActionRuntime {
3639
3640
  throw new Error(`Can't update Runtime Coordinate with different "envId" properties (static environment id). "${this.coordinate.envId}" (current) !== ${newCoordinate.envId}`);
3640
3641
  }
3641
3642
  this._coordinate = newCoordinate;
3643
+ this.apply();
3642
3644
  }
3643
3645
  registerRunningAction(ra) {
3644
3646
  this._pendingRunningActions.set(ra.cuid, ra);
@@ -3741,6 +3743,9 @@ class ActionRuntime {
3741
3743
  }
3742
3744
  const handlerRouter = handler.getActionRouter();
3743
3745
  this.actionRouter.addDomainsFromOther(handlerRouter);
3746
+ if (this._applied) {
3747
+ this.apply();
3748
+ }
3744
3749
  for (const key of handlerRouter.getRegisteredKeys()) {
3745
3750
  const alreadyRegistered = this.actionRouter.getForKey(key).some((h2) => h2.cuid === handler.cuid);
3746
3751
  if (!alreadyRegistered) {
@@ -3750,6 +3755,19 @@ class ActionRuntime {
3750
3755
  }
3751
3756
  return this;
3752
3757
  }
3758
+ applyRuntimeForDomain(domain) {
3759
+ const rootDomain = domain.rootDomain;
3760
+ if (!rootDomain._hasRuntime(this)) {
3761
+ rootDomain._registerRuntime(this);
3762
+ }
3763
+ }
3764
+ apply() {
3765
+ this._applied = true;
3766
+ for (const domain of this.actionRouter.getDomains()) {
3767
+ this.applyRuntimeForDomain(domain);
3768
+ }
3769
+ return this;
3770
+ }
3753
3771
  getReturnHandlerForOrigin(originClient) {
3754
3772
  if (originClient.envId === UNSET_RUNTIME_ENV_ID)
3755
3773
  return;
@@ -3949,6 +3967,9 @@ class ActionDomain extends ActionDomainBase {
3949
3967
  get rootDomain() {
3950
3968
  return this._rootDomain;
3951
3969
  }
3970
+ _registerRuntime(runtime2) {
3971
+ this._rootDomain._registerRuntime(runtime2);
3972
+ }
3952
3973
  createChildDomain(subDomainDef) {
3953
3974
  if (this.allDomains.includes(subDomainDef.domain)) {
3954
3975
  throw err_nice_action.fromId("domain_already_exists_in_hierarchy" /* domain_already_exists_in_hierarchy */, {
@@ -4166,6 +4187,9 @@ class ActionRuntimeManager {
4166
4187
  getBestRuntime(clientSpecifier) {
4167
4188
  return clientSpecifier != null ? this.getBestRuntimeForSpecifier(clientSpecifier) : this.getPreferredRuntime();
4168
4189
  }
4190
+ hasRuntime(runtime2) {
4191
+ return this._runtimes.has(runtime2.coordinate.stringId);
4192
+ }
4169
4193
  getBestRuntimeOrThrow(specifier) {
4170
4194
  const runtime2 = this.getBestRuntime(specifier);
4171
4195
  if (!runtime2) {
@@ -4211,9 +4235,11 @@ class ActionRootDomain extends ActionDomainBase {
4211
4235
  actionSchema: subDomainDef.actions
4212
4236
  }, { rootDomain: this });
4213
4237
  }
4214
- addActionRuntime(runtime2) {
4238
+ _registerRuntime(runtime2) {
4215
4239
  this._actionRuntimeManager.registerRuntime(runtime2);
4216
- return this;
4240
+ }
4241
+ _hasRuntime(runtime2) {
4242
+ return this._actionRuntimeManager.hasRuntime(runtime2);
4217
4243
  }
4218
4244
  getRuntime(clientSpecifier) {
4219
4245
  return this._actionRuntimeManager.getBestRuntimeForSpecifier(clientSpecifier);
@@ -6924,7 +6950,11 @@ class ConnectionTransportManager {
6924
6950
  if (cacheKey != null) {
6925
6951
  const cached = this._cache.get(cacheKey);
6926
6952
  if (cached != null) {
6927
- return cached instanceof Promise ? await cached : { ...cached, transport };
6953
+ if (cached instanceof Promise) {
6954
+ initializingWaiters.push(cached);
6955
+ continue;
6956
+ }
6957
+ return { ...cached, transport };
6928
6958
  }
6929
6959
  }
6930
6960
  const statusInfo = transport.getTransport(routeActionParams);
@@ -7241,8 +7271,10 @@ class TransportWebSocket extends Transport {
7241
7271
  const ws = transportStatusInfo.readyData.ws;
7242
7272
  if (ws.readyState !== WebSocket.OPEN) {
7243
7273
  const initialization = async () => {
7244
- await new Promise((resolve) => {
7245
- ws.addEventListener("open", resolve, { once: true });
7274
+ await new Promise((resolve, reject) => {
7275
+ ws.addEventListener("open", () => resolve(), { once: true });
7276
+ ws.addEventListener("error", (event) => reject(event), { once: true });
7277
+ ws.addEventListener("close", (event) => reject(new Error(`WebSocket closed before open: code=${event.code}`)), { once: true });
7246
7278
  });
7247
7279
  return {
7248
7280
  status: "ready" /* ready */,
@@ -7260,8 +7292,10 @@ class TransportWebSocket extends Transport {
7260
7292
  const promiseForReadyData = transportStatusInfo.initializationPromise.then(async (result) => {
7261
7293
  if (result.status === "ready" /* ready */) {
7262
7294
  const ws = result.readyData.ws;
7263
- await new Promise((resolve) => {
7264
- ws.addEventListener("open", resolve, { once: true });
7295
+ await new Promise((resolve, reject) => {
7296
+ ws.addEventListener("open", () => resolve(), { once: true });
7297
+ ws.addEventListener("error", (event) => reject(event), { once: true });
7298
+ ws.addEventListener("close", (event) => reject(new Error(`WebSocket closed before open: code=${event.code}`)), { once: true });
7265
7299
  });
7266
7300
  return {
7267
7301
  status: "ready" /* ready */,
@@ -3114,12 +3114,20 @@ function actionQueryKey(action, input) {
3114
3114
  }
3115
3115
  return ["nice-action", action.domain, action.allDomains, action.id, input];
3116
3116
  }
3117
- function useActionQuery(action, input, options) {
3117
+ function useActionQuery(action, ...args) {
3118
+ const hasInputSchema = action.schema.inputSchema != null;
3119
+ let input;
3120
+ let options;
3121
+ if (hasInputSchema) {
3122
+ [input, options] = args;
3123
+ } else {
3124
+ [options] = args;
3125
+ }
3118
3126
  const { enabled, ...queryOptions2 } = options ?? {};
3119
3127
  return useQuery({
3120
3128
  queryKey: input != null ? actionQueryKey(action, input) : actionQueryKey(action),
3121
3129
  queryFn: () => action.request(input).runToOutput(),
3122
- enabled: input != null && (enabled ?? true),
3130
+ enabled: hasInputSchema ? input != null && (enabled ?? true) : enabled ?? true,
3123
3131
  ...queryOptions2
3124
3132
  });
3125
3133
  }
@@ -1,5 +1,6 @@
1
1
  import type { IExecuteActionOptions } from "../../ActionRuntime/Handler/ActionHandler.types";
2
2
  import { ActionLocalHandler } from "../../ActionRuntime/Handler/Local/ActionLocalHandler";
3
+ import type { ActionRuntime } from "../../ActionRuntime/ActionRuntime";
3
4
  import type { TAction_Any_JsonObject, TDistributeActionPayload_Request, TDistributeActionPayload_Result, TDistributedDomainActions, TNarrowActionJsonTypeToActionInstanceType } from "../Action/Action.combined.types";
4
5
  import { type IActionBase } from "../Action/ActionBase.types";
5
6
  import { ActionContext } from "../Action/Context/ActionContext";
@@ -21,6 +22,7 @@ export declare class ActionDomain<ACT_DOM extends IActionDomain = IActionDomain>
21
22
  rootDomain: ActionRootDomain<any>;
22
23
  });
23
24
  get rootDomain(): ActionRootDomain<any>;
25
+ _registerRuntime(runtime: ActionRuntime): void;
24
26
  createChildDomain<SUB_DOM extends IActionDomainChildOptions>(subDomainDef: SUB_DOM & {
25
27
  [K in Exclude<keyof SUB_DOM, keyof IActionDomainChildOptions>]: never;
26
28
  }): ActionDomain<TActionDomainChildDef<ACT_DOM, SUB_DOM>>;
@@ -17,7 +17,8 @@ export declare class ActionRootDomain<ROOT_DOM extends IActionRootDomain = IActi
17
17
  createChildDomain<SUB_DOM extends IActionDomainChildOptions>(subDomainDef: SUB_DOM & {
18
18
  [K in Exclude<keyof SUB_DOM, keyof IActionDomainChildOptions>]: never;
19
19
  }): ActionDomain<TActionDomainChildDef<ROOT_DOM, SUB_DOM>>;
20
- addActionRuntime(runtime: ActionRuntime): this;
20
+ _registerRuntime(runtime: ActionRuntime): void;
21
+ _hasRuntime(runtime: ActionRuntime): boolean;
21
22
  getRuntime(clientSpecifier: IRuntimeCoordinate): ActionRuntime | undefined;
22
23
  _runAction<DOM extends IActionDomain, ID extends keyof DOM["actionSchema"] & string = keyof DOM["actionSchema"] & string, ACT extends ActionPayload_Request<DOM, ID> = ActionPayload_Request<DOM, ID>>(actionPayload: ACT, options?: IExecuteActionOptions<DOM, ID>): Promise<RunningAction<DOM, ID>>;
23
24
  }
@@ -13,6 +13,7 @@ export declare class ActionRuntime {
13
13
  private readonly actionRouter;
14
14
  private readonly _pendingRunningActions;
15
15
  private readonly _registeredExternalHandlers;
16
+ private _applied;
16
17
  static getDefault(): ActionRuntime;
17
18
  constructor(coordinate: RuntimeCoordinate);
18
19
  get coordinate(): RuntimeCoordinate;
@@ -35,6 +36,13 @@ export declare class ActionRuntime {
35
36
  * Duplicate registrations (same handler cuid for the same key) are skipped.
36
37
  */
37
38
  addHandlers(handlers: TActionRuntimeHandler[]): this;
39
+ private applyRuntimeForDomain;
40
+ /**
41
+ * Register this runtime with all root domains covered by its currently-added handlers,
42
+ * making it eligible to execute actions dispatched from those domains.
43
+ * After apply() is called, any subsequent addHandlers() calls also auto-register.
44
+ */
45
+ apply(): this;
38
46
  /**
39
47
  * Find the best registered external handler that can reach `originClient` directly.
40
48
  * Used to locate the return-path channel for dispatching results back to the action origin.
@@ -15,5 +15,6 @@ export declare class ActionRuntimeManager {
15
15
  getPreferredRuntime(): ActionRuntime | undefined;
16
16
  getBestRuntimeForSpecifier(clientSpecifier: IRuntimeCoordinate): ActionRuntime | undefined;
17
17
  getBestRuntime(clientSpecifier?: IRuntimeCoordinate): ActionRuntime | undefined;
18
+ hasRuntime(runtime: ActionRuntime): boolean;
18
19
  getBestRuntimeOrThrow(specifier?: IRuntimeCoordinate): ActionRuntime;
19
20
  }
@@ -13,7 +13,8 @@ export interface IRuntimeCoordinate {
13
13
  */
14
14
  insId?: string;
15
15
  }
16
- export type TRuntimeCoordinateStringId = `envId[${string}]perId[${string | "_"}]:insId[${string | "_"}]`;
16
+ export type TRuntimeCoordinateEnvId = `envId[${string}]`;
17
+ export type TRuntimeCoordinateStringId = `${TRuntimeCoordinateEnvId}perId[${string | "_"}]:insId[${string | "_"}]`;
17
18
  export interface IRuntimeFullCoordinates extends Required<IRuntimeCoordinate> {
18
19
  }
19
20
  export declare class RuntimeCoordinate implements IRuntimeCoordinate {
@@ -8,8 +8,10 @@ export declare function actionQueryKey<DOM extends IActionDomain, ID extends key
8
8
  DOM["domain"],
9
9
  DOM["allDomains"],
10
10
  ID,
11
- TInferInputFromSchema<DOM["actionSchema"][ID]>["Input"],
12
- string | undefined
11
+ TInferInputFromSchema<DOM["actionSchema"][ID]>["Input"]
13
12
  ];
14
13
  export type TUseActionQueryOptions<DOM extends IActionDomain, ID extends keyof DOM["actionSchema"] & string, SCH extends DOM["actionSchema"][ID] = DOM["actionSchema"][ID], TSelect = TInferOutputFromSchema<SCH>["Output"]> = Omit<UseQueryOptions<TInferOutputFromSchema<SCH>["Output"], TInferActionError<SCH>, TSelect, QueryKey>, "queryKey" | "queryFn">;
15
- export declare function useActionQuery<DOM extends IActionDomain, ID extends keyof DOM["actionSchema"] & string, SCH extends DOM["actionSchema"][ID], TSelect = TInferOutputFromSchema<SCH>["Output"]>(action: ActionCore<DOM, ID>, input: TInferInputFromSchema<SCH>["Input"] | null | undefined, options?: TUseActionQueryOptions<DOM, ID, SCH, TSelect>): UseQueryResult<TSelect, TInferActionError<SCH>>;
14
+ export declare function useActionQuery<DOM extends IActionDomain, ID extends keyof DOM["actionSchema"] & string, SCH extends DOM["actionSchema"][ID], TSelect = TInferOutputFromSchema<SCH>["Output"]>(action: ActionCore<DOM, ID>, ...args: [TInferInputFromSchema<SCH>["Input"]] extends [never] ? [options?: TUseActionQueryOptions<DOM, ID, SCH, TSelect>] : [
15
+ input: TInferInputFromSchema<SCH>["Input"] | null | undefined,
16
+ options?: TUseActionQueryOptions<DOM, ID, SCH, TSelect>
17
+ ]): UseQueryResult<TSelect, TInferActionError<SCH>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nice-code/action",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {