@productcraft/flags-react 0.1.2 → 0.1.3

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 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/provider.ts"],"sourcesContent":["/**\n * @productcraft/flags-react: React bindings for ProductCraft Flags. `FlagsProvider` holds a\n * FlagsClient (local evaluation; user attributes never leave the app), the hooks read it\n * synchronously and re-render when the config changes. SSR: pass `snapshot().toJSON()` from the\n * server as `bootstrap`.\n *\n * A key used in a browser or mobile app makes its whole config public: keep client flags in their\n * own config, with no internal rules.\n */\nexport { FlagsProvider, useFlag, useFlagDetails, useFlagsClient, useFlagsReady, type FlagDefault, type FlagsProviderProps } from \"./provider.js\";\n","// React bindings for FlagsClient (task 019, design 8.1): one FlagsClient per provider, hooks that\n// read a FlagsSnapshot synchronously, and re-renders on config changes via useSyncExternalStore.\n// SSR: the server renders from `bootstrap` (a `snapshot().toJSON()`), and hydration reads the same\n// bootstrap, so the first client render matches the server HTML. Downloads start after mount.\n\nimport { createContext, createElement, useContext, useEffect, useMemo, useState, useSyncExternalStore, type ReactElement, type ReactNode } from \"react\";\nimport { FlagsClient, type EvaluationDetails, type FlagsClientOptions, type FlagsSnapshot, type FlagValue, type SettingValue, type User } from \"@productcraft/flags\";\n\n/** A default value: booleans, strings and numbers keep their type; objects, arrays and `null` read json settings. */\nexport type FlagDefault = SettingValue | object | null;\n\nexport interface FlagsProviderProps {\n /** A FlagsClient you built and own (the provider never closes it). Give this or `options`, and keep to that choice for the provider's lifetime. */\n client?: FlagsClient;\n /** Options for a FlagsClient the provider builds on mount and closes on unmount. On the server it never downloads. */\n options?: FlagsClientOptions;\n /** A config JSON, usually `snapshot().toJSON()` from the server: the first render (server and hydration) evaluates it. */\n bootstrap?: string | object | null;\n /** The user hooks evaluate for when a hook passes none. */\n user?: User;\n children?: ReactNode;\n}\n\ninterface Ctx {\n client: FlagsClient;\n store: Store;\n user: User | undefined;\n}\n\nconst FlagsContext = createContext<Ctx | null>(null);\nconst onServer = (): boolean => typeof (globalThis as { window?: unknown }).window === \"undefined\";\n\n/** The snapshot hooks read, replaced only when the config or the client state really changed. */\nclass Store {\n #snap: FlagsSnapshot;\n #state: string;\n readonly #listeners = new Set<() => void>();\n /** What the server rendered: the bootstrap when there is one. */\n readonly server: FlagsSnapshot;\n readonly client: FlagsClient;\n\n constructor(client: FlagsClient, bootstrap: string | object | null | undefined) {\n this.client = client;\n this.#snap = client.snapshot();\n this.#state = client.state;\n this.server = bootstrap == null ? this.#snap : offlineSnapshot(bootstrap);\n }\n\n readonly subscribe = (listener: () => void): (() => void) => {\n this.#listeners.add(listener);\n return () => this.#listeners.delete(listener);\n };\n\n readonly get = (): FlagsSnapshot => this.#snap;\n\n readonly ready = (): boolean => this.client.state !== \"initializing\";\n\n /** Listens to the client; returns the function that stops. Picks up anything that changed before it listened. */\n attach(): () => void {\n const update = (): void => {\n const next = this.client.snapshot();\n if (next.toJSON() === this.#snap.toJSON() && this.client.state === this.#state) return;\n this.#snap = next;\n this.#state = this.client.state;\n for (const l of [...this.#listeners]) l();\n };\n const offs = [this.client.on(\"configChanged\", update), this.client.on(\"ready\", update), this.client.on(\"error\", update)];\n update();\n return () => offs.forEach((off) => off());\n }\n}\n\nfunction offlineSnapshot(bootstrap: string | object): FlagsSnapshot {\n const c = new FlagsClient({ bootstrap, offline: true, logger: null });\n c.close();\n return c.snapshot();\n}\n\nfunction build(options: FlagsClientOptions, bootstrap: string | object | null | undefined): FlagsClient {\n return new FlagsClient({ ...options, bootstrap: bootstrap ?? options.bootstrap, ...(onServer() ? { offline: true } : null) });\n}\n\n/**\n * Makes a FlagsClient available to the hooks below it.\n * @example\n * ```tsx\n * <FlagsProvider options={{ sdkKey: \"pcf_sdk_…\" }} user={{ identifier: user.id }}>\n * <App />\n * </FlagsProvider>\n * ```\n */\nexport function FlagsProvider(props: FlagsProviderProps): ReactElement {\n const { client: given, options, bootstrap, user, children } = props;\n if (!given && !options) throw new Error(\"<FlagsProvider> needs a `client` or `options`\");\n const [owned, setOwned] = useState<FlagsClient | null>(() => (given ? null : build(options!, bootstrap)));\n useEffect(() => {\n if (given || !owned) return;\n // React StrictMode unmounts and remounts once in development: replace the client that unmount closed.\n if (owned.state === \"closed\") return void setOwned(build(options!, bootstrap));\n return () => owned.close();\n // `options` and `bootstrap` are read once, like useState's initial value.\n }, [given, owned]);\n const client = given ?? owned!;\n const store = useMemo(() => new Store(client, bootstrap), [client]);\n useEffect(() => store.attach(), [store]);\n const value = useMemo<Ctx>(() => ({ client, store, user }), [client, store, user]);\n return createElement(FlagsContext.Provider, { value }, children);\n}\n\nfunction useFlagsContext(hook: string): Ctx {\n const ctx = useContext(FlagsContext);\n if (!ctx) throw new Error(`${hook}() must be called inside <FlagsProvider>`);\n return ctx;\n}\n\n/** A stable dependency for a default or user that callers usually write inline. */\nconst dep = (v: unknown): unknown => (v !== null && typeof v === \"object\" ? JSON.stringify(v) : v);\n\n/** The value of a flag or setting, evaluated locally; `defaultValue` until a config is there, or on any failure. Re-renders when the config changes. */\nexport function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): FlagValue<D> {\n return useFlagDetails(key, defaultValue, user).value;\n}\n\n/** The value plus why (reason, matched rule or split, error code, revision). */\nexport function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): EvaluationDetails<FlagValue<D>> {\n const { store, user: fallback } = useFlagsContext(\"useFlagDetails\");\n const snap = useSyncExternalStore(store.subscribe, store.get, () => store.server);\n const u = user ?? fallback;\n return useMemo(() => snap.getValueDetails(key, defaultValue, u), [snap, key, dep(defaultValue), dep(u)]);\n}\n\n/** The provider's FlagsClient (for `getAllValues`, `forceRefresh`, events). */\nexport function useFlagsClient(): FlagsClient {\n return useFlagsContext(\"useFlagsClient\").client;\n}\n\n/** False until the client's start-up settled (first download, or `maxInitWaitSeconds`); always false while rendering on the server. */\nexport function useFlagsReady(): boolean {\n const { store } = useFlagsContext(\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AAM,CAAC,MAAyB,MAAM,QAAQ,OAAO,MAAM,WAAW,KAAK,UAAU,CAAC,IAAI;AAGzF,SAAS,QAA+B,KAAa,cAAiB,MAAkC;AAC7G,SAAO,eAAe,KAAK,cAAc,IAAI,EAAE;AACjD;AAGO,SAAS,eAAsC,KAAa,cAAiB,MAAqD;AACvI,QAAM,EAAE,OAAO,MAAM,SAAS,IAAI,gBAAgB,gBAAgB;AAClE,QAAM,WAAO,mCAAqB,MAAM,WAAW,MAAM,KAAK,MAAM,MAAM,MAAM;AAChF,QAAM,IAAI,QAAQ;AAClB,aAAO,sBAAQ,MAAM,KAAK,gBAAgB,KAAK,cAAc,CAAC,GAAG,CAAC,MAAM,KAAK,IAAI,YAAY,GAAG,IAAI,CAAC,CAAC,CAAC;AACzG;AAGO,SAAS,iBAA8B;AAC5C,SAAO,gBAAgB,gBAAgB,EAAE;AAC3C;AAGO,SAAS,gBAAyB;AACvC,QAAM,EAAE,MAAM,IAAI,gBAAgB,eAAe;AACjD,aAAO,mCAAqB,MAAM,WAAW,MAAM,OAAO,MAAM,KAAK;AACvE;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/provider.ts"],"sourcesContent":["/**\n * @productcraft/flags-react: React bindings for ProductCraft Flags. `FlagsProvider` holds a\n * FlagsClient (local evaluation; user attributes never leave the app), the hooks read it\n * synchronously and re-render when the config changes. SSR: pass `snapshot().toJSON()` from the\n * server as `bootstrap`.\n *\n * A key used in a browser or mobile app makes its whole config public: keep client flags in their\n * own config, with no internal rules.\n */\nexport { FlagsProvider, useFlag, useFlagDetails, useFlagsClient, useFlagsReady, type FlagDefault, type FlagsProviderProps } from \"./provider.js\";\n","// React bindings for FlagsClient (task 019, design 8.1): one FlagsClient per provider, hooks that\n// read a FlagsSnapshot synchronously, and re-renders on config changes via useSyncExternalStore.\n// SSR: the server renders from `bootstrap` (a `snapshot().toJSON()`), and hydration reads the same\n// bootstrap, so the first client render matches the server HTML. Downloads start after mount.\n\nimport { createContext, createElement, useContext, useEffect, useMemo, useState, useSyncExternalStore, type ReactElement, type ReactNode } from \"react\";\nimport { FlagsClient, type EvaluationDetails, type FlagsClientOptions, type FlagsSnapshot, type FlagValue, type SettingValue, type UserInput } from \"@productcraft/flags\";\n\n/** A default value: booleans, strings and numbers keep their type; objects, arrays and `null` read json settings. */\nexport type FlagDefault = SettingValue | object | null;\n\nexport interface FlagsProviderProps {\n /** A FlagsClient you built and own (the provider never closes it). Give this or `options`, and keep to that choice for the provider's lifetime. */\n client?: FlagsClient;\n /** Options for a FlagsClient the provider builds on mount and closes on unmount. On the server it never downloads. */\n options?: FlagsClientOptions;\n /** A config JSON, usually `snapshot().toJSON()` from the server: the first render (server and hydration) evaluates it. */\n bootstrap?: string | object | null;\n /** The user hooks evaluate for when a hook passes none. */\n user?: UserInput;\n children?: ReactNode;\n}\n\ninterface Ctx {\n client: FlagsClient;\n store: Store;\n user: UserInput | undefined;\n}\n\nconst FlagsContext = createContext<Ctx | null>(null);\nconst onServer = (): boolean => typeof (globalThis as { window?: unknown }).window === \"undefined\";\n\n/** The snapshot hooks read, replaced only when the config or the client state really changed. */\nclass Store {\n #snap: FlagsSnapshot;\n #state: string;\n readonly #listeners = new Set<() => void>();\n /** What the server rendered: the bootstrap when there is one. */\n readonly server: FlagsSnapshot;\n readonly client: FlagsClient;\n\n constructor(client: FlagsClient, bootstrap: string | object | null | undefined) {\n this.client = client;\n this.#snap = client.snapshot();\n this.#state = client.state;\n this.server = bootstrap == null ? this.#snap : offlineSnapshot(bootstrap);\n }\n\n readonly subscribe = (listener: () => void): (() => void) => {\n this.#listeners.add(listener);\n return () => this.#listeners.delete(listener);\n };\n\n readonly get = (): FlagsSnapshot => this.#snap;\n\n readonly ready = (): boolean => this.client.state !== \"initializing\";\n\n /** Listens to the client; returns the function that stops. Picks up anything that changed before it listened. */\n attach(): () => void {\n const update = (): void => {\n const next = this.client.snapshot();\n if (next.toJSON() === this.#snap.toJSON() && this.client.state === this.#state) return;\n this.#snap = next;\n this.#state = this.client.state;\n for (const l of [...this.#listeners]) l();\n };\n const offs = [this.client.on(\"configChanged\", update), this.client.on(\"ready\", update), this.client.on(\"error\", update)];\n update();\n return () => offs.forEach((off) => off());\n }\n}\n\nfunction offlineSnapshot(bootstrap: string | object): FlagsSnapshot {\n const c = new FlagsClient({ bootstrap, offline: true, logger: null });\n c.close();\n return c.snapshot();\n}\n\nfunction build(options: FlagsClientOptions, bootstrap: string | object | null | undefined): FlagsClient {\n return new FlagsClient({ ...options, bootstrap: bootstrap ?? options.bootstrap, ...(onServer() ? { offline: true } : null) });\n}\n\n/**\n * Makes a FlagsClient available to the hooks below it.\n * @example\n * ```tsx\n * <FlagsProvider options={{ sdkKey: \"pcf_sdk_…\" }} user={{ identifier: user.id }}>\n * <App />\n * </FlagsProvider>\n * ```\n */\nexport function FlagsProvider(props: FlagsProviderProps): ReactElement {\n const { client: given, options, bootstrap, user, children } = props;\n if (!given && !options) throw new Error(\"<FlagsProvider> needs a `client` or `options`\");\n const [owned, setOwned] = useState<FlagsClient | null>(() => (given ? null : build(options!, bootstrap)));\n useEffect(() => {\n if (given || !owned) return;\n // React StrictMode unmounts and remounts once in development: replace the client that unmount closed.\n if (owned.state === \"closed\") return void setOwned(build(options!, bootstrap));\n return () => owned.close();\n // `options` and `bootstrap` are read once, like useState's initial value.\n }, [given, owned]);\n const client = given ?? owned!;\n const store = useMemo(() => new Store(client, bootstrap), [client]);\n useEffect(() => store.attach(), [store]);\n const value = useMemo<Ctx>(() => ({ client, store, user }), [client, store, user]);\n return createElement(FlagsContext.Provider, { value }, children);\n}\n\nfunction useFlagsContext(hook: string): Ctx {\n const ctx = useContext(FlagsContext);\n if (!ctx) throw new Error(`${hook}() must be called inside <FlagsProvider>`);\n return ctx;\n}\n\n/** A stable dependency for a default or user that callers usually write inline. */\nconst dep = (v: unknown): unknown => (v !== null && typeof v === \"object\" ? JSON.stringify(v) : v);\n\n/** The value of a flag or setting, evaluated locally; `defaultValue` until a config is there, or on any failure. Re-renders when the config changes. */\nexport function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): FlagValue<D> {\n return useFlagDetails(key, defaultValue, user).value;\n}\n\n/** The value plus why (reason, matched rule or split, error code, revision). */\nexport function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): EvaluationDetails<FlagValue<D>> {\n const { store, user: fallback } = useFlagsContext(\"useFlagDetails\");\n const snap = useSyncExternalStore(store.subscribe, store.get, () => store.server);\n const u = user ?? fallback;\n return useMemo(() => snap.getValueDetails(key, defaultValue, u), [snap, key, dep(defaultValue), dep(u)]);\n}\n\n/** The provider's FlagsClient (for `getAllValues`, `forceRefresh`, events). */\nexport function useFlagsClient(): FlagsClient {\n return useFlagsContext(\"useFlagsClient\").client;\n}\n\n/** False until the client's start-up settled (first download, or `maxInitWaitSeconds`); always false while rendering on the server. */\nexport function useFlagsReady(): boolean {\n const { store } = useFlagsContext(\"useFlagsReady\");\n return useSyncExternalStore(store.subscribe, store.ready, () => false);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,mBAAgJ;AAChJ,mBAAoJ;AAuBpJ,IAAM,mBAAe,4BAA0B,IAAI;AACnD,IAAM,WAAW,MAAe,OAAQ,WAAoC,WAAW;AAGvF,IAAM,QAAN,MAAY;AAAA,EACV;AAAA,EACA;AAAA,EACS,aAAa,oBAAI,IAAgB;AAAA;AAAA,EAEjC;AAAA,EACA;AAAA,EAET,YAAY,QAAqB,WAA+C;AAC9E,SAAK,SAAS;AACd,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,SAAS,OAAO;AACrB,SAAK,SAAS,aAAa,OAAO,KAAK,QAAQ,gBAAgB,SAAS;AAAA,EAC1E;AAAA,EAES,YAAY,CAAC,aAAuC;AAC3D,SAAK,WAAW,IAAI,QAAQ;AAC5B,WAAO,MAAM,KAAK,WAAW,OAAO,QAAQ;AAA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AM,OAAO,MAAM,KAAK;AACvE;","names":[]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ReactNode, ReactElement } from 'react';
2
- import { SettingValue, FlagsClient, FlagsClientOptions, User, FlagValue, EvaluationDetails } from '@productcraft/flags';
2
+ import { SettingValue, FlagsClient, FlagsClientOptions, UserInput, FlagValue, EvaluationDetails } from '@productcraft/flags';
3
3
 
