Skip to content

Commit

Permalink
feat: Add support for client-side prerequisite events.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Oct 14, 2024
1 parent 7d49d9e commit 39a1155
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
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
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

0 comments on commit 39a1155

Please sign in to comment.