@holo-js/events 0.1.4 → 0.1.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/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { QueueJobDefinition, QueueJsonValue } from '@holo-js/queue';
2
2
 
3
3
  type EventDelayValue = number | Date;
4
- declare function normalizeOptionalString(value: string | undefined, label: string): string | undefined;
4
+ declare function normalizeOptionalString(value: unknown, label: string): string | undefined;
5
5
  declare function normalizeOptionalBoolean(value: boolean | undefined, label: string): boolean | undefined;
6
6
  declare function normalizeOptionalDelay(value: EventDelayValue | undefined): EventDelayValue | undefined;
7
7
  declare function toPosixPath(value: string): string;
@@ -9,6 +9,7 @@ declare function deriveEventNameFromSourcePath(sourcePath: string): string;
9
9
  declare function isReadonlyArray(value: unknown): value is readonly unknown[];
10
10
  declare function hasEventDefinitionMarker(value: unknown): boolean;
11
11
  declare function hasListenerDefinitionMarker(value: unknown): boolean;
12
+ declare function isPlainObject$1(value: unknown): value is Record<string, unknown>;
12
13
  interface EventDefinition<TPayload = unknown, TName extends string | undefined = string | undefined> {
13
14
  readonly name?: TName;
14
15
  readonly __payloadType?: TPayload;
@@ -115,6 +116,7 @@ declare function defineListener<TInput extends EventReferenceInput, TResult>(lis
115
116
  declare const eventInternals: {
116
117
  hasEventDefinitionMarker: typeof hasEventDefinitionMarker;
117
118
  hasListenerDefinitionMarker: typeof hasListenerDefinitionMarker;
119
+ isPlainObject: typeof isPlainObject$1;
118
120
  isReadonlyArray: typeof isReadonlyArray;
119
121
  deriveEventNameFromSourcePath: typeof deriveEventNameFromSourcePath;
120
122
  normalizeEventDefinition: typeof normalizeEventDefinition;
package/dist/index.mjs CHANGED
@@ -5,6 +5,9 @@ function normalizeOptionalString(value, label) {
5
5
  if (typeof value === "undefined") {
6
6
  return void 0;
7
7
  }
8
+ if (typeof value !== "string") {
9
+ throw new Error(`[Holo Events] ${label} must be a non-empty string when provided.`);
10
+ }
8
11
  const normalized = value.trim();
9
12
  if (!normalized) {
10
13
  throw new Error(`[Holo Events] ${label} must be a non-empty string when provided.`);
@@ -57,8 +60,15 @@ function hasEventDefinitionMarker(value) {
57
60
  function hasListenerDefinitionMarker(value) {
58
61
  return !!value && typeof value === "object" && HOLO_LISTENER_DEFINITION_MARKER in value;
59
62
  }
63
+ function isPlainObject(value) {
64
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
65
+ return false;
66
+ }
67
+ const prototype = Object.getPrototypeOf(value);
68
+ return prototype === Object.prototype || prototype === null;
69
+ }
60
70
  function isEventDefinition(value) {
61
- return value !== null && typeof value === "object" && !Array.isArray(value);
71
+ return isPlainObject(value);
62
72
  }
63
73
  function normalizeEventDefinition(event) {
64
74
  if (!isEventDefinition(event)) {
@@ -140,6 +150,7 @@ function defineListener(listener) {
140
150
  var eventInternals = {
141
151
  hasEventDefinitionMarker,
142
152
  hasListenerDefinitionMarker,
153
+ isPlainObject,
143
154
  isReadonlyArray,
144
155
  deriveEventNameFromSourcePath,
145
156
  normalizeEventDefinition,
@@ -173,30 +184,30 @@ function deriveListenerIdFromSourcePath(sourcePath) {
173
184
  return derived;
174
185
  }
175
186
  function resolveRegisteredEventName(definition, options = {}) {
176
- const explicitOption = options.name?.trim();
187
+ const explicitOption = eventInternals.normalizeOptionalString(options.name, "Event registration name");
177
188
  if (explicitOption) {
178
189
  return explicitOption;
179
190
  }
180
- const explicitDefinition = definition.name?.trim();
191
+ const explicitDefinition = eventInternals.normalizeOptionalString(definition.name, "Event name");
181
192
  if (explicitDefinition) {
182
193
  return explicitDefinition;
183
194
  }
184
- const sourcePath = options.sourcePath?.trim();
195
+ const sourcePath = eventInternals.normalizeOptionalString(options.sourcePath, "Event source path");
185
196
  if (sourcePath) {
186
197
  return eventInternals.deriveEventNameFromSourcePath(sourcePath);
187
198
  }
188
199
  throw new Error("[Holo Events] Registered events require an explicit name or a sourcePath-derived name.");
189
200
  }
190
201
  function resolveRegisteredListenerId(definition, options = {}) {
191
- const explicitOption = options.id?.trim();
202
+ const explicitOption = eventInternals.normalizeOptionalString(options.id, "Listener id");
192
203
  if (explicitOption) {
193
204
  return explicitOption;
194
205
  }
195
- const explicitDefinition = definition.name?.trim();
206
+ const explicitDefinition = eventInternals.normalizeOptionalString(definition.name, "Listener name");
196
207
  if (explicitDefinition) {
197
208
  return explicitDefinition;
198
209
  }
199
- const sourcePath = options.sourcePath?.trim();
210
+ const sourcePath = eventInternals.normalizeOptionalString(options.sourcePath, "Listener source path");
200
211
  if (sourcePath) {
201
212
  return deriveListenerIdFromSourcePath(sourcePath);
202
213
  }
@@ -206,7 +217,7 @@ function resolveListenerEventName(reference) {
206
217
  if (typeof reference === "string") {
207
218
  return reference;
208
219
  }
209
- const explicitName = reference.name?.trim();
220
+ const explicitName = eventInternals.normalizeOptionalString(reference.name, "Listener event reference name");
210
221
  if (explicitName) {
211
222
  return explicitName;
212
223
  }
@@ -247,13 +258,14 @@ function registerEvent(definition, options = {}) {
247
258
  }
248
259
  const normalizedDefinition = normalizeEventDefinition(definition);
249
260
  const name = resolveRegisteredEventName(normalizedDefinition, options);
261
+ const sourcePath = eventInternals.normalizeOptionalString(options.sourcePath, "Event source path");
250
262
  const state = getEventRegistryState();
251
263
  if (state.events.has(name) && options.replaceExisting !== true) {
252
264
  throw new Error(`[Holo Events] Event "${name}" is already registered.`);
253
265
  }
254
266
  const entry = Object.freeze({
255
267
  name,
256
- ...options.sourcePath ? { sourcePath: options.sourcePath } : {},
268
+ ...typeof sourcePath === "undefined" ? {} : { sourcePath },
257
269
  definition: Object.freeze({
258
270
  ...normalizedDefinition,
259
271
  name
@@ -287,6 +299,7 @@ function registerListener(definition, options = {}) {
287
299
  }
288
300
  const normalizedDefinition = normalizeListenerDefinition(definition);
289
301
  const id = resolveRegisteredListenerId(normalizedDefinition, options);
302
+ const sourcePath = eventInternals.normalizeOptionalString(options.sourcePath, "Listener source path");
290
303
  const state = getEventRegistryState();
291
304
  if (state.listeners.has(id) && options.replaceExisting !== true) {
292
305
  throw new Error(`[Holo Events] Listener "${id}" is already registered.`);
@@ -298,7 +311,7 @@ function registerListener(definition, options = {}) {
298
311
  }
299
312
  const listener = Object.freeze({
300
313
  id,
301
- ...options.sourcePath ? { sourcePath: options.sourcePath } : {},
314
+ ...typeof sourcePath === "undefined" ? {} : { sourcePath },
302
315
  eventNames,
303
316
  definition: Object.freeze({
304
317
  ...normalizedDefinition,
@@ -467,7 +480,7 @@ var eventQueueInternals = {
467
480
  };
468
481
 
469
482
  // src/runtime.ts
470
- function isPlainObject(value) {
483
+ function isPlainObject2(value) {
471
484
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
472
485
  return false;
473
486
  }
@@ -498,7 +511,7 @@ function assertEventJsonValue(value, path, state) {
498
511
  state.seen.delete(value);
499
512
  return;
500
513
  }
501
- if (!isPlainObject(value)) {
514
+ if (!isPlainObject2(value)) {
502
515
  throw new TypeError(`[Holo Events] Event payload at "${path}" must be a plain JSON object, array, or primitive for queued listeners.`);
503
516
  }
504
517
  if (state.seen.has(value)) {
@@ -528,6 +541,9 @@ function createRuntimeBinding(state) {
528
541
  });
529
542
  }
530
543
  function normalizeEventName(name) {
544
+ if (typeof name !== "string") {
545
+ throw new Error("[Holo Events] Event names must be non-empty strings.");
546
+ }
531
547
  const normalized = name.trim();
532
548
  if (!normalized) {
533
549
  throw new Error("[Holo Events] Event names must be non-empty strings.");
@@ -541,7 +557,7 @@ function resolveDispatchedEventName(event) {
541
557
  if (!isEventDefinition(event)) {
542
558
  throw new Error("[Holo Events] Events must be plain objects.");
543
559
  }
544
- const explicitName = event.name?.trim();
560
+ const explicitName = eventInternals.normalizeOptionalString(event.name, "Event name");
545
561
  if (!explicitName) {
546
562
  throw new Error("[Holo Events] Dispatching an event definition requires an explicit event name.");
547
563
  }
@@ -786,7 +802,7 @@ var eventRuntimeInternals = {
786
802
  executeListener,
787
803
  executeSyncListeners,
788
804
  getRuntimeState,
789
- isPlainObject,
805
+ isPlainObject: isPlainObject2,
790
806
  normalizeEventName,
791
807
  requireRegisteredEvent,
792
808
  resolveDispatchedEventName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holo-js/events",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Holo-JS Framework - event contracts and runtime surface",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,10 +23,10 @@
23
23
  "test": "vitest --run"
24
24
  },
25
25
  "dependencies": {
26
- "@holo-js/db": "^0.1.4"
26
+ "@holo-js/db": "^0.1.6"
27
27
  },
28
28
  "peerDependencies": {
29
- "@holo-js/queue": "^0.1.4"
29
+ "@holo-js/queue": "^0.1.6"
30
30
  },
31
31
  "peerDependenciesMeta": {
32
32
  "@holo-js/queue": {
@@ -34,9 +34,10 @@
34
34
  }
35
35
  },
36
36
  "devDependencies": {
37
+ "@holo-js/queue": "^0.1.6",
37
38
  "@types/node": "^22.10.2",
38
39
  "tsup": "^8.3.5",
39
40
  "typescript": "^5.7.2",
40
- "vitest": "^2.1.8"
41
+ "vitest": "^4.1.5"
41
42
  }
42
43
  }