From dd259e5704a530e0ebcc57ef257c28407f6e3369 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Thu, 22 Sep 2022 09:06:25 -0400 Subject: [PATCH 1/3] Remove execution context handling --- packages/optimizely-sdk/lib/index.browser.ts | 4 -- packages/optimizely-sdk/lib/index.node.ts | 4 -- .../optimizely-sdk/lib/index.react_native.ts | 4 -- .../optimizely-sdk/lib/utils/enums/index.ts | 10 ----- .../lib/utils/execution_context/index.ts | 45 ------------------- .../request_handler_factory.ts | 15 +++---- 6 files changed, 6 insertions(+), 76 deletions(-) delete mode 100644 packages/optimizely-sdk/lib/utils/execution_context/index.ts diff --git a/packages/optimizely-sdk/lib/index.browser.ts b/packages/optimizely-sdk/lib/index.browser.ts index 5749ebf43..c8532d7cd 100644 --- a/packages/optimizely-sdk/lib/index.browser.ts +++ b/packages/optimizely-sdk/lib/index.browser.ts @@ -32,8 +32,6 @@ import { createNotificationCenter } from './core/notification_center'; import { default as eventProcessor } from './plugins/event_processor'; import { OptimizelyDecideOption, Client, Config } from './shared_types'; import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager'; -import { EXECUTION_CONTEXT_TYPE } from './utils/enums'; -import { ExecutionContext } from './utils/execution_context'; const logger = getLogger(); logHelper.setLogHandler(loggerPlugin.createLogger()); @@ -46,8 +44,6 @@ const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000; let hasRetriedEvents = false; -ExecutionContext.Current = EXECUTION_CONTEXT_TYPE.BROWSER; - /** * Creates an instance of the Optimizely class * @param {Config} config diff --git a/packages/optimizely-sdk/lib/index.node.ts b/packages/optimizely-sdk/lib/index.node.ts index 6fe59eb22..56ba69c71 100644 --- a/packages/optimizely-sdk/lib/index.node.ts +++ b/packages/optimizely-sdk/lib/index.node.ts @@ -32,8 +32,6 @@ import { createNotificationCenter } from './core/notification_center'; import { createEventProcessor } from './plugins/event_processor'; import { OptimizelyDecideOption, Client, Config } from './shared_types'; import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager'; -import { ExecutionContext } from './utils/execution_context'; -import { EXECUTION_CONTEXT_TYPE } from './utils/enums'; const logger = getLogger(); setLogLevel(LogLevel.ERROR); @@ -42,8 +40,6 @@ const DEFAULT_EVENT_BATCH_SIZE = 10; const DEFAULT_EVENT_FLUSH_INTERVAL = 30000; // Unit is ms, default is 30s const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000; -ExecutionContext.Current = EXECUTION_CONTEXT_TYPE.NODE; - /** * Creates an instance of the Optimizely class * @param {Config} config diff --git a/packages/optimizely-sdk/lib/index.react_native.ts b/packages/optimizely-sdk/lib/index.react_native.ts index cece646f2..b2c67bac8 100644 --- a/packages/optimizely-sdk/lib/index.react_native.ts +++ b/packages/optimizely-sdk/lib/index.react_native.ts @@ -32,8 +32,6 @@ import { createNotificationCenter } from './core/notification_center'; import { createEventProcessor } from './plugins/event_processor/index.react_native'; import { OptimizelyDecideOption, Client, Config } from './shared_types'; import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager'; -import { EXECUTION_CONTEXT_TYPE } from './utils/enums'; -import { ExecutionContext } from './utils/execution_context'; const logger = getLogger(); setLogHandler(loggerPlugin.createLogger()); @@ -43,8 +41,6 @@ const DEFAULT_EVENT_BATCH_SIZE = 10; const DEFAULT_EVENT_FLUSH_INTERVAL = 1000; // Unit is ms, default is 1s const DEFAULT_EVENT_MAX_QUEUE_SIZE = 10000; -ExecutionContext.Current = EXECUTION_CONTEXT_TYPE.BROWSER; - /** * Creates an instance of the Optimizely class * @param {Config} config diff --git a/packages/optimizely-sdk/lib/utils/enums/index.ts b/packages/optimizely-sdk/lib/utils/enums/index.ts index e9eecdcf4..5db60a894 100644 --- a/packages/optimizely-sdk/lib/utils/enums/index.ts +++ b/packages/optimizely-sdk/lib/utils/enums/index.ts @@ -291,13 +291,3 @@ export enum NOTIFICATION_TYPES { OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE', TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event', } - - -/** - * Valid types of Javascript contexts in which this code is executing - */ -export enum EXECUTION_CONTEXT_TYPE { - NOT_DEFINED, - BROWSER, - NODE, -} diff --git a/packages/optimizely-sdk/lib/utils/execution_context/index.ts b/packages/optimizely-sdk/lib/utils/execution_context/index.ts deleted file mode 100644 index f9f3e2457..000000000 --- a/packages/optimizely-sdk/lib/utils/execution_context/index.ts +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Copyright 2022, Optimizely - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { EXECUTION_CONTEXT_TYPE } from '../enums'; - -/** - * Determine the running or execution context for JavaScript - * Note: React Native is considered a browser context - */ -export class ExecutionContext { - /** - * Holds the current value of the execution context - * @private - */ - private static _currentContext: EXECUTION_CONTEXT_TYPE = EXECUTION_CONTEXT_TYPE.NOT_DEFINED; - - /** - * Gets the current running context - * @constructor - */ - public static get Current(): EXECUTION_CONTEXT_TYPE { - return this._currentContext; - } - - /** - * Sets the current running context ideally from package initialization - * @param newValue The new execution context - * @constructor - */ - public static set Current(newValue: EXECUTION_CONTEXT_TYPE) { - this._currentContext = newValue; - } -} diff --git a/packages/optimizely-sdk/lib/utils/http_request_handler/request_handler_factory.ts b/packages/optimizely-sdk/lib/utils/http_request_handler/request_handler_factory.ts index 455754cb5..ddab23faf 100644 --- a/packages/optimizely-sdk/lib/utils/http_request_handler/request_handler_factory.ts +++ b/packages/optimizely-sdk/lib/utils/http_request_handler/request_handler_factory.ts @@ -18,21 +18,18 @@ import { LogHandler } from '../../modules/logging'; import { RequestHandler } from './http'; import { NodeRequestHandler } from './node_request_handler'; import { BrowserRequestHandler } from './browser_request_handler'; -import { ExecutionContext } from '../execution_context'; -import { EXECUTION_CONTEXT_TYPE } from '../enums'; /** * Factory to create the appropriate type of RequestHandler based on a provided context */ export class RequestHandlerFactory { public static createHandler(logger: LogHandler, timeout?: number): RequestHandler { - switch (ExecutionContext.Current) { - case EXECUTION_CONTEXT_TYPE.BROWSER: - return new BrowserRequestHandler(logger, timeout); - case EXECUTION_CONTEXT_TYPE.NODE: - return new NodeRequestHandler(logger, timeout); - default: - return null as unknown as RequestHandler; + if (window) { + return new BrowserRequestHandler(logger, timeout); + } else if (process) { + return new NodeRequestHandler(logger, timeout); + } else { + return null as unknown as RequestHandler; } } } From 2aeea41748d5f2efe45d44c8a4784d2db57f1fb8 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 26 Sep 2022 10:42:48 -0400 Subject: [PATCH 2/3] Fix bad merge on my part --- packages/optimizely-sdk/lib/index.browser.ts | 2 +- packages/optimizely-sdk/lib/index.react_native.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/optimizely-sdk/lib/index.browser.ts b/packages/optimizely-sdk/lib/index.browser.ts index c8532d7cd..1c8cf4068 100644 --- a/packages/optimizely-sdk/lib/index.browser.ts +++ b/packages/optimizely-sdk/lib/index.browser.ts @@ -31,7 +31,7 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida import { createNotificationCenter } from './core/notification_center'; import { default as eventProcessor } from './plugins/event_processor'; import { OptimizelyDecideOption, Client, Config } from './shared_types'; -import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager'; +import { createHttpPollingDatafileManager } from './plugins/datafile_manager/browser_http_polling_datafile_manager'; const logger = getLogger(); logHelper.setLogHandler(loggerPlugin.createLogger()); diff --git a/packages/optimizely-sdk/lib/index.react_native.ts b/packages/optimizely-sdk/lib/index.react_native.ts index b2c67bac8..04525d82f 100644 --- a/packages/optimizely-sdk/lib/index.react_native.ts +++ b/packages/optimizely-sdk/lib/index.react_native.ts @@ -31,7 +31,9 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida import { createNotificationCenter } from './core/notification_center'; import { createEventProcessor } from './plugins/event_processor/index.react_native'; import { OptimizelyDecideOption, Client, Config } from './shared_types'; -import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager'; +import { + createHttpPollingDatafileManager, +} from './plugins/datafile_manager/react_native_http_polling_datafile_manager'; const logger = getLogger(); setLogHandler(loggerPlugin.createLogger()); From 61c9b96a735ff469aca2a6af1ac7d5d97481aa72 Mon Sep 17 00:00:00 2001 From: Mike Chu Date: Mon, 26 Sep 2022 11:50:26 -0400 Subject: [PATCH 3/3] Match import formatting --- packages/optimizely-sdk/lib/index.react_native.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/optimizely-sdk/lib/index.react_native.ts b/packages/optimizely-sdk/lib/index.react_native.ts index 04525d82f..31dd92d45 100644 --- a/packages/optimizely-sdk/lib/index.react_native.ts +++ b/packages/optimizely-sdk/lib/index.react_native.ts @@ -31,9 +31,8 @@ import eventProcessorConfigValidator from './utils/event_processor_config_valida import { createNotificationCenter } from './core/notification_center'; import { createEventProcessor } from './plugins/event_processor/index.react_native'; import { OptimizelyDecideOption, Client, Config } from './shared_types'; -import { - createHttpPollingDatafileManager, -} from './plugins/datafile_manager/react_native_http_polling_datafile_manager'; +import { createHttpPollingDatafileManager } from + './plugins/datafile_manager/react_native_http_polling_datafile_manager'; const logger = getLogger(); setLogHandler(loggerPlugin.createLogger());