Skip to content

Commit c9972e6

Browse files
[FSSDK-10201] queueMicrotask alternative for unsupported platforms (#933)
* [FSSDK-10201] queueMicrotask alternative for unsupported platforms * [FSSDK-10201] dir rename, testcase addition * [FSSDK-10201] test improvement, redundant code removal * [FSSDK-10201] licence text addition, only removal * [FSSDK-10201] unused import removal
1 parent 851b066 commit c9972e6

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-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/microtask';
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/microtask';
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/microtask/index.tests.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import { scheduleMicrotaskOrTimeout } from './';
18+
19+
describe('scheduleMicrotaskOrTimeout', () => {
20+
it('should use queueMicrotask if available', (done) => {
21+
// Assuming queueMicrotask is available in the environment
22+
scheduleMicrotaskOrTimeout(() => {
23+
done();
24+
});
25+
});
26+
27+
it('should fallback to setTimeout if queueMicrotask is not available', (done) => {
28+
// Temporarily remove queueMicrotask to test the fallback
29+
const originalQueueMicrotask = window.queueMicrotask;
30+
window.queueMicrotask = undefined;
31+
32+
scheduleMicrotaskOrTimeout(() => {
33+
// Restore queueMicrotask before calling done
34+
window.queueMicrotask = originalQueueMicrotask;
35+
done();
36+
});
37+
});
38+
});

lib/utils/microtask/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)