@faststats/react 0.4.0 → 0.4.2

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.
Files changed (2) hide show
  1. package/dist/index.js +23 -29
  2. package/package.json +5 -4
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { WebAnalytics, getInstance, getInstance as getInstance$1, identify, identify as identify$1, logout, logout as logout$1, optIn, optIn as optIn$1, optOut, optOut as optOut$1, reportError, setConsentMode, setConsentMode as setConsentMode$1, trackEvent, trackEvent as trackEvent$1 } from "@faststats/web";
3
- import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
3
+ import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
4
4
  //#region src/instance.ts
5
5
  let instance = null;
6
6
  const listeners = /* @__PURE__ */ new Set();
@@ -20,7 +20,7 @@ function setInstance(wa) {
20
20
  //#endregion
21
21
  //#region src/analytics.tsx
22
22
  const SDK_NAME = "@faststats/react";
23
- const SDK_VERSION = "0.4.0";
23
+ const SDK_VERSION = "0.4.2";
24
24
  function Analytics(props) {
25
25
  const ownedRef = useRef(null);
26
26
  const propsRef = useRef(props);
@@ -81,42 +81,34 @@ function useOptOut() {
81
81
  //#endregion
82
82
  //#region src/use-flag.ts
83
83
  const defaultEvaluation = { value: "false" };
84
- function stableSerialize(attributes) {
85
- if (!attributes) return "";
86
- const keys = Object.keys(attributes).sort();
87
- const obj = {};
88
- for (const k of keys) obj[k] = attributes[k];
89
- return JSON.stringify(obj);
90
- }
84
+ let hasWarnedAboutMissingInstance = false;
91
85
  function useFlag(flagKey, options) {
92
86
  const wa = useSyncExternalStore(subscribeToInstance, getInstanceSnapshot, () => null);
93
87
  const attributes = options?.attributes;
94
88
  const externalId = options?.externalId;
95
89
  const skip = options?.skip ?? false;
96
- const attrKey = useMemo(() => stableSerialize(attributes), [attributes]);
97
- const [refetchCount, setRefetchCount] = useState(0);
98
90
  const [evaluation, setEvaluation] = useState(defaultEvaluation);
99
91
  const [isLoading, setIsLoading] = useState(!skip);
100
92
  const [error, setError] = useState(null);
101
- const refetch = useCallback(() => setRefetchCount((n) => n + 1), []);
102
- useEffect(() => {
103
- if (skip || typeof window === "undefined") {
104
- setIsLoading(false);
105
- setEvaluation(defaultEvaluation);
106
- setError(null);
107
- return;
108
- }
109
- if (!wa) {
93
+ const abortRef = useRef(null);
94
+ const fetchFlag = useCallback(() => {
95
+ abortRef.current?.abort();
96
+ abortRef.current = null;
97
+ if (skip || !wa) {
98
+ if (!skip && !wa && !hasWarnedAboutMissingInstance && true) {
99
+ hasWarnedAboutMissingInstance = true;
100
+ console.warn("[faststats/react] useFlag requires an active <Analytics /> instance.");
101
+ }
110
102
  setIsLoading(false);
111
103
  setEvaluation(defaultEvaluation);
112
104
  setError(null);
113
105
  return;
114
106
  }
115
107
  const ac = new AbortController();
108
+ abortRef.current = ac;
116
109
  setIsLoading(true);
117
110
  setError(null);
118
- const requestAttributes = attrKey ? JSON.parse(attrKey) : void 0;
119
- wa.checkFeatureFlag(flagKey, requestAttributes, {
111
+ wa.checkFeatureFlag(flagKey, attributes, {
120
112
  externalId,
121
113
  signal: ac.signal
122
114
  }).then((result) => {
@@ -131,22 +123,24 @@ function useFlag(flagKey, options) {
131
123
  setEvaluation(defaultEvaluation);
132
124
  setIsLoading(false);
133
125
  });
134
- return () => {
135
- ac.abort();
136
- };
137
126
  }, [
138
127
  wa,
139
128
  flagKey,
129
+ attributes,
140
130
  externalId,
141
- skip,
142
- attrKey,
143
- refetchCount
131
+ skip
144
132
  ]);
133
+ useEffect(() => {
134
+ fetchFlag();
135
+ return () => {
136
+ abortRef.current?.abort();
137
+ };
138
+ }, [fetchFlag]);
145
139
  return {
146
140
  ...evaluation,
147
141
  isLoading,
148
142
  error,
149
- refetch
143
+ refetch: fetchFlag
150
144
  };
151
145
  }
152
146
  //#endregion
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "type": "git",
5
5
  "url": "https://github.com/faststats-dev/faststats-javascript.git"
6
6
  },
7
- "version": "0.4.0",
7
+ "version": "0.4.2",
8
8
  "type": "module",
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.js",
@@ -31,12 +31,13 @@
31
31
  "react": ">=18"
32
32
  },
33
33
  "dependencies": {
34
- "@faststats/web": "^0.4.0"
34
+ "@faststats/web": "^0.4.2"
35
35
  },
36
36
  "devDependencies": {
37
+ "@types/node": "^25.9.3",
37
38
  "@types/react": "^19.0.0",
38
39
  "react": "^19.0.0",
39
- "tsdown": "^0.21.4",
40
- "typescript": "^5.9.3"
40
+ "tsdown": "^0.22.3",
41
+ "typescript": "^6.0.3"
41
42
  }
42
43
  }