Skip to content

Commit

Permalink
Add U2C types and make minimal adjustments for type changes. (#58)
Browse files Browse the repository at this point in the history
* Add U2C types.

* Update typings.d.ts. Make minimal adjustments for changes to typings.
  • Loading branch information
kinyoklion authored Apr 18, 2022
1 parent b8f4b61 commit 1922560
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function Identity(initialUser, onChange) {
}
};

ident.getUser = function() {
ident.getContext = function() {
return user ? utils.clone(user) : null;
};

Expand Down
2 changes: 1 addition & 1 deletion src/PersistentFlagStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function PersistentFlagStore(storage, environment, hash, ident) {

function getFlagsKey() {
let key = '';
const user = ident.getUser();
const user = ident.getContext();
if (user) {
key = hash || utils.btoa(JSON.stringify(user));
}
Expand Down
2 changes: 1 addition & 1 deletion src/UserFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const utils = require('./utils');
function UserFilter(config) {
const filter = {};
const allAttributesPrivate = config.allAttributesPrivate;
const privateAttributeNames = config.privateAttributeNames || [];
const privateAttributeNames = config.privateAttributes || [];
const ignoreAttrs = { key: true, custom: true, anonymous: true };
const allowedTopLevelAttrs = {
key: true,
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/LDClient-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('LDClient', () => {
await withServers(async baseConfig => {
await withClient(numericUser, baseConfig, async client => {
await client.waitForInitialization();
expect(client.getUser()).toEqual(stringifiedNumericUser);
expect(client.getContext()).toEqual(stringifiedNumericUser);
});
});
});
Expand All @@ -139,14 +139,14 @@ describe('LDClient', () => {
await withClient(anonUser, baseConfig, async client0 => {
await client0.waitForInitialization();

generatedUser = client0.getUser();
generatedUser = client0.getContext();
expect(generatedUser.key).toEqual(expect.anything());
expect(generatedUser).toMatchObject(anonUser);
});
await withClient(anonUser, baseConfig, async client1 => {
await client1.waitForInitialization();

const newUser1 = client1.getUser();
const newUser1 = client1.getContext();
expect(newUser1).toEqual(generatedUser);
});
});
Expand All @@ -161,15 +161,15 @@ describe('LDClient', () => {
await withClient(anonUser, baseConfig, async client0 => {
await client0.waitForInitialization();

generatedUser = client0.getUser();
generatedUser = client0.getContext();
expect(generatedUser.key).toEqual(expect.anything());
expect(generatedUser).toMatchObject(anonUser);
});
await sleepAsync(100); // so that the time-based UUID algorithm will produce a different result below
await withClient(anonUser, baseConfig, async client1 => {
await client1.waitForInitialization();

const newUser1 = client1.getUser();
const newUser1 = client1.getContext();
expect(newUser1.key).toEqual(expect.anything());
expect(newUser1.key).not.toEqual(generatedUser.key);
expect(newUser1).toMatchObject(anonUser);
Expand Down Expand Up @@ -466,12 +466,12 @@ describe('LDClient', () => {

const identifyPromise = client.identify(user2);
await sleepAsync(100); // sleep to jump some async ticks
expect(client.getUser()).toEqual(user);
expect(client.getContext()).toEqual(user);

signal.add();
await identifyPromise;

expect(client.getUser()).toEqual(user2);
expect(client.getContext()).toEqual(user2);
});
});
});
Expand Down Expand Up @@ -533,7 +533,7 @@ describe('LDClient', () => {
const anonUser = { anonymous: true, country: 'US' };
await client.identify(anonUser);

const newUser = client.getUser();
const newUser = client.getContext();
expect(newUser.key).toEqual(expect.anything());
expect(newUser).toMatchObject(anonUser);
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/UserFilter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('UserFilter', () => {
});

it('hides some attributes if privateAttributeNames is set', () => {
const uf = UserFilter({ privateAttributeNames: ['firstName', 'bizzle'] });
const uf = UserFilter({ privateAttributes: ['firstName', 'bizzle'] });
expect(uf.filterUser(user)).toEqual(userWithSomeAttrsHidden);
});

Expand All @@ -77,7 +77,7 @@ describe('UserFilter', () => {
});

it('looks at both per-user privateAttrs and global config', () => {
const uf = UserFilter({ privateAttributeNames: ['firstName', 'bizzle'] });
const uf = UserFilter({ privateAttributes: ['firstName', 'bizzle'] });
expect(uf.filterUser(userSpecifyingOwnPrivateAttr)).toEqual(userWithAllAttrsHidden);
});

Expand Down
2 changes: 1 addition & 1 deletion src/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const baseOptionDefs = {
samplingInterval: { default: 0, minimum: 0 },
streamReconnectDelay: { default: 1000, minimum: 0 },
allAttributesPrivate: { default: false },
privateAttributeNames: { default: [] },
privateAttributes: { default: [] },
bootstrap: { type: 'string|object' },
diagnosticRecordingInterval: { default: 900000, minimum: 2000 },
diagnosticOptOut: { default: false },
Expand Down
24 changes: 12 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
}

function sendFlagEvent(key, detail, defaultValue, includeReason) {
const user = ident.getUser();
const user = ident.getContext();
const now = new Date();
const value = detail ? detail.value : null;
if (!options.allowFrequentDuplicateEvents) {
Expand Down Expand Up @@ -258,8 +258,8 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
);
}

function getUser() {
return ident.getUser();
function getContext() {
return ident.getContext();
}

function flush(onDone) {
Expand Down Expand Up @@ -360,7 +360,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
logger.warn(messages.unknownCustomEventKey(key));
}

const user = ident.getUser();
const user = ident.getContext();
const e = {
kind: 'custom',
key: key,
Expand All @@ -383,7 +383,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {

function connectStream() {
streamActive = true;
if (!ident.getUser()) {
if (!ident.getContext()) {
return;
}
const tryParseData = jsonData => {
Expand All @@ -394,16 +394,16 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
return undefined;
}
};
stream.connect(ident.getUser(), hash, {
stream.connect(ident.getContext(), hash, {
ping: function() {
logger.debug(messages.debugStreamPing());
const userAtTimeOfPingEvent = ident.getUser();
const userAtTimeOfPingEvent = ident.getContext();
requestor
.fetchFlagSettings(userAtTimeOfPingEvent, hash)
.then(requestedFlags => {
// Check whether the current user is still the same - we don't want to overwrite the flags if
// the application has called identify() while this request was in progress
if (utils.deepEquals(userAtTimeOfPingEvent, ident.getUser())) {
if (utils.deepEquals(userAtTimeOfPingEvent, ident.getContext())) {
replaceAllFlags(requestedFlags || {});
}
})
Expand Down Expand Up @@ -646,7 +646,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
if (storedFlags === null || storedFlags === undefined) {
flags = {};
return requestor
.fetchFlagSettings(ident.getUser(), hash)
.fetchFlagSettings(ident.getContext(), hash)
.then(requestedFlags => replaceAllFlags(requestedFlags || {}))
.then(signalSuccessfulInit)
.catch(err => {
Expand All @@ -661,7 +661,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
utils.onNextTick(signalSuccessfulInit);

return requestor
.fetchFlagSettings(ident.getUser(), hash)
.fetchFlagSettings(ident.getContext(), hash)
.then(requestedFlags => replaceAllFlags(requestedFlags))
.catch(err => emitter.maybeReportError(err));
}
Expand All @@ -670,7 +670,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {

function finishInitWithPolling() {
return requestor
.fetchFlagSettings(ident.getUser(), hash)
.fetchFlagSettings(ident.getContext(), hash)
.then(requestedFlags => {
flags = requestedFlags || {};
// Note, we don't need to call updateSettings here because local storage and change events are not relevant
Expand Down Expand Up @@ -751,7 +751,7 @@ function initialize(env, user, specifiedOptions, platform, extraOptionDefs) {
waitForInitialization: () => initializationStateTracker.getInitializationPromise(),
waitUntilReady: () => initializationStateTracker.getReadyPromise(),
identify: identify,
getUser: getUser,
getContext: getContext,
variation: variation,
variationDetail: variationDetail,
track: track,
Expand Down
9 changes: 3 additions & 6 deletions test-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as ld from 'launchdarkly-js-sdk-common';
var userWithKeyOnly: ld.LDUser = { key: 'user' };
var anonUserWithNoKey: ld.LDUser = { anonymous: true };
var anonUserWithKey: ld.LDUser = { key: 'anon-user', anonymous: true };
var user: ld.LDUser = {
var user: ld.LDContext = {
key: 'user',
secondary: 'otherkey',
name: 'name',
Expand Down Expand Up @@ -41,8 +41,7 @@ var allBaseOptions: ld.LDOptionsBase = {
evaluationReasons: true,
sendEvents: true,
allAttributesPrivate: true,
privateAttributeNames: [ 'x' ],
inlineUsersInEvents: true,
privateAttributes: [ 'x' ],
allowFrequentDuplicateEvents: true,
sendEventsOnlyForVariation: true,
flushInterval: 1,
Expand All @@ -59,9 +58,7 @@ client.identify(user).then(() => {});
client.identify(user, undefined, () => {});
client.identify(user, 'hash').then(() => {});

client.alias(user, anonUserWithKey);

var user: ld.LDUser = client.getUser();
var user: ld.LDContext = client.getContext();

client.flush(() => {});
client.flush().then(() => {});
Expand Down
Loading

0 comments on commit 1922560

Please sign in to comment.