lib: add navigator.onLine
Motivated by #50200
I did not add the ability to listen on a status change, as I expect that it is problematical as getting the online status is slow.
Using following benchmark I get 2600 ops/s. I could add to process
the events online
and offline
, if we listen to online
or offline
. And then i would set a setInterval with lets say 50ms or 250 ms, and check if there was a status change and emit the corresponding event. Maybe if we have listeners, than instead of directly calling getOnlineStatus
, we use the last value derived in the setInterval
.
But for now, this is an proposal. Maybe you dont like it and we can stomp this PR into the trashbin :P
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
n: [1e4],
});
// Warm up.
const length = 1024;
const array = [];
for (let i = 0; i < length; ++i) {
array.push(global.navigator.onLine);
}
function main({ n }) {
bench.start();
for (let i = 0; i < n; ++i) {
const index = i % length;
array[index] = global.navigator.onLine;
}
bench.end(n);
// Verify the entries to prevent dead code elimination from making
// the benchmark invalid.
for (let i = 0; i < length; ++i) {
assert.strictEqual(typeof array[i], 'boolean');
}
}