4
4
  /** A default value: booleans, strings and numbers keep their type; objects, arrays and `null` read json settings. */
5
5
  type FlagDefault = SettingValue | object | null;
@@ -11,7 +11,7 @@ interface FlagsProviderProps {
11
11
  /** A config JSON, usually `snapshot().toJSON()` from the server: the first render (server and hydration) evaluates it. */
12
12
  bootstrap?: string | object | null;
13
13
  /** The user hooks evaluate for when a hook passes none. */
14
- user?: User;
14
+ user?: UserInput;
15
15
  children?: ReactNode;
16
16
  }
17
17
  /**
@@ -25,9 +25,9 @@ interface FlagsProviderProps {
25
25
  */
26
26
  declare function FlagsProvider(props: FlagsProviderProps): ReactElement;
27
27
  /** The value of a flag or setting, evaluated locally; `defaultValue` until a config is there, or on any failure. Re-renders when the config changes. */
28
- declare function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): FlagValue<D>;
28
+ declare function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): FlagValue<D>;
29
29
  /** The value plus why (reason, matched rule or split, error code, revision). */
30
- declare function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): EvaluationDetails<FlagValue<D>>;
30
+ declare function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): EvaluationDetails<FlagValue<D>>;
31
31
  /** The provider's FlagsClient (for `getAllValues`, `forceRefresh`, events). */
