Skip to content

Commit

Permalink
revert a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
steveseguin committed Dec 12, 2024
1 parent d23c0ed commit be9d2fa
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 403 deletions.
201 changes: 0 additions & 201 deletions sources/tiktok.js
Original file line number Diff line number Diff line change
@@ -1,207 +1,6 @@
(function() {


const TimeoutShim = {
audioCtx: null,
currentOscillatorId: 0,
oscillatorActive: false,
queue: [],
activeIntervals: new Set(),
MIN_OSCILLATOR_TIME: 200,

addToQueue(item) {
if (item.isInterval) {
this.activeIntervals.add(item.id);
}

const index = this.queue.findIndex(existing => existing.executeTime > item.executeTime);
if (index === -1) {
this.queue.push(item);
} else {
this.queue.splice(index, 0, item);
}

if (!this.oscillatorActive || (this.queue[0] === item)) {
this.restartOscillator();
}
},

createTimedOscillator() {
if (!this.queue.length) {
this.oscillatorActive = false;
return;
}

const oscillatorId = ++this.currentOscillatorId;

try {
if (!this.audioCtx) {
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
}

// Check if audio context is suspended
if (this.audioCtx.state === 'suspended') {
// Fall back to regular setTimeout for this execution
const item = this.queue[0];
const delay = Math.max(0, item.executeTime - performance.now());
this.originalSetTimeout(() => {
if (this.currentOscillatorId === oscillatorId) {
this.processQueue(item.executeTime + this.MIN_OSCILLATOR_TIME);
}
}, delay);
this.oscillatorActive = true;
return;
}

const oscillator = this.audioCtx.createOscillator();
const silence = this.audioCtx.createGain();
silence.gain.value = 0;
oscillator.connect(silence);
silence.connect(this.audioCtx.destination);

const timeStart = this.audioCtx.currentTime;
const now = performance.now();
const nextExecuteTime = this.queue[0].executeTime;
const batchEndTime = nextExecuteTime + this.MIN_OSCILLATOR_TIME;
const eventsInWindow = this.queue.filter(item => item.executeTime <= batchEndTime);
const lastEventTime = eventsInWindow.length > 0 ?
eventsInWindow[eventsInWindow.length - 1].executeTime :
nextExecuteTime;

const waitTime = Math.max(
this.MIN_OSCILLATOR_TIME,
lastEventTime - now
) / 1000;

oscillator.onended = () => {
oscillator.disconnect();
silence.disconnect();

if (this.currentOscillatorId === oscillatorId) {
this.processQueue(batchEndTime);
}
};

this.oscillatorActive = true;
oscillator.start(timeStart);
oscillator.stop(timeStart + waitTime);

} catch (e) {
console.log('Error in audio timing, falling back:', e);
// Fall back to regular setTimeout
const item = this.queue[0];
const delay = Math.max(0, item.executeTime - performance.now());
this.originalSetTimeout(() => {
if (this.currentOscillatorId === oscillatorId) {
this.processQueue(item.executeTime + this.MIN_OSCILLATOR_TIME);
}
}, delay);
this.oscillatorActive = true;
}
},

processQueue(batchEndTime) {
const now = performance.now();

while (this.queue.length && this.queue[0].executeTime <= batchEndTime) {
const item = this.queue.shift();

if (item.isInterval && !this.activeIntervals.has(item.id)) {
continue;
}

try {
item.callback();
} catch (e) {
console.log('Error in timer callback:', e);
}

if (item.isInterval && this.activeIntervals.has(item.id)) {
this.addToQueue({
id: item.id,
callback: item.callback,
executeTime: now + item.delay,
isInterval: true,
delay: item.delay
});
}
}

if (this.queue.length) {
this.restartOscillator();
} else {
this.oscillatorActive = false;
}
},

restartOscillator() {
this.currentOscillatorId++;
this.createTimedOscillator();
},

setTimeout(callback, delay, ...args) {
if (!callback || typeof callback !== 'function') return;
const timeoutId = Math.random();

this.addToQueue({
id: timeoutId,
callback: () => callback.apply(null, args),
executeTime: performance.now() + delay,
isInterval: false
});

return timeoutId;
},

setInterval(callback, delay, ...args) {
if (!callback || typeof callback !== 'function') return;
const intervalId = Math.random();

this.addToQueue({
id: intervalId,
callback: () => callback.apply(null, args),
executeTime: performance.now() + delay,
isInterval: true,
delay: delay
});

return intervalId;
},

clearInterval(id) {
this.activeIntervals.delete(id);
this.queue = this.queue.filter(item => item.id !== id);

if (this.queue.length === 0) {
this.currentOscillatorId++;
this.oscillatorActive = false;
}

return true;
},

clearTimeout(id) {
return this.clearInterval(id);
},

initialize() {
this.originalSetTimeout = window.setTimeout;
this.originalSetInterval = window.setInterval;
this.originalClearTimeout = window.clearTimeout;
this.originalClearInterval = window.clearInterval;

window.setTimeout = this.setTimeout.bind(this);
window.setInterval = this.setInterval.bind(this);
window.clearTimeout = this.clearTimeout.bind(this);
window.clearInterval = this.clearInterval.bind(this);

return this;
}
};

console.log("About to initialize TimeoutShim");
const initializedTimeoutShim = TimeoutShim.initialize();

const avatarCache = {
_cache: {},
MAX_SIZE: 300,
Expand Down
Loading

0 comments on commit be9d2fa

Please sign in to comment.