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

Commit 8e17f8d

Browse files
committed
Remove UserID / UserAttributes stuff from js-web-sdk
1 parent c67a3ce commit 8e17f8d

File tree

7 files changed

+64
-187
lines changed

7 files changed

+64
-187
lines changed

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

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import * as optimizely from '@optimizely/optimizely-sdk'
2-
import { OptimizelyDatafile, VariableValue, VariableValuesObject, VariableDef } from './Datafile'
3-
import { StaticUserIdLoader, UserId } from './UserIdLoaders'
2+
import {
3+
OptimizelyDatafile,
4+
VariableValue,
5+
VariableValuesObject,
6+
VariableDef,
7+
} from './Datafile'
48
import { find } from './utils'
59
import { ProvidedDatafileLoader, FetchUrlDatafileLoader } from './DatafileLoaders'
6-
import { ProvidedAttributesLoader } from './UserAttributesLoaders'
7-
import { ResourceLoader, ResourceManager } from './ResourceManager'
10+
import { ResourceLoader, Resource } from './ResourceManager'
811

912
// export types
1013
export { OptimizelyDatafile }
@@ -21,18 +24,11 @@ type UserAttributes = {
2124
export interface OptimizelySDKWrapperConfig extends Partial<optimizely.Config> {
2225
datafile?: OptimizelyDatafile
2326
sdkKey?: string
24-
UNSTABLE_datafileLoader?: ResourceLoader<OptimizelyDatafile>
25-
26-
attributes?: UserAttributes
27-
UNSTABLE_attributesLoader?: ResourceLoader<UserAttributes>
28-
29-
userId?: string
30-
UNSTABLE_userIdLoader?: ResourceLoader<UserId>
3127
}
3228

3329
type TrackEventCallArgs = [
3430
string,
35-
string | undefined,
31+
string,
3632
UserAttributes | undefined,
3733
optimizely.EventTags | undefined
3834
]
@@ -46,9 +42,8 @@ export class OptimizelySDKWrapper {
4642
public instance: optimizely.Client
4743
public isInitialized: boolean
4844

45+
public datafileResource: Resource<OptimizelyDatafile>
4946
public datafile: OptimizelyDatafile | null
50-
private userId: string | null
51-
private attributes: UserAttributes
5247

5348
private initialConfig: OptimizelySDKWrapperConfig
5449

@@ -57,8 +52,6 @@ export class OptimizelySDKWrapper {
5752
// This will be `datafile` and `attributes`
5853
private initializingPromise: Promise<any>
5954

60-
private resourceManager: ResourceManager
61-
6255
/**
6356
* Creates an instance of OptimizelySDKWrapper.
6457
* @param {OptimizelySDKWrapperConfig} [config={}]
@@ -70,20 +63,15 @@ export class OptimizelySDKWrapper {
7063
this.datafile = null
7164
this.trackEventQueue = []
7265

73-
this.resourceManager = new ResourceManager({
74-
datafile: this.setupDatafileLoader(config),
75-
attributes: this.setupAttributesLoader(config),
76-
userId: this.setupUserIdLoader(config),
77-
})
66+
this.datafileResource = this.setupDatafileResource(config)
7867

79-
if (this.resourceManager.allResourcesLoaded()) {
68+
if (this.datafileResource.hasLoaded) {
8069
this.onInitialized()
8170
this.initializingPromise = Promise.resolve()
8271
} else {
83-
this.initializingPromise = this.resourceManager.allResourcePromises()
84-
.then(() => {
85-
this.onInitialized();
86-
})
72+
this.initializingPromise = this.datafileResource.promise.then(() => {
73+
this.onInitialized()
74+
})
8775
}
8876
}
8977

@@ -92,7 +80,7 @@ export class OptimizelySDKWrapper {
9280
* Returns a promise where the resolved value is a boolean indicating whether
9381
* the optimizely instance has been initialized. This only is false when
9482
* you supply a timeout
95-
*
83+
9684
* @param {{ timeout?: number }} [config={}]
9785
* @returns {Promise<boolean>}
9886
* @memberof OptimizelySDKWrapper
@@ -231,21 +219,41 @@ export class OptimizelySDKWrapper {
231219

232220
const variableObj: VariableValuesObject = {}
233221
variableDefs.forEach(({ key, type }) => {
234-
switch(type) {
222+
switch (type) {
235223
case 'string':
236-
variableObj[key] = this.instance.getFeatureVariableString(feature, key, userId, attributes)
237-
break;
224+
variableObj[key] = this.instance.getFeatureVariableString(
225+
feature,
226+
key,
227+
userId,
228+
attributes,
229+
)
230+
break
238231

239232
case 'boolean':
240-
variableObj[key] = this.instance.getFeatureVariableBoolean(feature, key, userId, attributes)
233+
variableObj[key] = this.instance.getFeatureVariableBoolean(
234+
feature,
235+
key,
236+
userId,
237+
attributes,
238+
)
241239
break
242240

243241
case 'integer':
244-
variableObj[key] = this.instance.getFeatureVariableInteger(feature, key, userId, attributes)
242+
variableObj[key] = this.instance.getFeatureVariableInteger(
243+
feature,
244+
key,
245+
userId,
246+
attributes,
247+
)
245248
break
246249

247250
case 'double':
248-
variableObj[key] = this.instance.getFeatureVariableDouble(feature, key, userId, attributes)
251+
variableObj[key] = this.instance.getFeatureVariableDouble(
252+
feature,
253+
key,
254+
userId,
255+
attributes,
256+
)
249257
break
250258
}
251259
})
@@ -336,7 +344,11 @@ export class OptimizelySDKWrapper {
336344
* @returns {boolean}
337345
* @memberof OptimizelySDKWrapper
338346
*/
339-
public setForcedVariation(experiment: string, userId: string, variationKey: string): boolean {
347+
public setForcedVariation(
348+
experiment: string,
349+
userId: string,
350+
variationKey: string,
351+
): boolean {
340352
return this.instance.setForcedVariation(experiment, userId, variationKey)
341353
}
342354

@@ -360,20 +372,9 @@ export class OptimizelySDKWrapper {
360372
}
361373
}
362374

363-
private setupAttributesLoader(config: OptimizelySDKWrapperConfig): ResourceLoader<UserAttributes> {
364-
let attributesLoader: ResourceLoader<UserAttributes>
365-
366-
if (config.UNSTABLE_attributesLoader) {
367-
attributesLoader = config.UNSTABLE_attributesLoader
368-
} else {
369-
attributesLoader = new ProvidedAttributesLoader({
370-
attributes: config.attributes,
371-
})
372-
}
373-
return attributesLoader
374-
}
375-
376-
private setupDatafileLoader(config: OptimizelySDKWrapperConfig): ResourceLoader<OptimizelyDatafile> {
375+
private setupDatafileResource(
376+
config: OptimizelySDKWrapperConfig,
377+
): Resource<OptimizelyDatafile> {
377378
let datafileLoader: ResourceLoader<OptimizelyDatafile>
378379

379380
if (config.datafile) {
@@ -384,29 +385,15 @@ export class OptimizelySDKWrapper {
384385
datafileLoader = new FetchUrlDatafileLoader({
385386
sdkKey: config.sdkKey,
386387
})
387-
} else if (config.UNSTABLE_datafileLoader) {
388-
datafileLoader = config.UNSTABLE_datafileLoader
389388
} else {
390-
throw new Error('Must supply either "datafile", "SDKKey" or "datafileLoader"')
389+
throw new Error('Must supply either "datafile", "SDKKey"')
391390
}
392391

393-
return datafileLoader
394-
}
395-
396-
private setupUserIdLoader(config: OptimizelySDKWrapperConfig): ResourceLoader<UserId> {
397-
if (config.UNSTABLE_userIdLoader) {
398-
return config.UNSTABLE_userIdLoader
399-
} else if (config.userId) {
400-
return new StaticUserIdLoader(config.userId)
401-
} else {
402-
return new StaticUserIdLoader(null)
403-
}
392+
return new Resource(datafileLoader)
404393
}
405394

406395
private onInitialized() {
407-
const datafile = this.resourceManager.datafile.value
408-
this.userId = this.resourceManager.userId.value || null
409-
this.attributes = this.resourceManager.attributes.value || {}
396+
const datafile = this.datafileResource.value
410397
if (datafile) {
411398
this.datafile = datafile
412399
}
Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { OptimizelyDatafile } from './OptimizelySDKWrapper'
22
import { UserAttributes } from '@optimizely/optimizely-sdk'
3-
import { UserId } from './UserIdLoaders'
43

54
export interface ResourceLoader<K> {
65
load: () => K | Promise<K>
76
}
87

9-
class Resource<K> {
8+
export class Resource<K> {
109
private loader: ResourceLoader<K>
1110

1211
private _value?: K
@@ -24,56 +23,26 @@ class Resource<K> {
2423
}
2524

2625
constructor(loader: ResourceLoader<K>) {
27-
this.loader = loader;
28-
this._hasLoaded = false;
29-
this.promise = this.load();
26+
this.loader = loader
27+
this._hasLoaded = false
28+
this.promise = this.load()
3029
}
3130

3231
private updateStateFromLoadResult(value: K) {
33-
this._value = value;
34-
this._hasLoaded = true;
32+
this._value = value
33+
this._hasLoaded = true
3534
}
3635

3736
private load(): Promise<K> {
3837
const maybeValue = this.loader.load()
3938
// TODO: test does this work with polyfilled promise?
4039
if (maybeValue instanceof Promise) {
4140
return maybeValue.then(value => {
42-
this.updateStateFromLoadResult(value);
41+
this.updateStateFromLoadResult(value)
4342
return value
4443
})
4544
}
46-
this.updateStateFromLoadResult(maybeValue);
47-
return Promise.resolve(maybeValue);
45+
this.updateStateFromLoadResult(maybeValue)
46+
return Promise.resolve(maybeValue)
4847
}
49-
}
50-
51-
type OptimizelyResource = OptimizelyDatafile | UserAttributes | UserId
52-
53-
export class ResourceManager {
54-
public datafile: Resource<OptimizelyDatafile>
55-
public attributes: Resource<UserAttributes>
56-
public userId: Resource<UserId>
57-
58-
private resources: Resource<OptimizelyResource>[]
59-
60-
constructor(loaders: {
61-
datafile: ResourceLoader<OptimizelyDatafile>
62-
attributes: ResourceLoader<UserAttributes>
63-
userId: ResourceLoader<UserId>
64-
}) {
65-
this.datafile = new Resource(loaders.datafile)
66-
this.attributes = new Resource(loaders.attributes)
67-
this.userId = new Resource(loaders.userId)
68-
69-
this.resources = [this.datafile, this.attributes, this.userId];
70-
}
71-
72-
public allResourcesLoaded(): boolean {
73-
return this.resources.every(resource => resource.hasLoaded);
74-
}
75-
76-
public allResourcePromises(): Promise<OptimizelyResource[]> {
77-
return Promise.all([...this.resources.map(resource => resource.promise)])
78-
}
79-
}
48+
}

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

Lines changed: 0 additions & 48 deletions
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { OptimizelySDKWrapperConfig, OptimizelySDKWrapper } from './OptimizelySD
33

44
export { OptimizelySDKWrapper } from './OptimizelySDKWrapper'
55
export { OptimizelyDatafile, VariableValuesObject } from './Datafile'
6-
export { CookieRandomUserIdLoader } from './UserIdLoaders'
76
import * as optimizelyEnums from '@optimizely/optimizely-sdk/lib/utils/enums'
87

98
export function createInstance(config: OptimizelySDKWrapperConfig): OptimizelySDKWrapper {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function testLocalDatafile() {
2828
async function testUrlLoad() {
2929
let optimizely = new OptimizelySDKWrapper({
3030
sdkKey: 'GaXr9RoDhRcqXJm3ruskRa',
31-
userId: 'user',
3231
})
3332
optimizely.track('foo', 'jordan')
3433
optimizely.track('foo', 'jordan', {

0 commit comments

Comments
 (0)