@reactionary/core 0.12.0 → 0.12.1

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.
@@ -1,4 +1,3 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  import { traceSpan } from "../decorators/reactionary.decorator.js";
3
2
  import {
4
3
  AnalyticsEventMutationSchemas,
@@ -8,7 +7,29 @@ import {
8
7
  } from "../schemas/index.js";
9
8
  import { error, success } from "../schemas/result.js";
10
9
  const DEFAULT_CONSENT = { analytics: true, marketing: true };
11
- const insideHandler = new AsyncLocalStorage();
10
+ function createHandlerScope() {
11
+ const asyncHooks = typeof process !== "undefined" ? process.getBuiltinModule?.("node:async_hooks") : void 0;
12
+ if (asyncHooks) {
13
+ const storage = new asyncHooks.AsyncLocalStorage();
14
+ return {
15
+ run: (fn) => storage.run(true, fn),
16
+ active: () => storage.getStore() === true
17
+ };
18
+ }
19
+ let running = 0;
20
+ return {
21
+ async run(fn) {
22
+ running++;
23
+ try {
24
+ return await fn();
25
+ } finally {
26
+ running--;
27
+ }
28
+ },
29
+ active: () => running > 0
30
+ };
31
+ }
32
+ const insideHandler = createHandlerScope();
12
33
  function eventTypeMatches(pattern, type) {
13
34
  if (pattern === "*") {
14
35
  return true;
@@ -54,7 +75,7 @@ class AnalyticsDispatcher {
54
75
  * Never throws: invalid input and emits made from inside a subscriber are logged and dropped.
55
76
  */
56
77
  emit(type, input, options = {}) {
57
- if (insideHandler.getStore()) {
78
+ if (insideHandler.active()) {
58
79
  console.warn(`Analytics event '${type}' was emitted from inside a subscriber and has been dropped`);
59
80
  return error({ type: "InvalidInput", error: "Events cannot be emitted from inside an analytics subscriber" });
60
81
  }
@@ -162,7 +183,6 @@ class AnalyticsDispatcher {
162
183
  for (const event of events) {
163
184
  try {
164
185
  const outcome = await insideHandler.run(
165
- true,
166
186
  () => traceSpan(`AnalyticsSubscriber.${subscriber.name}.handle`, () => subscriber.handle(event, ctx))
167
187
  );
168
188
  if (outcome === "rejected") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactionary/core",
3
- "version": "0.12.0",
3
+ "version": "0.12.1",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./src/index.d.ts",