@faststats/react 0.5.0 → 0.7.0

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 +30 -21
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -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.5.0";
23
+ const SDK_VERSION = "0.7.0";
24
24
  function Analytics(props) {
25
25
  const ownedRef = useRef(null);
26
26
  const propsRef = useRef(props);
@@ -28,7 +28,11 @@ function Analytics(props) {
28
28
  useEffect(() => {
29
29
  if (typeof window === "undefined") return;
30
30
  if (ownedRef.current) return;
31
- getInstance$1()?.destroy();
31
+ const existing = getInstance$1();
32
+ if (existing) {
33
+ setInstance(existing);
34
+ return () => setInstance(null);
35
+ }
32
36
  const wa = new WebAnalytics({
33
37
  ...propsRef.current,
34
38
  sdkName: SDK_NAME,
@@ -36,14 +40,12 @@ function Analytics(props) {
36
40
  });
37
41
  ownedRef.current = wa;
38
42
  setInstance(wa);
39
- window.__faststats = { getInstance: getInstance$1 };
40
43
  return () => {
41
44
  if (ownedRef.current === wa) {
42
45
  wa.destroy();
43
46
  ownedRef.current = null;
44
47
  }
45
48
  setInstance(null);
46
- window.__faststats = void 0;
47
49
  };
48
50
  }, []);
49
51
  useEffect(() => {
@@ -80,16 +82,21 @@ function useOptOut() {
80
82
  }
81
83
  //#endregion
82
84
  //#region src/use-flag.ts
83
- const defaultEvaluation = { value: "false" };
85
+ const defaultState = {
86
+ value: "false",
87
+ isLoading: false,
88
+ error: null
89
+ };
84
90
  let hasWarnedAboutMissingInstance = false;
85
91
  function useFlag(flagKey, options) {
86
92
  const wa = useSyncExternalStore(subscribeToInstance, getInstanceSnapshot, () => null);
87
93
  const attributes = options?.attributes;
88
94
  const externalId = options?.externalId;
89
95
  const skip = options?.skip ?? false;
90
- const [evaluation, setEvaluation] = useState(defaultEvaluation);
91
- const [isLoading, setIsLoading] = useState(!skip);
92
- const [error, setError] = useState(null);
96
+ const [state, setState] = useState({
97
+ ...defaultState,
98
+ isLoading: !skip
99
+ });
93
100
  const abortRef = useRef(null);
94
101
  const fetchFlag = useCallback(() => {
95
102
  abortRef.current?.abort();
@@ -99,29 +106,33 @@ function useFlag(flagKey, options) {
99
106
  hasWarnedAboutMissingInstance = true;
100
107
  console.warn("[faststats/react] useFlag requires an active <Analytics /> instance.");
101
108
  }
102
- setIsLoading(false);
103
- setEvaluation(defaultEvaluation);
104
- setError(null);
109
+ setState(defaultState);
105
110
  return;
106
111
  }
107
112
  const ac = new AbortController();
108
113
  abortRef.current = ac;
109
- setIsLoading(true);
110
- setError(null);
114
+ setState({
115
+ ...defaultState,
116
+ isLoading: true
117
+ });
111
118
  wa.checkFeatureFlag(flagKey, attributes, {
112
119
  externalId,
113
120
  signal: ac.signal
114
121
  }).then((result) => {
115
122
  if (ac.signal.aborted) return;
116
- setEvaluation(result);
117
- setIsLoading(false);
123
+ setState({
124
+ ...result,
125
+ isLoading: false,
126
+ error: null
127
+ });
118
128
  }).catch((e) => {
119
129
  if (ac.signal.aborted) return;
120
130
  const err = e instanceof Error ? e : new Error(String(e));
121
131
  if (err.name === "AbortError") return;
122
- setError(err);
123
- setEvaluation(defaultEvaluation);
124
- setIsLoading(false);
132
+ setState({
133
+ ...defaultState,
134
+ error: err
135
+ });
125
136
  });
126
137
  }, [
127
138
  wa,
@@ -137,9 +148,7 @@ function useFlag(flagKey, options) {
137
148
  };
138
149
  }, [fetchFlag]);
139
150
  return {
140
- ...evaluation,
141
- isLoading,
142
- error,
151
+ ...state,
143
152
  refetch: fetchFlag
144
153
  };
145
154
  }
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.5.0",
7
+ "version": "0.7.0",
8
8
  "type": "module",
9
9
  "main": "./dist/index.js",
10
10
  "module": "./dist/index.js",
@@ -31,7 +31,7 @@
31
31
  "react": ">=18"
32
32
  },
33
33
  "dependencies": {
34
- "@faststats/web": "^0.5.0"
34
+ "@faststats/web": "^0.7.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "^25.9.3",