Skip to content

[FSSDK-10201] queueMicrotask alternative for unsupported platforms #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/core/odp/odp_event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/microtask';

const MAX_RETRIES = 3;

Expand Down Expand Up @@ -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);
});
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/core/project_config/project_config_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/microtask';

const logger = getLogger();
const MODULE_NAME = 'PROJECT_CONFIG_MANAGER';
Expand Down Expand Up @@ -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));
});
})
}
}

Expand Down
38 changes: 38 additions & 0 deletions lib/utils/microtask/index.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* 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 { scheduleMicrotaskOrTimeout } from './';

describe('scheduleMicrotaskOrTimeout', () => {
it('should use queueMicrotask if available', (done) => {
// Assuming queueMicrotask is available in the environment
scheduleMicrotaskOrTimeout(() => {
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(() => {
// Restore queueMicrotask before calling done
window.queueMicrotask = originalQueueMicrotask;
done();
});
});
});
25 changes: 25 additions & 0 deletions lib/utils/microtask/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading