Skip to content

Commit e3dae0c

Browse files
[FSSDK-10201] queueMicrotask alternative for unsupported platforms
1 parent 851b066 commit e3dae0c

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/core/odp/odp_event_manager.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { OdpConfig } from './odp_config';
2424
import { IOdpEventApiManager } from './odp_event_api_manager';
2525
import { invalidOdpDataFound } from './odp_utils';
2626
import { IUserAgentParser } from './user_agent_parser';
27+
import { scheduleMicrotaskOrTimeout } from '../../utils/microtasks';
2728

2829
const MAX_RETRIES = 3;
2930

@@ -393,14 +394,14 @@ export abstract class OdpEventManager implements IOdpEventManager {
393394

394395
if (batch.length > 0) {
395396
// put sending the event on another event loop
396-
queueMicrotask(async () => {
397+
scheduleMicrotaskOrTimeout(async () => {
397398
let shouldRetry: boolean;
398399
let attemptNumber = 0;
399400
do {
400401
shouldRetry = await this.apiManager.sendEvents(odpConfig, batch);
401402
attemptNumber += 1;
402403
} while (shouldRetry && attemptNumber < this.retries);
403-
});
404+
})
404405
}
405406
}
406407

lib/core/project_config/project_config_manager.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ERROR_MESSAGES } from '../../utils/enums';
2020
import { createOptimizelyConfig } from '../optimizely_config';
2121
import { OnReadyResult, OptimizelyConfig, DatafileManager } from '../../shared_types';
2222
import { ProjectConfig, toDatafile, tryCreatingProjectConfig } from '../project_config';
23+
import { scheduleMicrotaskOrTimeout } from '../../utils/microtasks';
2324

2425
const logger = getLogger();
2526
const MODULE_NAME = 'PROJECT_CONFIG_MANAGER';
@@ -189,10 +190,9 @@ export class ProjectConfigManager {
189190
if (configObj && oldRevision !== configObj.revision) {
190191
this.configObj = configObj;
191192
this.optimizelyConfigObj = null;
192-
193-
queueMicrotask(() => {
193+
scheduleMicrotaskOrTimeout(() => {
194194
this.updateListeners.forEach(listener => listener(configObj));
195-
});
195+
})
196196
}
197197
}
198198

lib/utils/microtasks/index.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2024, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
type Callback = () => void;
18+
19+
export const scheduleMicrotaskOrTimeout = (callback: Callback): void =>{
20+
if (typeof queueMicrotask === 'function') {
21+
queueMicrotask(callback);
22+
} else {
23+
setTimeout(callback);
24+
}
25+
}

0 commit comments

Comments
 (0)