-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add polling function management | refs #35772
- Loading branch information
Showing
13 changed files
with
195 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "jsu", | ||
"version": "8", | ||
"version": "11", | ||
"description": "", | ||
"main": "gulpfile.js", | ||
"type": "module", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
export class PollingManager { | ||
constructor (fct, interval, enabled) { | ||
/* | ||
This class purpose is to handle a function used for polling. | ||
The polling will be stopped when page is hidden and restarted if visible. | ||
Arguments: | ||
- `fct`: | ||
The function to use for the polling. | ||
It will receive a callback function as argument which must be | ||
called once the function is done (after a request for example). | ||
- `interval`: | ||
The interval is the time between the end of the execution of the | ||
function and the next execution of the function. | ||
This means that the execution duration of the function will delay | ||
the next run. | ||
- `enabled`: | ||
Boolean to indicate if the polling should be initially enabled. | ||
Default is `true`. | ||
*/ | ||
this.enabled = false; | ||
this.timeoutId = null; | ||
this.running = false; | ||
this.lastRun = 0; | ||
this.interval = interval; | ||
this.fct = fct; | ||
|
||
if (enabled === true || enabled === undefined) { | ||
this.enable(); | ||
} | ||
document.addEventListener('visibilitychange', function () { | ||
if (document.visibilityState === 'visible') { | ||
this.resume(); | ||
} else { | ||
this.cancel(); | ||
} | ||
}.bind(this)); | ||
} | ||
enable () { | ||
if (!this.enabled) { | ||
this.enabled = true; | ||
this.resume(); | ||
} | ||
} | ||
disable () { | ||
if (this.enabled) { | ||
this.enabled = false; | ||
this.cancel(); | ||
} | ||
} | ||
run () { | ||
if (this.enabled && !this.running) { | ||
this.running = true; | ||
this.cancel(); | ||
this.fct(function () { | ||
this.lastRun = (new Date()).getTime(); | ||
this.running = false; | ||
this.plan(this.interval); | ||
}.bind(this)); | ||
} | ||
} | ||
plan (delay) { | ||
if (this.enabled && !this.timeoutId && document.visibilityState === 'visible') { | ||
this.timeoutId = setTimeout(this.run.bind(this), delay); | ||
} | ||
} | ||
resume () { | ||
const now = (new Date()).getTime(); | ||
const delay = Math.max(this.lastRun + this.interval - now, 1); | ||
this.plan(delay); | ||
} | ||
cancel () { | ||
if (this.timeoutId !== null) { | ||
clearTimeout(this.timeoutId); | ||
this.timeoutId = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* globals require, describe, it */ | ||
const assert = require('assert'); | ||
import { PollingManager } from '../src/lib/polling-manager.js'; | ||
|
||
describe('PollingManager', () => { | ||
it('should handle polling function', async () => { | ||
const sleep = (time) => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, time); | ||
}); | ||
}; | ||
|
||
const start = new Date().getTime(); | ||
const calls = []; | ||
const polling = new PollingManager(function (callback) { | ||
calls.push(new Date().getTime() - start); | ||
callback(); | ||
}, 1000); | ||
|
||
// Test call after init | ||
await sleep(500); | ||
assert(calls.length == 1); | ||
assert(calls[0] < 100, `${calls[0]} ~= 0`); | ||
|
||
// Test call after interval | ||
await sleep(1000); | ||
assert(calls.length == 2); | ||
assert(Math.abs(calls[1] - 1000) < 100, `${calls[1]} ~= 1000`); | ||
|
||
// Test no call when disabled | ||
polling.disable(); | ||
await sleep(1000); | ||
assert(calls.length == 2); | ||
|
||
// Test immediate call after enabling because interval is already reached | ||
polling.enable(); | ||
await sleep(100); | ||
assert(calls.length == 3); | ||
assert(Math.abs(calls[2] - 2500) < 100, `${calls[2]} ~= 2500`); | ||
|
||
// Test no call after enabling because a too recent call was made | ||
polling.disable(); | ||
polling.enable(); | ||
await sleep(100); | ||
assert(calls.length == 3); | ||
|
||
// Cleanup test | ||
polling.disable(); | ||
}).timeout(5000); | ||
}); |
Oops, something went wrong.