@@ -5,15 +5,12 @@ import { Defaults, ExperimentConfig } from './config';
55import { ExperimentClient } from './experimentClient' ;
66import { AmplitudeIntegrationPlugin } from './integration/amplitude' ;
77import { DefaultUserProvider } from './providers/default' ;
8+ import { ExperimentPlugin } from './types/plugin' ;
89
910// Global instances for debugging.
1011safeGlobal . experimentInstances = { } ;
1112const instances = safeGlobal . experimentInstances ;
1213
13- const getInstanceName = ( config : ExperimentConfig ) : string => {
14- return config ?. instanceName || Defaults . instanceName ;
15- } ;
16-
1714/**
1815 * Initializes a singleton {@link ExperimentClient} identified by the configured
1916 * instance name.
@@ -25,23 +22,7 @@ export const initialize = (
2522 apiKey : string ,
2623 config ?: ExperimentConfig ,
2724) : ExperimentClient => {
28- // Store instances by appending the instance name and api key. Allows for
29- // initializing multiple default instances for different api keys.
30- const instanceName = getInstanceName ( config ) ;
31- // The internal instance name prefix is used by web experiment to differentiate
32- // web and feature experiment sdks which use the same api key.
33- const internalInstanceNameSuffix = config ?. [ 'internalInstanceNameSuffix' ] ;
34- const instanceKey = internalInstanceNameSuffix
35- ? `${ instanceName } .${ apiKey } .${ internalInstanceNameSuffix } `
36- : `${ instanceName } .${ apiKey } ` ;
37- if ( ! instances [ instanceKey ] ) {
38- config = {
39- ...config ,
40- userProvider : new DefaultUserProvider ( config ?. userProvider , apiKey ) ,
41- } ;
42- instances [ instanceKey ] = new ExperimentClient ( apiKey , config ) ;
43- }
44- return instances [ instanceKey ] ;
25+ return _initialize ( apiKey , config ) ;
4526} ;
4627
4728/**
@@ -59,15 +40,56 @@ export const initializeWithAmplitudeAnalytics = (
5940 apiKey : string ,
6041 config ?: ExperimentConfig ,
6142) : ExperimentClient => {
62- const instanceName = getInstanceName ( config ) ;
63- const client = initialize ( apiKey , config ) ;
64- client . addPlugin (
43+ const plugin = ( ) =>
6544 new AmplitudeIntegrationPlugin (
6645 apiKey ,
67- AnalyticsConnector . getInstance ( instanceName ) ,
46+ AnalyticsConnector . getInstance ( getInstanceName ( config ) ) ,
6847 10000 ,
69- ) ,
70- ) ;
48+ ) ;
49+ return _initialize ( apiKey , config , plugin ) ;
50+ } ;
51+
52+ const getInstanceName = ( config : ExperimentConfig ) : string => {
53+ return config ?. instanceName || Defaults . instanceName ;
54+ } ;
55+
56+ const getInstanceKey = ( apiKey : string , config : ExperimentConfig ) : string => {
57+ // Store instances by appending the instance name and api key. Allows for
58+ // initializing multiple default instances for different api keys.
59+ const instanceName = getInstanceName ( config ) ;
60+ // The internal instance name prefix is used by web experiment to differentiate
61+ // web and feature experiment sdks which use the same api key.
62+ const internalInstanceNameSuffix = config ?. [ 'internalInstanceNameSuffix' ] ;
63+ return internalInstanceNameSuffix
64+ ? `${ instanceName } .${ apiKey } .${ internalInstanceNameSuffix } `
65+ : `${ instanceName } .${ apiKey } ` ;
66+ } ;
67+
68+ const newExperimentClient = (
69+ apiKey : string ,
70+ config : ExperimentConfig ,
71+ ) : ExperimentClient => {
72+ return new ExperimentClient ( apiKey , {
73+ ...config ,
74+ userProvider : new DefaultUserProvider ( config ?. userProvider , apiKey ) ,
75+ } ) ;
76+ } ;
77+
78+ const _initialize = (
79+ apiKey : string ,
80+ config ?: ExperimentConfig ,
81+ plugin ?: ( ) => ExperimentPlugin ,
82+ ) : ExperimentClient => {
83+ const instanceKey = getInstanceKey ( apiKey , config ) ;
84+ let client = instances [ instanceKey ] ;
85+ if ( client ) {
86+ return client ;
87+ }
88+ client = newExperimentClient ( apiKey , config ) ;
89+ if ( plugin ) {
90+ client . addPlugin ( plugin ( ) ) ;
91+ }
92+ instances [ instanceKey ] = client ;
7193 return client ;
7294} ;
7395
0 commit comments