Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit c67a3ce

Browse files
committed
Remove user memoization
1 parent bec3b3f commit c67a3ce

File tree

4 files changed

+65
-252
lines changed

4 files changed

+65
-252
lines changed

packages/js-web-sdk/packages/js-web-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test": "tests"
1414
},
1515
"scripts": {
16-
"tsc": "tsc",
16+
"tsc": "rm -rf lib/ && tsc",
1717
"test": "karma start karma.conf.js --single-run",
1818
"test:watch": "karma start karma.conf.js",
1919
"build": "rm -rf dist/ && node ./scripts/build.js",

packages/js-web-sdk/packages/js-web-sdk/src/OptimizelySDKWrapper.ts

Lines changed: 48 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ export { VariableValuesObject, VariableValue }
1212

1313
type Partial<T> = { [P in keyof T]?: T[P] }
1414

15+
// TODO use optimizely.UserAttributes when this is fixed in
16+
// https://github.com/optimizely/javascript-sdk/issues/211
17+
type UserAttributes = {
18+
[attribute: string]: any
19+
}
20+
1521
export interface OptimizelySDKWrapperConfig extends Partial<optimizely.Config> {
1622
datafile?: OptimizelyDatafile
1723
sdkKey?: string
1824
UNSTABLE_datafileLoader?: ResourceLoader<OptimizelyDatafile>
1925

20-
attributes?: optimizely.UserAttributes
21-
UNSTABLE_attributesLoader?: ResourceLoader<optimizely.UserAttributes>
26+
attributes?: UserAttributes
27+
UNSTABLE_attributesLoader?: ResourceLoader<UserAttributes>
2228

2329
userId?: string
2430
UNSTABLE_userIdLoader?: ResourceLoader<UserId>
@@ -27,7 +33,7 @@ export interface OptimizelySDKWrapperConfig extends Partial<optimizely.Config> {
2733
type TrackEventCallArgs = [
2834
string,
2935
string | undefined,
30-
optimizely.UserAttributes | undefined,
36+
UserAttributes | undefined,
3137
optimizely.EventTags | undefined
3238
]
3339

@@ -42,7 +48,7 @@ export class OptimizelySDKWrapper {
4248

4349
public datafile: OptimizelyDatafile | null
4450
private userId: string | null
45-
private attributes: optimizely.UserAttributes
51+
private attributes: UserAttributes
4652

4753
private initialConfig: OptimizelySDKWrapperConfig
4854

@@ -117,40 +123,38 @@ export class OptimizelySDKWrapper {
117123
*
118124
*
119125
* @param {string} experimentKey
120-
* @param {string} [overrideUserId]
121-
* @param {optimizely.UserAttributes} [overrideAttributes]
126+
* @param {string} [userId]
127+
* @param {UserAttributes} [attributes]
122128
* @returns {(string | null)}
123129
* @memberof OptimizelySDKWrapper
124130
*/
125131
public activate(
126132
experimentKey: string,
127-
overrideUserId?: string,
128-
overrideAttributes?: optimizely.UserAttributes,
133+
userId: string,
134+
attributes?: UserAttributes,
129135
): string | null {
130136
if (!this.isInitialized) {
131137
return null
132138
}
133-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
134139
return this.instance.activate(experimentKey, userId, attributes)
135140
}
136141

137142
/**
138143
*
139144
* @param {string} experimentKey
140-
* @param {string} [overrideUserId]
141-
* @param {optimizely.UserAttributes} [overrideAttributes]
145+
* @param {string} [userId]
146+
* @param {UserAttributes} [attributes]
142147
* @returns {(string | null)}
143148
* @memberof OptimizelySDKWrapper
144149
*/
145150
public getVariation(
146151
experimentKey: string,
147-
overrideUserId?: string,
148-
overrideAttributes?: optimizely.UserAttributes,
152+
userId: string,
153+
attributes?: UserAttributes,
149154
): string | null {
150155
if (!this.isInitialized) {
151156
return null
152157
}
153-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
154158
return this.instance.getVariation(experimentKey, userId, attributes)
155159
}
156160

@@ -163,28 +167,21 @@ export class OptimizelySDKWrapper {
163167
* The first is a shortcut in the case where userId and attributes are stored on the SDK instance
164168
*
165169
* @param {string} eventKey
166-
* @param {(string | optimizely.EventTags)} [overrideUserId]
167-
* @param {optimizely.UserAttributes} [overrideAttributes]
170+
* @param {(string | optimizely.EventTags)} [userId]
171+
* @param {UserAttributes} [attributes]
168172
* @param {optimizely.EventTags} [eventTags]
169173
* @memberof OptimizelySDKWrapper
170174
*/
171175
public track(
172176
eventKey: string,
173-
overrideUserId?: string | optimizely.EventTags,
174-
overrideAttributes?: optimizely.UserAttributes,
177+
userId: string,
178+
attributes?: UserAttributes,
175179
eventTags?: optimizely.EventTags,
176180
): void {
177-
if (typeof overrideUserId !== 'undefined' && typeof overrideUserId !== 'string') {
178-
eventTags = overrideUserId
179-
overrideUserId = undefined
180-
overrideAttributes = undefined
181-
}
182-
183181
if (!this.isInitialized) {
184-
this.trackEventQueue.push([eventKey, overrideUserId, overrideAttributes, eventTags])
182+
this.trackEventQueue.push([eventKey, userId, attributes, eventTags])
185183
return
186184
}
187-
let [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
188185
this.instance.track(eventKey, userId, attributes, eventTags)
189186
}
190187

@@ -193,41 +190,39 @@ export class OptimizelySDKWrapper {
193190
* loaded, this will return `false`
194191
*
195192
* @param {string} feature
196-
* @param {string} [overrideUserId]
197-
* @param {optimizely.UserAttributes} [overrideAttributes]
193+
* @param {string} [userId]
194+
* @param {UserAttributes} [attributes]
198195
* @returns {boolean}
199196
* @memberof OptimizelySDKWrapper
200197
*/
201198
public isFeatureEnabled(
202199
feature: string,
203-
overrideUserId?: string,
204-
overrideAttributes?: optimizely.UserAttributes,
200+
userId: string,
201+
attributes?: UserAttributes,
205202
): boolean {
206203
if (!this.isInitialized) {
207204
return false
208205
}
209-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
210206
return this.instance.isFeatureEnabled(feature, userId, attributes)
211207
}
212208

213209
/**
214210
* Get all variables for a feature, regardless of the feature being enabled/disabled
215211
*
216212
* @param {string} feature
217-
* @param {string} [overrideUserId]
218-
* @param {optimizely.UserAttributes} [overrideAttributes]
213+
* @param {string} [userId]
214+
* @param {UserAttributes} [attributes]
219215
* @returns {VariableValuesObject}
220216
* @memberof OptimizelySDKWrapper
221217
*/
222218
public getFeatureVariables(
223219
feature: string,
224-
overrideUserId?: string,
225-
overrideAttributes?: optimizely.UserAttributes,
220+
userId: string,
221+
attributes?: UserAttributes,
226222
): VariableValuesObject {
227223
if (!this.isInitialized) {
228224
return {}
229225
}
230-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
231226
const variableDefs = this.getVariableDefsForFeature(feature)
232227
if (!variableDefs) {
233228
// TODO: error
@@ -261,130 +256,90 @@ export class OptimizelySDKWrapper {
261256
public getFeatureVariableString(
262257
feature: string,
263258
variable: string,
264-
overrideUserId?: string,
265-
overrideAttributes?: optimizely.UserAttributes,
259+
userId: string,
260+
attributes?: UserAttributes,
266261
): string | null {
267262
if (!this.isInitialized) {
268263
return null
269264
}
270-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
271265

272266
return this.instance.getFeatureVariableString(feature, variable, userId, attributes)
273267
}
274268

275269
public getFeatureVariableBoolean(
276270
feature: string,
277271
variable: string,
278-
overrideUserId?: string,
279-
overrideAttributes?: optimizely.UserAttributes,
272+
userId: string,
273+
attributes?: UserAttributes,
280274
): boolean | null {
281275
if (!this.isInitialized) {
282276
return null
283277
}
284-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
285278

286279
return this.instance.getFeatureVariableBoolean(feature, variable, userId, attributes)
287280
}
288281

289282
public getFeatureVariableInteger(
290283
feature: string,
291284
variable: string,
292-
overrideUserId?: string,
293-
overrideAttributes?: optimizely.UserAttributes,
285+
userId: string,
286+
attributes?: UserAttributes,
294287
): number | null {
295288
if (!this.isInitialized) {
296289
return null
297290
}
298-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
299291

300292
return this.instance.getFeatureVariableInteger(feature, variable, userId, attributes)
301293
}
302294

303295
public getFeatureVariableDouble(
304296
feature: string,
305297
variable: string,
306-
overrideUserId?: string,
307-
overrideAttributes?: optimizely.UserAttributes,
298+
userId: string,
299+
attributes?: UserAttributes,
308300
): number | null {
309301
if (!this.isInitialized) {
310302
return null
311303
}
312-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
313-
314304
return this.instance.getFeatureVariableDouble(feature, variable, userId, attributes)
315305
}
316306

317307
/**
318308
* Get an array of all enabled features
319309
*
320-
* @param {string} [overrideUserId]
321-
* @param {optimizely.UserAttributes} [overrideAttributes]
310+
* @param {string} [userId]
311+
* @param {UserAttributes} [attributes]
322312
* @returns {Array<string>}
323313
* @memberof OptimizelySDKWrapper
324314
*/
325-
public getEnabledFeatures(overrideUserId?: string, overrideAttributes?: optimizely.UserAttributes): Array<string> {
315+
public getEnabledFeatures(userId: string, attributes?: UserAttributes): Array<string> {
326316
if (!this.isInitialized) {
327317
return []
328318
}
329-
const [userId, attributes] = this.getUserIdAndAttributes(overrideUserId, overrideAttributes)
330319
return this.instance.getEnabledFeatures(userId, attributes)
331320
}
332321

333322
/**
334323
* @param {string} experiment
335-
* @param {string} [overrideUserId]
324+
* @param {string} [userId]
336325
* @returns {(string | null)}
337326
* @memberof OptimizelySDKWrapper
338327
*/
339-
public getForcedVariation(experiment: string, overrideUserId?: string): string | null {
340-
const [userId] = this.getUserIdAndAttributes(overrideUserId)
328+
public getForcedVariation(experiment: string, userId: string): string | null {
341329
return this.instance.getForcedVariation(experiment, userId)
342330
}
343331

344332
/**
345333
* @param {string} experiment
346-
* @param {string} overrideUserIdOrVariationKey
334+
* @param {string} userId
347335
* @param {string} [variationKey]
348336
* @returns {boolean}
349337
* @memberof OptimizelySDKWrapper
350338
*/
351-
public setForcedVariation(experiment: string, overrideUserIdOrVariationKey: string, variationKey?: string): boolean {
352-
if (typeof variationKey === 'undefined') {
353-
const [userId] = this.getUserIdAndAttributes()
354-
return this.instance.setForcedVariation(experiment, userId, overrideUserIdOrVariationKey)
355-
}
356-
357-
const [userId] = this.getUserIdAndAttributes(overrideUserIdOrVariationKey)
358-
339+
public setForcedVariation(experiment: string, userId: string, variationKey: string): boolean {
359340
return this.instance.setForcedVariation(experiment, userId, variationKey)
360341
}
361342

362-
protected getUserIdAndAttributes(
363-
overrideUserId?: string,
364-
overrideAttributes?: optimizely.UserAttributes,
365-
): [string, optimizely.UserAttributes] {
366-
let userId
367-
if (overrideUserId) {
368-
userId = overrideUserId
369-
} else if (this.userId) {
370-
userId = this.userId
371-
}
372-
373-
if (!userId) {
374-
// TODO make this a warning
375-
throw new Error('No userId supplied')
376-
}
377-
378-
// TODO store this as state when onInitialized is called
379-
let attributes = this.attributes
380-
if (overrideAttributes) {
381-
// should we override or merge attributes here
382-
attributes = overrideAttributes
383-
}
384-
385-
return [userId, attributes]
386-
}
387-
388343
protected getVariableDefsForFeature(feature: string): VariableDef[] | null {
389344
if (!this.datafile) {
390345
return null
@@ -405,8 +360,8 @@ export class OptimizelySDKWrapper {
405360
}
406361
}
407362

408-
private setupAttributesLoader(config: OptimizelySDKWrapperConfig): ResourceLoader<optimizely.UserAttributes> {
409-
let attributesLoader: ResourceLoader<optimizely.UserAttributes>
363+
private setupAttributesLoader(config: OptimizelySDKWrapperConfig): ResourceLoader<UserAttributes> {
364+
let attributesLoader: ResourceLoader<UserAttributes>
410365

411366
if (config.UNSTABLE_attributesLoader) {
412367
attributesLoader = config.UNSTABLE_attributesLoader

packages/js-web-sdk/packages/js-web-sdk/src/test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ async function testUrlLoad() {
3131
userId: 'user',
3232
})
3333
optimizely.track('foo', 'jordan')
34-
optimizely.track('foo', {
35-
revenue: 123
36-
})
3734
optimizely.track('foo', 'jordan', {
3835
plan_type: 'bronze'
3936
})

0 commit comments

Comments
 (0)