Skip to content

Commit

Permalink
Merge pull request launchdarkly-labs#5 from launchdarkly-labs/subscri…
Browse files Browse the repository at this point in the history
…beToFlagChanges

Add Subscribe to flag updates
  • Loading branch information
bjlagman12 committed Jun 16, 2022
2 parents 8de49eb + 375a0e8 commit f836ddb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
45 changes: 24 additions & 21 deletions src/client/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { Component, PropsWithChildren } from 'react';
import { initialize, LDOptions } from 'launchdarkly-js-client-sdk';
import { ProviderConfig } from './types';
import { initialize, LDOptions, LDClient, LDFlagChangeset, LDFlagSet } from 'launchdarkly-js-client-sdk';
import { ProviderConfig, defaultReactOptions } from './types';
import { Provider, LDContext as HocState } from './context';
import { camelCaseKeys } from './utils';
import { camelCaseKeys, getFlattenedFlagsFromChangeset } from './utils';

// eslint-disable-next-line @typescript-eslint/ban-types
class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState> {
Expand All @@ -13,26 +13,29 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState>
const { clientSideID } = props;

console.log(`initializing ld client with ${clientSideID}...`);
let ldClient;

if (typeof window !== 'undefined') {
const options = {
bootstrap: window.ssrFlags,
};
ldClient = initialize(clientSideID, { anonymous: true }, options);
this.state = {
flags: camelCaseKeys(options.bootstrap),
ldClient,
};
} else {
console.error(`Fix LDProvider to work on the server`);
//TODO: ldClient = this.importInitServerSdk(props.sdkKey, options);
}
const options = {
bootstrap: window.ssrFlags,
};
const ldClient = initialize(clientSideID, { anonymous: true }, options);
this.state = {
flags: camelCaseKeys(options.bootstrap),
ldClient,
};
}

async importInitServerSdk(clientSideID: string, options: LDOptions) {
const { initServerSdk } = await import('../server');
return initServerSdk(clientSideID, options);
componentDidMount() {
this.subscribeToChanges()
}


subscribeToChanges = () => {
const { flags: targetFlags } = this.state;
this.state.ldClient.on('change', (changes: LDFlagChangeset) => {
const flattened: LDFlagSet = getFlattenedFlagsFromChangeset(changes, targetFlags);
if (Object.keys(flattened).length > 0) {
this.setState(({ flags }) => ({ flags: { ...flags, ...flattened } }));
}
});
}

render() {
Expand Down
5 changes: 3 additions & 2 deletions src/client/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ export const getFlattenedFlagsFromChangeset = (
): LDFlagSet => {
const flattened: LDFlagSet = {};
for (const key in changes) {
if (!targetFlags || targetFlags[key] !== undefined) {

const flagKey = camelCase(key);
if (!targetFlags || targetFlags[flagKey] !== undefined) {
// tslint:disable-next-line:no-unsafe-any
const flagKey = camelCase(key);
flattened[flagKey] = changes[key].current;
}
}
Expand Down

0 comments on commit f836ddb

Please sign in to comment.