| | import { |
| | retrieveExperimentAssignment, |
| | storeExperimentAssignment, |
| | removeExpiredExperimentAssignments, |
| | } from './internal/experiment-assignment-store'; |
| | import * as ExperimentAssignments from './internal/experiment-assignments'; |
| | import { createFallbackExperimentAssignment as createFallbackExperimentAssignment } from './internal/experiment-assignments'; |
| | import * as Request from './internal/requests'; |
| | import * as Timing from './internal/timing'; |
| | import * as Validation from './internal/validations'; |
| | import type { ExperimentAssignment, Config } from './types'; |
| |
|
| | |
| | |
| | |
| | const EXPERIMENT_FETCH_TIMEOUT = 10000; |
| |
|
| | export interface ExPlatClient { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | loadExperimentAssignment: ( experimentName: string ) => Promise< ExperimentAssignment >; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | dangerouslyGetExperimentAssignment: ( experimentName: string ) => ExperimentAssignment; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | dangerouslyGetMaybeLoadedExperimentAssignment: ( |
| | experimentName: string |
| | ) => null | ExperimentAssignment; |
| |
|
| | |
| | |
| | |
| | config: Config; |
| | } |
| |
|
| | export class MissingExperimentAssignmentError extends Error { |
| | constructor( message?: string ) { |
| | super( message ); |
| |
|
| | |
| | if ( Error.captureStackTrace ) { |
| | Error.captureStackTrace( this, MissingExperimentAssignmentError ); |
| | } |
| |
|
| | this.name = 'MissingExperimentAssignmentError'; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function createExPlatClient( config: Config ): ExPlatClient { |
| | if ( typeof window === 'undefined' ) { |
| | throw new Error( 'Running outside of a browser context.' ); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | const createWrappedExperimentAssignmentFetchAndStore = ( experimentName: string ) => |
| | Timing.asyncOneAtATime( async () => { |
| | const fetchedExperimentAssignment = await Request.fetchExperimentAssignment( |
| | config, |
| | experimentName |
| | ); |
| | storeExperimentAssignment( fetchedExperimentAssignment ); |
| | return fetchedExperimentAssignment; |
| | } ); |
| | const experimentNameToWrappedExperimentAssignmentFetchAndStore: Record< |
| | string, |
| | () => Promise< ExperimentAssignment > |
| | > = {}; |
| |
|
| | const safeLogError: typeof config.logError = ( ...args ) => { |
| | try { |
| | config.logError( ...args ); |
| | } catch ( e ) {} |
| | }; |
| |
|
| | |
| | try { |
| | removeExpiredExperimentAssignments(); |
| | } catch ( error ) { |
| | safeLogError( { |
| | message: ( error as Error ).message, |
| | source: 'removeExpiredExperimentAssignments-error', |
| | } ); |
| | } |
| |
|
| | return { |
| | loadExperimentAssignment: async ( experimentName: string ): Promise< ExperimentAssignment > => { |
| | try { |
| | if ( ! Validation.isName( experimentName ) ) { |
| | throw new Error( `Invalid experimentName: "${ experimentName }"` ); |
| | } |
| |
|
| | const storedExperimentAssignment = retrieveExperimentAssignment( experimentName ); |
| | if ( |
| | storedExperimentAssignment && |
| | ExperimentAssignments.isAlive( storedExperimentAssignment ) |
| | ) { |
| | return storedExperimentAssignment; |
| | } |
| |
|
| | if ( |
| | experimentNameToWrappedExperimentAssignmentFetchAndStore[ experimentName ] === undefined |
| | ) { |
| | experimentNameToWrappedExperimentAssignmentFetchAndStore[ experimentName ] = |
| | createWrappedExperimentAssignmentFetchAndStore( experimentName ); |
| | } |
| |
|
| | |
| | let experimentFetchTimeout = EXPERIMENT_FETCH_TIMEOUT; |
| | if ( Math.random() > 0.5 ) { |
| | experimentFetchTimeout = 5000; |
| | } |
| |
|
| | |
| | |
| | const fetchedExperimentAssignment = await Timing.timeoutPromise( |
| | experimentNameToWrappedExperimentAssignmentFetchAndStore[ experimentName ](), |
| | experimentFetchTimeout |
| | ); |
| | if ( ! fetchedExperimentAssignment ) { |
| | throw new Error( 'Could not fetch ExperimentAssignment' ); |
| | } |
| |
|
| | return fetchedExperimentAssignment; |
| | } catch ( initialError ) { |
| | safeLogError( { |
| | message: ( initialError as Error ).message, |
| | experimentName, |
| | source: 'loadExperimentAssignment-initialError', |
| | } ); |
| | } |
| |
|
| | |
| | try { |
| | |
| | const storedExperimentAssignment = retrieveExperimentAssignment( experimentName ); |
| | if ( storedExperimentAssignment ) { |
| | return storedExperimentAssignment; |
| | } |
| |
|
| | |
| | |
| | |
| | const fallbackExperimentAssignment = createFallbackExperimentAssignment( experimentName ); |
| | storeExperimentAssignment( fallbackExperimentAssignment ); |
| | return fallbackExperimentAssignment; |
| | } catch ( fallbackError ) { |
| | safeLogError( { |
| | message: ( fallbackError as Error ).message, |
| | experimentName, |
| | source: 'loadExperimentAssignment-fallbackError', |
| | } ); |
| |
|
| | |
| | return createFallbackExperimentAssignment( experimentName ); |
| | } |
| | }, |
| | dangerouslyGetExperimentAssignment: ( experimentName: string ): ExperimentAssignment => { |
| | try { |
| | if ( ! Validation.isName( experimentName ) ) { |
| | throw new Error( `Invalid experimentName: ${ experimentName }` ); |
| | } |
| |
|
| | const storedExperimentAssignment = retrieveExperimentAssignment( experimentName ); |
| | if ( ! storedExperimentAssignment ) { |
| | throw new Error( |
| | "Trying to dangerously get an ExperimentAssignment that hasn't loaded." |
| | ); |
| | } |
| |
|
| | |
| | if ( config.isDevelopmentMode ) { |
| | |
| | if ( |
| | storedExperimentAssignment && |
| | Timing.monotonicNow() - storedExperimentAssignment.retrievedTimestamp < 1000 |
| | ) { |
| | safeLogError( { |
| | message: |
| | 'Warning: Trying to dangerously get an ExperimentAssignment too soon after loading it.', |
| | experimentName, |
| | source: 'dangerouslyGetExperimentAssignment', |
| | } ); |
| | } |
| | } |
| |
|
| | return storedExperimentAssignment; |
| | } catch ( error ) { |
| | if ( config.isDevelopmentMode ) { |
| | safeLogError( { |
| | message: ( error as Error ).message, |
| | experimentName, |
| | source: 'dangerouslyGetExperimentAssignment-error', |
| | } ); |
| | } |
| | return createFallbackExperimentAssignment( experimentName ); |
| | } |
| | }, |
| | dangerouslyGetMaybeLoadedExperimentAssignment: ( |
| | experimentName: string |
| | ): ExperimentAssignment | null => { |
| | try { |
| | if ( ! Validation.isName( experimentName ) ) { |
| | throw new Error( `Invalid experimentName: ${ experimentName }` ); |
| | } |
| |
|
| | const storedExperimentAssignment = retrieveExperimentAssignment( experimentName ); |
| | if ( ! storedExperimentAssignment ) { |
| | return null; |
| | } |
| |
|
| | return storedExperimentAssignment; |
| | } catch ( error ) { |
| | if ( config.isDevelopmentMode ) { |
| | safeLogError( { |
| | message: ( error as Error ).message, |
| | experimentName, |
| | source: 'dangerouslyGetMaybeLoadedExperimentAssignment-error', |
| | } ); |
| | } |
| | return createFallbackExperimentAssignment( experimentName ); |
| | } |
| | }, |
| | config, |
| | }; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function createSsrSafeDummyExPlatClient( config: Config ): ExPlatClient { |
| | return { |
| | loadExperimentAssignment: async ( experimentName: string ) => { |
| | config.logError( { |
| | message: 'Attempting to load ExperimentAssignment in SSR context', |
| | experimentName, |
| | } ); |
| | return createFallbackExperimentAssignment( experimentName ); |
| | }, |
| | dangerouslyGetExperimentAssignment: ( experimentName: string ) => { |
| | config.logError( { |
| | message: 'Attempting to dangerously get ExperimentAssignment in SSR context', |
| | experimentName, |
| | } ); |
| | return createFallbackExperimentAssignment( experimentName ); |
| | }, |
| | dangerouslyGetMaybeLoadedExperimentAssignment: ( experimentName: string ) => { |
| | config.logError( { |
| | message: 'Attempting to dangerously get ExperimentAssignment in SSR context', |
| | experimentName, |
| | } ); |
| | return createFallbackExperimentAssignment( experimentName ); |
| | }, |
| | config, |
| | }; |
| | } |
| |
|