Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for client-side prerequisite events. #112

Merged
merged 7 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/__tests__/LDClient-events-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this ts-nocheck as typescript checking in eslint isn't happy in this file. Nothing changed, but the typescript version was updated. Basically just affects the IDE.

import * as messages from '../messages';

import { withCloseable, sleepAsync } from 'launchdarkly-js-test-helpers';
Expand Down Expand Up @@ -253,6 +254,81 @@ describe('LDClient events', () => {
});
});

it('sends events for prerequisites', async () => {
const initData = makeBootstrap({
'is-prereq': {
value: true,
variation: 1,
reason: {
kind: 'FALLTHROUGH',
},
version: 1,
trackEvents: true,
trackReason: true,
},
'has-prereq-depth-1': {
value: true,
variation: 0,
prerequisites: ['is-prereq'],
reason: {
kind: 'FALLTHROUGH',
},
version: 4,
trackEvents: true,
trackReason: true,
},
'has-prereq-depth-2': {
value: true,
variation: 0,
prerequisites: ['has-prereq-depth-1'],
reason: {
kind: 'FALLTHROUGH',
},
version: 5,
trackEvents: true,
trackReason: true,
},
});
await withClientAndEventProcessor(user, { bootstrap: initData }, async (client, ep) => {
await client.waitForInitialization(5);
client.variation('has-prereq-depth-2', false);

// An identify event and 3 feature events.
expect(ep.events.length).toEqual(4);
expectIdentifyEvent(ep.events[0], user);
expect(ep.events[1]).toMatchObject({
kind: 'feature',
key: 'is-prereq',
variation: 1,
value: true,
version: 1,
reason: {
kind: 'FALLTHROUGH',
},
});
expect(ep.events[2]).toMatchObject({
kind: 'feature',
key: 'has-prereq-depth-1',
variation: 0,
value: true,
version: 4,
reason: {
kind: 'FALLTHROUGH',
},
});
expect(ep.events[3]).toMatchObject({
kind: 'feature',
key: 'has-prereq-depth-2',
variation: 0,
value: true,
version: 5,
reason: {
kind: 'FALLTHROUGH',
},
});
});
});

it('sends a feature event on receiving a new flag value', async () => {
const oldFlags = { foo: { value: 'a', variation: 1, version: 2, flagVersion: 2000 } };
const newFlags = { foo: { value: 'b', variation: 2, version: 3, flagVersion: 2001 } };
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {

function variationDetailInternal(key, defaultValue, sendEvent, includeReasonInEvent, isAllFlags) {
let detail;
let flag;

if (flags && utils.objectHasOwnProperty(flags, key) && flags[key] && !flags[key].deleted) {
const flag = flags[key];
flag = flags[key];
detail = getFlagDetail(flag);
if (flag.value === null || flag.value === undefined) {
detail.value = defaultValue;
Expand All @@ -320,6 +321,12 @@ function initialize(env, context, specifiedOptions, platform, extraOptionDefs) {
}

if (sendEvent) {
// An event will be send for each of these by virtue of sending events for all flags.
if (!isAllFlags) {
flag?.prerequisites?.forEach(key => {
variation(key, undefined);
});
}
sendFlagEvent(key, detail, defaultValue, includeReasonInEvent);
}

Expand Down
2 changes: 1 addition & 1 deletion typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ declare module 'launchdarkly-js-sdk-common' {

/**
* Describes the reason that a flag evaluation produced a particular value. This is
* part of the {@link LDEvaluationDetail} object returned by {@link LDClient.variationDetail]].
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed this when working on the docs for node-client-sdk. So a good chance to just fix that link.

* part of the {@link LDEvaluationDetail} object returned by {@link LDClient.variationDetail}.
*/
export interface LDEvaluationReason {
/**
Expand Down