Skip to content

Commit

Permalink
feat: add bootstrap init sample (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-statsig authored Mar 6, 2024
1 parent dc57c22 commit 240ec8d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
2 changes: 1 addition & 1 deletion samples/react/src/SamplesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ReactNode, useEffect } from 'react';
const SAMPLES = [
import('./samples/PrecomputedClientBasic'),
import('./samples/OnDeviceClientBasic'),
import('./samples/SynchronousInitialize'),
import('./samples/BootstrapInit'),
];

export default function SamplesPage(): ReactNode {
Expand Down
58 changes: 58 additions & 0 deletions samples/react/src/samples/BootstrapInit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable no-console */

/* eslint-disable @typescript-eslint/no-floating-promises */
import { DJB2 } from '@statsig/client-core';
// <snippet>
import {
BootstrapEvaluationsDataProvider,
PrecomputedEvaluationsClient,
} from '@statsig/precomputed-evaluations';

// </snippet>
import { STATSIG_CLIENT_KEY as YOUR_CLIENT_KEY } from '../Contants';

// prettier-ignore
export default async function Sample(): Promise<void> {
// <snippet>
const user = { userID: 'a-user' };

// Setup some Bootstrapped values
const bootstrapper = new BootstrapEvaluationsDataProvider();
bootstrapper.addDataForUser(YOUR_CLIENT_KEY, getStatsigJson(), user)

const options = {
dataProviders: [bootstrapper]
};
const client = new PrecomputedEvaluationsClient(YOUR_CLIENT_KEY, user, options);

client.initialize();

console.log("Statsig Status: ", client.loadingStatus); // prints: "Statsig Status: Ready"

const gate = client.getFeatureGate('a_gate');
console.log("a_gate source:", gate.source) // prints: "a_gate source: Bootstrap"

// </snippet>
}

function getStatsigJson(): string {
const hash = DJB2('a_gate');

return JSON.stringify({
__note: 'Sample Response using the SDK Demo project',
feature_gates: {
[hash]: {
name: hash,
value: true,
rule_id: '2QWhVkWdUEXR6Q3KYgV73O',
id_type: 'userID',
secondary_exposures: [],
},
},
dynamic_configs: {},
layer_configs: {},
has_updates: true,
time: 1705543730484,
hash_used: 'djb2',
});
}
31 changes: 0 additions & 31 deletions samples/react/src/samples/SynchronousInitialize.tsx

This file was deleted.

0 comments on commit 240ec8d

Please sign in to comment.