@faststats/react 0.10.3 → 0.12.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.
- package/README.md +26 -0
- package/dist/flags.d.ts +21 -0
- package/dist/flags.js +45 -0
- package/dist/index.js +1 -1
- package/package.json +8 -8
- package/dist/outbound-links.d.ts +0 -2
- package/dist/outbound-links.js +0 -3
package/README.md
CHANGED
|
@@ -40,3 +40,29 @@ export function FastStats() {
|
|
|
40
40
|
);
|
|
41
41
|
}
|
|
42
42
|
```
|
|
43
|
+
|
|
44
|
+
### Feature flags
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
"use client";
|
|
48
|
+
|
|
49
|
+
import { Analytics } from "@faststats/react";
|
|
50
|
+
import { featureFlags, useFlag } from "@faststats/react/flags";
|
|
51
|
+
|
|
52
|
+
const flags = featureFlags({ flags: ["new-checkout", "max-uploads"] });
|
|
53
|
+
|
|
54
|
+
export function FastStats() {
|
|
55
|
+
return <Analytics siteKey="site_123" extensions={[flags]} />;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function Checkout() {
|
|
59
|
+
const newCheckout = useFlag("new-checkout", false);
|
|
60
|
+
return newCheckout ? <NewCheckout /> : <OldCheckout />;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`useFlag` returns the default during server rendering and until flags have
|
|
65
|
+
loaded, then re-renders with the flag's value. `useIdentify` and `useLogout`
|
|
66
|
+
re-evaluate flags for the new user. `useFeatureFlags()` returns the running
|
|
67
|
+
instance for `status`, `details()`, `setContext()` and `reload()`; see
|
|
68
|
+
[`@faststats/web`](../core/README.md#feature-flags) for the options.
|
package/dist/flags.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { FeatureFlagReason, FeatureFlagResult, FeatureFlags as FeatureFlags$1, FeatureFlagsContext, FeatureFlagsOptions, FeatureFlagsOptions as FeatureFlagsOptions$1, FeatureFlagsStatus } from "@faststats/web/flags";
|
|
2
|
+
//#region src/flags.d.ts
|
|
3
|
+
/** Feature flags that the hooks read once `<Analytics>` has started them. */
|
|
4
|
+
export declare class FeatureFlags extends FeatureFlags$1 {
|
|
5
|
+
setup(...args: Parameters<FeatureFlags$1["setup"]>): (options: {
|
|
6
|
+
discard: boolean;
|
|
7
|
+
}) => void;
|
|
8
|
+
}
|
|
9
|
+
export declare function featureFlags(options?: FeatureFlagsOptions$1): FeatureFlags;
|
|
10
|
+
/**
|
|
11
|
+
* The flag's value, or `defaultValue` while flags are loading, during server
|
|
12
|
+
* rendering, and when the flag does not exist or has another type.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useFlag(key: string, defaultValue: boolean): boolean;
|
|
15
|
+
export declare function useFlag(key: string, defaultValue: string): string;
|
|
16
|
+
export declare function useFlag(key: string, defaultValue: number): number;
|
|
17
|
+
export declare function useFlag<T>(key: string, defaultValue: T): T;
|
|
18
|
+
/** The running flags instance, re-rendering whenever flags or status change. */
|
|
19
|
+
export declare function useFeatureFlags(): FeatureFlags | null;
|
|
20
|
+
//#endregion
|
|
21
|
+
export type { FeatureFlagReason, FeatureFlagResult, FeatureFlagsContext, FeatureFlagsOptions, FeatureFlagsStatus };
|
package/dist/flags.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useSyncExternalStore } from "react";
|
|
3
|
+
import { FeatureFlags as FeatureFlags$1 } from "@faststats/web/flags";
|
|
4
|
+
//#region src/flags.ts
|
|
5
|
+
let active = null;
|
|
6
|
+
let version = 0;
|
|
7
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
8
|
+
function notify() {
|
|
9
|
+
version++;
|
|
10
|
+
for (const l of listeners) l();
|
|
11
|
+
}
|
|
12
|
+
function subscribe(onStoreChange) {
|
|
13
|
+
listeners.add(onStoreChange);
|
|
14
|
+
return () => {
|
|
15
|
+
listeners.delete(onStoreChange);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** Feature flags that the hooks read once `<Analytics>` has started them. */
|
|
19
|
+
var FeatureFlags = class extends FeatureFlags$1 {
|
|
20
|
+
setup(...args) {
|
|
21
|
+
const cleanup = super.setup(...args);
|
|
22
|
+
const off = this.subscribe(notify);
|
|
23
|
+
active = this;
|
|
24
|
+
notify();
|
|
25
|
+
return (options) => {
|
|
26
|
+
cleanup(options);
|
|
27
|
+
off();
|
|
28
|
+
if (active === this) active = null;
|
|
29
|
+
notify();
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
function featureFlags(options = {}) {
|
|
34
|
+
return new FeatureFlags(options);
|
|
35
|
+
}
|
|
36
|
+
function useFlag(key, defaultValue) {
|
|
37
|
+
return useSyncExternalStore(subscribe, () => active ? active.get(key, defaultValue) : defaultValue, () => defaultValue);
|
|
38
|
+
}
|
|
39
|
+
/** The running flags instance, re-rendering whenever flags or status change. */
|
|
40
|
+
function useFeatureFlags() {
|
|
41
|
+
useSyncExternalStore(subscribe, () => version, () => 0);
|
|
42
|
+
return active;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { FeatureFlags, featureFlags, useFeatureFlags, useFlag };
|
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.
|
|
23
|
+
const SDK_VERSION = "0.12.0";
|
|
24
24
|
function Analytics(props) {
|
|
25
25
|
const ownedRef = useRef(null);
|
|
26
26
|
const propsRef = useRef(props);
|
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.
|
|
7
|
+
"version": "0.12.0",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"module": "./dist/index.js",
|
|
@@ -19,16 +19,16 @@
|
|
|
19
19
|
"import": "./dist/index.js",
|
|
20
20
|
"default": "./dist/index.js"
|
|
21
21
|
},
|
|
22
|
-
"./outbound-links": {
|
|
23
|
-
"types": "./dist/outbound-links.d.ts",
|
|
24
|
-
"import": "./dist/outbound-links.js",
|
|
25
|
-
"default": "./dist/outbound-links.js"
|
|
26
|
-
},
|
|
27
22
|
"./error": {
|
|
28
23
|
"types": "./dist/error.d.ts",
|
|
29
24
|
"import": "./dist/error.js",
|
|
30
25
|
"default": "./dist/error.js"
|
|
31
26
|
},
|
|
27
|
+
"./flags": {
|
|
28
|
+
"types": "./dist/flags.d.ts",
|
|
29
|
+
"import": "./dist/flags.js",
|
|
30
|
+
"default": "./dist/flags.js"
|
|
31
|
+
},
|
|
32
32
|
"./web-vitals": {
|
|
33
33
|
"types": "./dist/web-vitals.d.ts",
|
|
34
34
|
"import": "./dist/web-vitals.js",
|
|
@@ -51,10 +51,10 @@
|
|
|
51
51
|
"react": ">=18"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@faststats/web": "^0.
|
|
54
|
+
"@faststats/web": "^0.12.0"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/node": "^26.6.
|
|
57
|
+
"@types/node": "^26.6.4",
|
|
58
58
|
"@types/react": "^19.3.0",
|
|
59
59
|
"react": "^19.3.0",
|
|
60
60
|
"tsdown": "^0.23.0",
|
package/dist/outbound-links.d.ts
DELETED
package/dist/outbound-links.js
DELETED