audio-context-timers
Version:
A replacement for setInterval() and setTimeout() which works in unfocused windows.
76 lines (72 loc) • 3.36 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fast-unique-numbers'), require('standardized-audio-context')) :
typeof define === 'function' && define.amd ? define(['exports', 'fast-unique-numbers', 'standardized-audio-context'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.audioContextTimers = {}, global.fastUniqueNumbers, global.standardizedAudioContext));
})(this, (function (exports, fastUniqueNumbers, standardizedAudioContext) { 'use strict';
var MINIMAL_AUDIO_CONTEXT = new standardizedAudioContext.MinimalAudioContext();
var AUDIO_BUFFER = new standardizedAudioContext.AudioBuffer({
length: 2,
sampleRate: MINIMAL_AUDIO_CONTEXT.sampleRate
});
var SAMPLE_DURATION = 2 / MINIMAL_AUDIO_CONTEXT.sampleRate;
var SCHEDULED_TIMEOUT_FUNCTIONS = new Map();
var SCHEDULED_INTERVAL_FUNCTIONS = new Map();
var callIntervalFunction = function callIntervalFunction(id, type) {
var functions = type === 'interval' ? SCHEDULED_INTERVAL_FUNCTIONS : SCHEDULED_TIMEOUT_FUNCTIONS;
if (functions.has(id)) {
var func = functions.get(id);
if (func !== undefined) {
func();
if (type === 'timeout') {
SCHEDULED_TIMEOUT_FUNCTIONS["delete"](id);
}
}
}
};
var _scheduleFunction = function scheduleFunction(id, delay, type) {
var now = performance.now();
var audioBufferSourceNode = new standardizedAudioContext.AudioBufferSourceNode(MINIMAL_AUDIO_CONTEXT, {
buffer: AUDIO_BUFFER
});
audioBufferSourceNode.onended = function () {
var elapsedTime = performance.now() - now;
if (elapsedTime >= delay) {
callIntervalFunction(id, type);
} else {
_scheduleFunction(id, delay - elapsedTime, type);
}
audioBufferSourceNode.disconnect(MINIMAL_AUDIO_CONTEXT.destination);
};
audioBufferSourceNode.connect(MINIMAL_AUDIO_CONTEXT.destination);
audioBufferSourceNode.start(Math.max(0, MINIMAL_AUDIO_CONTEXT.currentTime + delay / 1000 - SAMPLE_DURATION));
};
var clearInterval = function clearInterval(id) {
SCHEDULED_INTERVAL_FUNCTIONS["delete"](id);
};
var clearTimeout = function clearTimeout(id) {
SCHEDULED_TIMEOUT_FUNCTIONS["delete"](id);
};
var setInterval = function setInterval(func, delay) {
var id = fastUniqueNumbers.generateUniqueNumber(SCHEDULED_INTERVAL_FUNCTIONS);
SCHEDULED_INTERVAL_FUNCTIONS.set(id, function () {
func();
_scheduleFunction(id, delay, 'interval');
});
_scheduleFunction(id, delay, 'interval');
return id;
};
var setTimeout = function setTimeout(func, delay) {
var id = fastUniqueNumbers.generateUniqueNumber(SCHEDULED_TIMEOUT_FUNCTIONS);
SCHEDULED_TIMEOUT_FUNCTIONS.set(id, func);
_scheduleFunction(id, delay, 'timeout');
return id;
};
Object.defineProperty(exports, "isSupported", {
enumerable: true,
get: function () { return standardizedAudioContext.isSupported; }
});
exports.clearInterval = clearInterval;
exports.clearTimeout = clearTimeout;
exports.setInterval = setInterval;
exports.setTimeout = setTimeout;
}));