32
32
  declare function useFlagsClient(): FlagsClient;
33
33
  /** False until the client's start-up settled (first download, or `maxInitWaitSeconds`); always false while rendering on the server. */
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ReactNode, ReactElement } from 'react';
2
- import { SettingValue, FlagsClient, FlagsClientOptions, User, FlagValue, EvaluationDetails } from '@productcraft/flags';
2
+ import { SettingValue, FlagsClient, FlagsClientOptions, UserInput, FlagValue, EvaluationDetails } from '@productcraft/flags';
3
3
 
4
4
  /** A default value: booleans, strings and numbers keep their type; objects, arrays and `null` read json settings. */
5
5
  type FlagDefault = SettingValue | object | null;
@@ -11,7 +11,7 @@ interface FlagsProviderProps {
11
11
  /** A config JSON, usually `snapshot().toJSON()` from the server: the first render (server and hydration) evaluates it. */
12
12
  bootstrap?: string | object | null;
13
13
  /** The user hooks evaluate for when a hook passes none. */
14
- user?: User;
14
+ user?: UserInput;
15
15
  children?: ReactNode;
16
16
  }
17
17
  /**
@@ -25,9 +25,9 @@ interface FlagsProviderProps {
25
25
  */
26
26
  declare function FlagsProvider(props: FlagsProviderProps): ReactElement;
