Skip to content

Commit

Permalink
chore: client event snippet and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-statsig committed Mar 8, 2024
1 parent 576b75e commit 106eb5e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
26 changes: 21 additions & 5 deletions packages/client-core/src/StatsigClientEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions samples/react-native/src/app/ClientEventStreamExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ function Content({ events }: { events: StatsigClientEventData[] }) {
export default function ClientEventStreamExample(): JSX.Element {
const [events, setEvents] = useState<StatsigClientEventData[]>([]);
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);
};
}, []);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-console */

/* eslint-disable @typescript-eslint/no-unused-vars */
// <snippet>
import type { StatsigClientEventData } from '@statsig/client-core';

// </snippet>
import { myStatsigClient } from './sample-precomp-instance';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
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);
// </snippet>
}

0 comments on commit 106eb5e

Please sign in to comment.