Skip to content
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

Use setTimeout for async work #922

Merged
merged 6 commits into from
Oct 2, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
dist/
tmp/
coverage/
lib/**/*.d.ts
.idea/
Expand Down
16 changes: 16 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var path = require('path');

var fs = require('fs');

var coverage = process.env.COVERAGE;

// configures browsers to run test against
Expand All @@ -11,6 +13,12 @@ var browsers = (process.env.TEST_BROWSERS || 'ChromeHeadless').split(',');
// use puppeteer provided Chrome for testing
process.env.CHROME_BIN = require('puppeteer').executablePath();

var tmpDir = path.join(__dirname, 'tmp');

fs.mkdirSync(tmpDir, { recursive: true });

var firefoxProfile = fs.mkdtempSync(path.join(tmpDir, 'firefox-profile'));

var absoluteBasePath = path.resolve(__dirname);

var suite = coverage ? 'test/coverageBundle.js' : 'test/testBundle.js';
Expand All @@ -35,6 +43,14 @@ module.exports = function(karma) {

reporters: [ 'progress' ].concat(coverage ? 'coverage' : []),

customLaunchers: {
'FirefoxHeadless': {
base: 'Firefox',
flags: [ '-headless' ],
profile: firefoxProfile
}
},

browsers,

browserNoActivityTimeout: 30000,
Expand Down
38 changes: 18 additions & 20 deletions lib/features/scheduler/Scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,26 @@

Scheduler.prototype._schedule = function(taskFn, id) {

const {
promise,
resolve,
reject
} = defer();
const deferred = defer();

const executionId = setTimeout(() => {

const executionId = requestAnimationFrame(() => {
try {
resolve(taskFn());
this._scheduled[id] = null;

try {
deferred.resolve(taskFn());
} catch (error) {
deferred.reject(error);
}
} catch (error) {
reject(error);
console.error('Scheduler#_schedule execution failed', error);

Check warning on line 75 in lib/features/scheduler/Scheduler.js

View check run for this annotation

Codecov / codecov/patch

lib/features/scheduler/Scheduler.js#L75

Added line #L75 was not covered by tests
}
});

return {
executionId,
promise
promise: deferred.promise
};
};

Expand All @@ -96,7 +99,7 @@
};

Scheduler.prototype._cancel = function(scheduled) {
cancelAnimationFrame(scheduled.executionId);
clearTimeout(scheduled.executionId);
};

/**
Expand All @@ -108,17 +111,12 @@
*/
function defer() {

let resolve;
let reject;
const deferred = {};

const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
deferred.promise = new Promise((resolve, reject) => {
deferred.resolve = resolve;
deferred.reject = reject;
});

return {
promise,
resolve,
reject
};
return deferred;
}
Loading