27
27
  /** The value of a flag or setting, evaluated locally; `defaultValue` until a config is there, or on any failure. Re-renders when the config changes. */
28
- declare function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): FlagValue<D>;
28
+ declare function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): FlagValue<D>;
29
29
  /** The value plus why (reason, matched rule or split, error code, revision). */
30
- declare function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): EvaluationDetails<FlagValue<D>>;
30
+ declare function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): EvaluationDetails<FlagValue<D>>;
31
31
  /** The provider's FlagsClient (for `getAllValues`, `forceRefresh`, events). */
32
32
  declare function useFlagsClient(): FlagsClient;
33
33
  /** False until the client's start-up settled (first download, or `maxInitWaitSeconds`); always false while rendering on the server. */
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/provider.ts"],"sourcesContent":["// React bindings for FlagsClient (task 019, design 8.1): one FlagsClient per provider, hooks that\n// read a FlagsSnapshot synchronously, and re-renders on config changes via useSyncExternalStore.\n// SSR: the server renders from `bootstrap` (a `snapshot().toJSON()`), and hydration reads the same\n// bootstrap, so the first client render matches the server HTML. Downloads start after mount.\n\nimport { createContext, createElement, useContext, useEffect, useMemo, useState, useSyncExternalStore, type ReactElement, type ReactNode } from \"react\";\nimport { FlagsClient, type EvaluationDetails, type FlagsClientOptions, type FlagsSnapshot, type FlagValue, type SettingValue, type User } from \"@productcraft/flags\";\n\n/** A default value: booleans, strings and numbers keep their type; objects, arrays and `null` read json settings. */\nexport type FlagDefault = SettingValue | object | null;\n\nexport interface FlagsProviderProps {\n /** A FlagsClient you built and own (the provider never closes it). Give this or `options`, and keep to that choice for the provider's lifetime. */\n client?: FlagsClient;\n /** Options for a FlagsClient the provider builds on mount and closes on unmount. On the server it never downloads. */\n options?: FlagsClientOptions;\n /** A config JSON, usually `snapshot().toJSON()` from the server: the first render (server and hydration) evaluates it. */\n bootstrap?: string | object | null;\n /** The user hooks evaluate for when a hook passes none. */\n user?: User;\n children?: ReactNode;\n}\n\ninterface Ctx {\n client: FlagsClient;\n store: Store;\n user: User | undefined;\n}\n\nconst FlagsContext = createContext<Ctx | null>(null);\nconst onServer = (): boolean => typeof (globalThis as { window?: unknown }).window === \"undefined\";\n\n/** The snapshot hooks read, replaced only when the config or the client state really changed. */\nclass Store {\n #snap: FlagsSnapshot;\n #state: string;\n readonly #listeners = new Set<() => void>();\n /** What the server rendered: the bootstrap when there is one. */\n readonly server: FlagsSnapshot;\n readonly client: FlagsClient;\n\n constructor(client: FlagsClient, bootstrap: string | object | null | undefined) {\n this.client = client;\n this.#snap = client.snapshot();\n this.#state = client.state;\n this.server = bootstrap == null ? this.#snap : offlineSnapshot(bootstrap);\n }\n\n readonly subscribe = (listener: () => void): (() => void) => {\n this.#listeners.add(listener);\n return () => this.#listeners.delete(listener);\n };\n\n readonly get = (): FlagsSnapshot => this.#snap;\n\n readonly ready = (): boolean => this.client.state !== \"initializing\";\n\n /** Listens to the client; returns the function that stops. Picks up anything that changed before it listened. */\n attach(): () => void {\n const update = (): void => {\n const next = this.client.snapshot();\n if (next.toJSON() === this.#snap.toJSON() && this.client.state === this.#state) return;\n this.#snap = next;\n this.#state = this.client.state;\n for (const l of [...this.#listeners]) l();\n };\n const offs = [this.client.on(\"configChanged\", update), this.client.on(\"ready\", update), this.client.on(\"error\", update)];\n update();\n return () => offs.forEach((off) => off());\n }\n}\n\nfunction offlineSnapshot(bootstrap: string | object): FlagsSnapshot {\n const c = new FlagsClient({ bootstrap, offline: true, logger: null });\n c.close();\n return c.snapshot();\n}\n\nfunction build(options: FlagsClientOptions, bootstrap: string | object | null | undefined): FlagsClient {\n return new FlagsClient({ ...options, bootstrap: bootstrap ?? options.bootstrap, ...(onServer() ? { offline: true } : null) });\n}\n\n/**\n * Makes a FlagsClient available to the hooks below it.\n * @example\n * ```tsx\n * <FlagsProvider options={{ sdkKey: \"pcf_sdk_…\" }} user={{ identifier: user.id }}>\n * <App />\n * </FlagsProvider>\n * ```\n */\nexport function FlagsProvider(props: FlagsProviderProps): ReactElement {\n const { client: given, options, bootstrap, user, children } = props;\n if (!given && !options) throw new Error(\"<FlagsProvider> needs a `client` or `options`\");\n const [owned, setOwned] = useState<FlagsClient | null>(() => (given ? null : build(options!, bootstrap)));\n useEffect(() => {\n if (given || !owned) return;\n // React StrictMode unmounts and remounts once in development: replace the client that unmount closed.\n if (owned.state === \"closed\") return void setOwned(build(options!, bootstrap));\n return () => owned.close();\n // `options` and `bootstrap` are read once, like useState's initial value.\n }, [given, owned]);\n const client = given ?? owned!;\n const store = useMemo(() => new Store(client, bootstrap), [client]);\n useEffect(() => store.attach(), [store]);\n const value = useMemo<Ctx>(() => ({ client, store, user }), [client, store, user]);\n return createElement(FlagsContext.Provider, { value }, children);\n}\n\nfunction useFlagsContext(hook: string): Ctx {\n const ctx = useContext(FlagsContext);\n if (!ctx) throw new Error(`${hook}() must be called inside <FlagsProvider>`);\n return ctx;\n}\n\n/** A stable dependency for a default or user that callers usually write inline. */\nconst dep = (v: unknown): unknown => (v !== null && typeof v === \"object\" ? JSON.stringify(v) : v);\n\n/** The value of a flag or setting, evaluated locally; `defaultValue` until a config is there, or on any failure. Re-renders when the config changes. */\nexport function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): FlagValue<D> {\n return useFlagDetails(key, defaultValue, user).value;\n}\n\n/** The value plus why (reason, matched rule or split, error code, revision). */\nexport function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: User | null): EvaluationDetails<FlagValue<D>> {\n const { store, user: fallback } = useFlagsContext(\"useFlagDetails\");\n const snap = useSyncExternalStore(store.subscribe, store.get, () => store.server);\n const u = user ?? fallback;\n return useMemo(() => snap.getValueDetails(key, defaultValue, u), [snap, key, dep(defaultValue), dep(u)]);\n}\n\n/** The provider's FlagsClient (for `getAllValues`, `forceRefresh`, events). */\nexport function useFlagsClient(): FlagsClient {\n return useFlagsContext(\"useFlagsClient\").client;\n}\n\n/** False until the client's start-up settled (first download, or `maxInitWaitSeconds`); always false while rendering on the server. */\nexport function useFlagsReady(): boolean {\n const { store } = useFlagsContext(\"useFlagsReady\");\n return useSyncExternalStore(store.subscribe, store.ready, () => false);\n}\n"],"mappings":";;;AAKA,SAAS,eAAe,eAAe,YAAY,WAAW,SAAS,UAAU,4BAA+D;AAChJ,SAAS,mBAAsI;AAuB/I,IAAM,eAAe,cAA0B,IAAI;AACnD,IAAM,WAAW,MAAe,OAAQ,WAAoC,WAAW;AAGvF,IAAM,QAAN,MAAY;AAAA,EACV;AAAA,EACA;AAAA,EACS,aAAa,oBAAI,IAAgB;AAAA;AAAA,EAEjC;AAAA,EACA;AAAA,EAET,YAAY,QAAqB,WAA+C;AAC9E,SAAK,SAAS;AACd,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,SAAS,OAAO;AACrB,SAAK,SAAS,aAAa,OAAO,KAAK,QAAQ,gBAAgB,SAAS;AAAA,EAC1E;AAAA,EAES,YAAY,CAAC,aAAuC;AAC3D,SAAK,WAAW,IAAI,QAAQ;AAC5B,WAAO,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qBAAqB,MAAM,WAAW,MAAM,OAAO,MAAM,KAAK;AACvE;","names":[]}
1
+ {"version":3,"sources":["../src/provider.ts"],"sourcesContent":["// React bindings for FlagsClient (task 019, design 8.1): one FlagsClient per provider, hooks that\n// read a FlagsSnapshot synchronously, and re-renders on config changes via useSyncExternalStore.\n// SSR: the server renders from `bootstrap` (a `snapshot().toJSON()`), and hydration reads the same\n// bootstrap, so the first client render matches the server HTML. Downloads start after mount.\n\nimport { createContext, createElement, useContext, useEffect, useMemo, useState, useSyncExternalStore, type ReactElement, type ReactNode } from \"react\";\nimport { FlagsClient, type EvaluationDetails, type FlagsClientOptions, type FlagsSnapshot, type FlagValue, type SettingValue, type UserInput } from \"@productcraft/flags\";\n\n/** A default value: booleans, strings and numbers keep their type; objects, arrays and `null` read json settings. */\nexport type FlagDefault = SettingValue | object | null;\n\nexport interface FlagsProviderProps {\n /** A FlagsClient you built and own (the provider never closes it). Give this or `options`, and keep to that choice for the provider's lifetime. */\n client?: FlagsClient;\n /** Options for a FlagsClient the provider builds on mount and closes on unmount. On the server it never downloads. */\n options?: FlagsClientOptions;\n /** A config JSON, usually `snapshot().toJSON()` from the server: the first render (server and hydration) evaluates it. */\n bootstrap?: string | object | null;\n /** The user hooks evaluate for when a hook passes none. */\n user?: UserInput;\n children?: ReactNode;\n}\n\ninterface Ctx {\n client: FlagsClient;\n store: Store;\n user: UserInput | undefined;\n}\n\nconst FlagsContext = createContext<Ctx | null>(null);\nconst onServer = (): boolean => typeof (globalThis as { window?: unknown }).window === \"undefined\";\n\n/** The snapshot hooks read, replaced only when the config or the client state really changed. */\nclass Store {\n #snap: FlagsSnapshot;\n #state: string;\n readonly #listeners = new Set<() => void>();\n /** What the server rendered: the bootstrap when there is one. */\n readonly server: FlagsSnapshot;\n readonly client: FlagsClient;\n\n constructor(client: FlagsClient, bootstrap: string | object | null | undefined) {\n this.client = client;\n this.#snap = client.snapshot();\n this.#state = client.state;\n this.server = bootstrap == null ? this.#snap : offlineSnapshot(bootstrap);\n }\n\n readonly subscribe = (listener: () => void): (() => void) => {\n this.#listeners.add(listener);\n return () => this.#listeners.delete(listener);\n };\n\n readonly get = (): FlagsSnapshot => this.#snap;\n\n readonly ready = (): boolean => this.client.state !== \"initializing\";\n\n /** Listens to the client; returns the function that stops. Picks up anything that changed before it listened. */\n attach(): () => void {\n const update = (): void => {\n const next = this.client.snapshot();\n if (next.toJSON() === this.#snap.toJSON() && this.client.state === this.#state) return;\n this.#snap = next;\n this.#state = this.client.state;\n for (const l of [...this.#listeners]) l();\n };\n const offs = [this.client.on(\"configChanged\", update), this.client.on(\"ready\", update), this.client.on(\"error\", update)];\n update();\n return () => offs.forEach((off) => off());\n }\n}\n\nfunction offlineSnapshot(bootstrap: string | object): FlagsSnapshot {\n const c = new FlagsClient({ bootstrap, offline: true, logger: null });\n c.close();\n return c.snapshot();\n}\n\nfunction build(options: FlagsClientOptions, bootstrap: string | object | null | undefined): FlagsClient {\n return new FlagsClient({ ...options, bootstrap: bootstrap ?? options.bootstrap, ...(onServer() ? { offline: true } : null) });\n}\n\n/**\n * Makes a FlagsClient available to the hooks below it.\n * @example\n * ```tsx\n * <FlagsProvider options={{ sdkKey: \"pcf_sdk_…\" }} user={{ identifier: user.id }}>\n * <App />\n * </FlagsProvider>\n * ```\n */\nexport function FlagsProvider(props: FlagsProviderProps): ReactElement {\n const { client: given, options, bootstrap, user, children } = props;\n if (!given && !options) throw new Error(\"<FlagsProvider> needs a `client` or `options`\");\n const [owned, setOwned] = useState<FlagsClient | null>(() => (given ? null : build(options!, bootstrap)));\n useEffect(() => {\n if (given || !owned) return;\n // React StrictMode unmounts and remounts once in development: replace the client that unmount closed.\n if (owned.state === \"closed\") return void setOwned(build(options!, bootstrap));\n return () => owned.close();\n // `options` and `bootstrap` are read once, like useState's initial value.\n }, [given, owned]);\n const client = given ?? owned!;\n const store = useMemo(() => new Store(client, bootstrap), [client]);\n useEffect(() => store.attach(), [store]);\n const value = useMemo<Ctx>(() => ({ client, store, user }), [client, store, user]);\n return createElement(FlagsContext.Provider, { value }, children);\n}\n\nfunction useFlagsContext(hook: string): Ctx {\n const ctx = useContext(FlagsContext);\n if (!ctx) throw new Error(`${hook}() must be called inside <FlagsProvider>`);\n return ctx;\n}\n\n/** A stable dependency for a default or user that callers usually write inline. */\nconst dep = (v: unknown): unknown => (v !== null && typeof v === \"object\" ? JSON.stringify(v) : v);\n\n/** The value of a flag or setting, evaluated locally; `defaultValue` until a config is there, or on any failure. Re-renders when the config changes. */\nexport function useFlag<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): FlagValue<D> {\n return useFlagDetails(key, defaultValue, user).value;\n}\n\n/** The value plus why (reason, matched rule or split, error code, revision). */\nexport function useFlagDetails<D extends FlagDefault>(key: string, defaultValue: D, user?: UserInput | null): EvaluationDetails<FlagValue<D>> {\n const { store, user: fallback } = useFlagsContext(\"useFlagDetails\");\n const snap = useSyncExternalStore(store.subscribe, store.get, () => store.server);\n const u = user ?? fallback;\n return useMemo(() => snap.getValueDetails(key, defaultValue, u), [snap, key, dep(defaultValue), dep(u)]);\n}\n\n/** The provider's FlagsClient (for `getAllValues`, `forceRefresh`, events). */\nexport function useFlagsClient(): FlagsClient {\n return useFlagsContext(\"useFlagsClient\").client;\n}\n\n/** False until the client's start-up settled (first download, or `maxInitWaitSeconds`); always false while rendering on the server. */\nexport function useFlagsReady(): boolean {\n const { store } = useFlagsContext(\"useFlagsReady\");\n return useSyncExternalStore(store.subscribe, store.ready, () => false);\n}\n"],"mappings":";;;AAKA,SAAS,eAAe,eAAe,YAAY,WAAW,SAAS,UAAU,4BAA+D;AAChJ,SAAS,mBAA2I;AAuBpJ,IAAM,eAAe,cAA0B,IAAI;AACnD,IAAM,WAAW,MAAe,OAAQ,WAAoC,WAAW;AAGvF,IAAM,QAAN,MAAY;AAAA,EACV;AAAA,EACA;AAAA,EACS,aAAa,oBAAI,IAAgB;AAAA;AAAA,EAEjC;AAAA,EACA;AAAA,EAET,YAAY,QAAqB,WA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,cAAc,CAAC,GAAG,CAAC,MAAM,KAAK,IAAI,YAAY,GAAG,IAAI,CAAC,CAAC,CAAC;AACzG;AAGO,SAAS,iBAA8B;AAC5C,SAAO,gBAAgB,gBAAgB,EAAE;AAC3C;AAGO,SAAS,gBAAyB;AACvC,QAAM,EAAE,MAAM,IAAI,gBAAgB,eAAe;AACjD,SAAO,qBAAqB,MAAM,WAAW,MAAM,OAAO,MAAM,KAAK;AACvE;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@productcraft/flags-react",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "React bindings for ProductCraft Flags: FlagsProvider, useFlag and SSR bootstrap (Next.js App and Pages Router) over FlagsClient's local evaluation. A key used in a browser or mobile app makes its whole config public, so client flags belong in their own config with no internal rules.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -52,7 +52,7 @@
52
52
  "provenance": true
53
53
  },
54
54
  "peerDependencies": {
55
- "@productcraft/flags": "^0.1.0 || ^0.2.0 || ^0.3.0 || ^0.4.0 || ^1.0.0",
55
+ "@productcraft/flags": "^0.1.0 || ^0.2.0 || ^0.3.0 || ^0.4.0 || ^0.5.0 || ^1.0.0",
56
56
  "react": ">=18.0.0"
57
57
  },
58
58
  "devDependencies": {
@@ -61,7 +61,7 @@
61
61
  "jsdom": "31.0.0",
62
62
  "react": "19.3.1",
63
63
  "react-dom": "19.3.1",
64
- "@productcraft/flags": "0.4.0"
64
+ "@productcraft/flags": "0.5.0"
65
65
  },
66
66
  "scripts": {
67
67
  "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup",