diff --git a/packages/client-core/src/StatsigClientEventEmitter.ts b/packages/client-core/src/StatsigClientEventEmitter.ts index 6e4fee82..bd2385c7 100644 --- a/packages/client-core/src/StatsigClientEventEmitter.ts +++ b/packages/client-core/src/StatsigClientEventEmitter.ts @@ -1,11 +1,24 @@ import { DynamicConfig, Experiment, FeatureGate, Layer } from './StatsigTypes'; -export type StatsigLoadingStatus = - | 'Uninitialized' - | 'Loading' - | 'Ready' - | 'Error'; +export type StatsigLoadingStatus = 'Uninitialized' | 'Loading' | 'Ready'; +/** + * All the possible events emitted from a Statsig client. + * + * `status_change` - When the Statsig clients internal values change as the result of an initialize/update operation. + * + * `error` - When an unexpected error occurs within the Statsig client. + * + * `logs_flushed` - When queued StatsigEvents are flushed to Statsig servers. + * + * `gate_evaluation` - Fired when any gate is checked from the Statsig client. + * + * `dynamic_config_evaluation` - Fired when any dyanamic config is checked from the Statsig client. + * + * `experiment_evaluation` - Fired when any experiment is checked from the Statsig client. + * + * `layer_evaluation` - Fired when any layer is checked from the Statsig client. + */ export type StatsigClientEvent = | 'status_change' | 'error' @@ -15,6 +28,9 @@ export type StatsigClientEvent = | 'experiment_evaluation' | 'layer_evaluation'; +/** + * Type representing various events emitted by a Statsig client. + */ export type StatsigClientEventData = | { event: StatsigClientEvent; diff --git a/samples/react-native/src/app/ClientEventStreamExample.tsx b/samples/react-native/src/app/ClientEventStreamExample.tsx index 76d1b3f9..49cf6f22 100644 --- a/samples/react-native/src/app/ClientEventStreamExample.tsx +++ b/samples/react-native/src/app/ClientEventStreamExample.tsx @@ -47,14 +47,14 @@ function Content({ events }: { events: StatsigClientEventData[] }) { export default function ClientEventStreamExample(): JSX.Element { const [events, setEvents] = useState([]); useEffect(() => { - const onFlush = (data: StatsigClientEventData) => { + const onClientEvent = (data: StatsigClientEventData) => { setEvents((old) => [...old, data]); }; - client.on('*', onFlush); + client.on('*', onClientEvent); return () => { - client.off('*', onFlush); + client.off('*', onClientEvent); }; }, []); diff --git a/samples/react/src/samples/precomputed-client/sample-precomp-client-event.tsx b/samples/react/src/samples/precomputed-client/sample-precomp-client-event.tsx new file mode 100644 index 00000000..1f914772 --- /dev/null +++ b/samples/react/src/samples/precomputed-client/sample-precomp-client-event.tsx @@ -0,0 +1,27 @@ +/* eslint-disable no-console */ + +/* eslint-disable @typescript-eslint/no-unused-vars */ +// +import type { StatsigClientEventData } from '@statsig/client-core'; + +// +import { myStatsigClient } from './sample-precomp-instance'; + +// prettier-ignore +export default async function Sample(): Promise { +// +const onClientEvent = (data: StatsigClientEventData) => { + console.log("Statsig Logs", data); +}; + +// subscribe to an individual StatsigClientEvent +myStatsigClient.on('logs_flushed', onClientEvent); + +// or, subscribe to all StatsigClientEvents +myStatsigClient.on('*', onClientEvent); + +// then later, unsubscribe from the events +myStatsigClient.off('logs_flushed', onClientEvent); +myStatsigClient.off('*', onClientEvent); +// +}