From e3dae0c2614005284c6a497fb88fe3bc22093ed8 Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:16:35 +0600 Subject: [PATCH 1/5] [FSSDK-10201] queueMicrotask alternative for unsupported platforms --- lib/core/odp/odp_event_manager.ts | 5 ++-- .../project_config/project_config_manager.ts | 6 ++--- lib/utils/microtasks/index.ts | 25 +++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 lib/utils/microtasks/index.ts diff --git a/lib/core/odp/odp_event_manager.ts b/lib/core/odp/odp_event_manager.ts index 80baa4822..f6641eab9 100644 --- a/lib/core/odp/odp_event_manager.ts +++ b/lib/core/odp/odp_event_manager.ts @@ -24,6 +24,7 @@ import { OdpConfig } from './odp_config'; import { IOdpEventApiManager } from './odp_event_api_manager'; import { invalidOdpDataFound } from './odp_utils'; import { IUserAgentParser } from './user_agent_parser'; +import { scheduleMicrotaskOrTimeout } from '../../utils/microtasks'; const MAX_RETRIES = 3; @@ -393,14 +394,14 @@ export abstract class OdpEventManager implements IOdpEventManager { if (batch.length > 0) { // put sending the event on another event loop - queueMicrotask(async () => { + scheduleMicrotaskOrTimeout(async () => { let shouldRetry: boolean; let attemptNumber = 0; do { shouldRetry = await this.apiManager.sendEvents(odpConfig, batch); attemptNumber += 1; } while (shouldRetry && attemptNumber < this.retries); - }); + }) } } diff --git a/lib/core/project_config/project_config_manager.ts b/lib/core/project_config/project_config_manager.ts index 3f3aea4df..1c1512406 100644 --- a/lib/core/project_config/project_config_manager.ts +++ b/lib/core/project_config/project_config_manager.ts @@ -20,6 +20,7 @@ import { ERROR_MESSAGES } from '../../utils/enums'; import { createOptimizelyConfig } from '../optimizely_config'; import { OnReadyResult, OptimizelyConfig, DatafileManager } from '../../shared_types'; import { ProjectConfig, toDatafile, tryCreatingProjectConfig } from '../project_config'; +import { scheduleMicrotaskOrTimeout } from '../../utils/microtasks'; const logger = getLogger(); const MODULE_NAME = 'PROJECT_CONFIG_MANAGER'; @@ -189,10 +190,9 @@ export class ProjectConfigManager { if (configObj && oldRevision !== configObj.revision) { this.configObj = configObj; this.optimizelyConfigObj = null; - - queueMicrotask(() => { + scheduleMicrotaskOrTimeout(() => { this.updateListeners.forEach(listener => listener(configObj)); - }); + }) } } diff --git a/lib/utils/microtasks/index.ts b/lib/utils/microtasks/index.ts new file mode 100644 index 000000000..816b17a27 --- /dev/null +++ b/lib/utils/microtasks/index.ts @@ -0,0 +1,25 @@ +/** + * Copyright 2024, 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. + */ + +type Callback = () => void; + +export const scheduleMicrotaskOrTimeout = (callback: Callback): void =>{ + if (typeof queueMicrotask === 'function') { + queueMicrotask(callback); + } else { + setTimeout(callback); + } +} \ No newline at end of file From 4d09d4c2d988d7d20dc439d6f787723a35ddfd6d Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:23:57 +0600 Subject: [PATCH 2/5] [FSSDK-10201] dir rename, testcase addition --- lib/core/odp/odp_event_manager.ts | 2 +- .../project_config/project_config_manager.ts | 2 +- lib/utils/microtask/index.tests.js | 45 +++++++++++++++++++ lib/utils/{microtasks => microtask}/index.ts | 0 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 lib/utils/microtask/index.tests.js rename lib/utils/{microtasks => microtask}/index.ts (100%) diff --git a/lib/core/odp/odp_event_manager.ts b/lib/core/odp/odp_event_manager.ts index f6641eab9..3b91d7712 100644 --- a/lib/core/odp/odp_event_manager.ts +++ b/lib/core/odp/odp_event_manager.ts @@ -24,7 +24,7 @@ import { OdpConfig } from './odp_config'; import { IOdpEventApiManager } from './odp_event_api_manager'; import { invalidOdpDataFound } from './odp_utils'; import { IUserAgentParser } from './user_agent_parser'; -import { scheduleMicrotaskOrTimeout } from '../../utils/microtasks'; +import { scheduleMicrotaskOrTimeout } from '../../utils/microtask'; const MAX_RETRIES = 3; diff --git a/lib/core/project_config/project_config_manager.ts b/lib/core/project_config/project_config_manager.ts index 1c1512406..b0fe25ddd 100644 --- a/lib/core/project_config/project_config_manager.ts +++ b/lib/core/project_config/project_config_manager.ts @@ -20,7 +20,7 @@ import { ERROR_MESSAGES } from '../../utils/enums'; import { createOptimizelyConfig } from '../optimizely_config'; import { OnReadyResult, OptimizelyConfig, DatafileManager } from '../../shared_types'; import { ProjectConfig, toDatafile, tryCreatingProjectConfig } from '../project_config'; -import { scheduleMicrotaskOrTimeout } from '../../utils/microtasks'; +import { scheduleMicrotaskOrTimeout } from '../../utils/microtask'; const logger = getLogger(); const MODULE_NAME = 'PROJECT_CONFIG_MANAGER'; diff --git a/lib/utils/microtask/index.tests.js b/lib/utils/microtask/index.tests.js new file mode 100644 index 000000000..63256dbae --- /dev/null +++ b/lib/utils/microtask/index.tests.js @@ -0,0 +1,45 @@ +/** + * Copyright 2024, 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 { assert } from 'chai'; +import { scheduleMicrotaskOrTimeout } from './'; + +describe('scheduleMicrotaskOrTimeout', () => { + let called; + + beforeEach(() => { + called = false; + }); + + it('should use queueMicrotask if available', (done) => { + scheduleMicrotaskOrTimeout(() => { + called = true; + assert.isTrue(called, 'queueMicrotask was called'); + done(); + }); + }); + + it('should fallback to setTimeout if queueMicrotask is not available', (done) => { + const originalQueueMicrotask = window.queueMicrotask; + window.queueMicrotask = undefined; + + scheduleMicrotaskOrTimeout(() => { + assert.isTrue(true, 'setTimeout was called'); + window.queueMicrotask = originalQueueMicrotask; + done(); + }); + }); +}); diff --git a/lib/utils/microtasks/index.ts b/lib/utils/microtask/index.ts similarity index 100% rename from lib/utils/microtasks/index.ts rename to lib/utils/microtask/index.ts From 39c8f72de3de1ae4cd4ef1637c5bf67037de8e11 Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:40:36 +0600 Subject: [PATCH 3/5] [FSSDK-10201] test improvement, redundant code removal --- lib/utils/microtask/index.tests.js | 32 +++++------------------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/lib/utils/microtask/index.tests.js b/lib/utils/microtask/index.tests.js index 63256dbae..28d22e170 100644 --- a/lib/utils/microtask/index.tests.js +++ b/lib/utils/microtask/index.tests.js @@ -1,43 +1,21 @@ -/** - * Copyright 2024, 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 { assert } from 'chai'; -import { scheduleMicrotaskOrTimeout } from './'; - -describe('scheduleMicrotaskOrTimeout', () => { - let called; - - beforeEach(() => { - called = false; - }); +import { scheduleMicrotaskOrTimeout } from '.'; +describe.only('scheduleMicrotaskOrTimeout', () => { it('should use queueMicrotask if available', (done) => { + // Assuming queueMicrotask is available in the environment scheduleMicrotaskOrTimeout(() => { - called = true; - assert.isTrue(called, 'queueMicrotask was called'); done(); }); }); it('should fallback to setTimeout if queueMicrotask is not available', (done) => { + // Temporarily remove queueMicrotask to test the fallback const originalQueueMicrotask = window.queueMicrotask; window.queueMicrotask = undefined; scheduleMicrotaskOrTimeout(() => { - assert.isTrue(true, 'setTimeout was called'); + // Restore queueMicrotask before calling done window.queueMicrotask = originalQueueMicrotask; done(); }); From 17ea6f475f8f3fc1b15117d9e5990784933d28cc Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:42:15 +0600 Subject: [PATCH 4/5] [FSSDK-10201] licence text addition, only removal --- lib/utils/microtask/index.tests.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/utils/microtask/index.tests.js b/lib/utils/microtask/index.tests.js index 28d22e170..f352f4ca0 100644 --- a/lib/utils/microtask/index.tests.js +++ b/lib/utils/microtask/index.tests.js @@ -1,7 +1,23 @@ +/** + * Copyright 2024, 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 { assert } from 'chai'; import { scheduleMicrotaskOrTimeout } from '.'; -describe.only('scheduleMicrotaskOrTimeout', () => { +describe('scheduleMicrotaskOrTimeout', () => { it('should use queueMicrotask if available', (done) => { // Assuming queueMicrotask is available in the environment scheduleMicrotaskOrTimeout(() => { From 8002ade5f7a6adcf2b511e93e10f3ea7828d749e Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:51:15 +0600 Subject: [PATCH 5/5] [FSSDK-10201] unused import removal --- lib/utils/microtask/index.tests.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/utils/microtask/index.tests.js b/lib/utils/microtask/index.tests.js index f352f4ca0..16091ad68 100644 --- a/lib/utils/microtask/index.tests.js +++ b/lib/utils/microtask/index.tests.js @@ -14,8 +14,7 @@ * limitations under the License. */ -import { assert } from 'chai'; -import { scheduleMicrotaskOrTimeout } from '.'; +import { scheduleMicrotaskOrTimeout } from './'; describe('scheduleMicrotaskOrTimeout', () => { it('should use queueMicrotask if available', (done) => {