Skip to content

Commit

Permalink
roughly 99% real time interval accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0ursTruly committed Jun 22, 2024
1 parent 558b148 commit 5b09204
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# timer
custom and more accurate setInterval and setTimeout functionality that is not nerfed for things like when the browser tab is not in focus

it is kind of annoying that the workflow jobs fail :(
Roughly 99% accurate _real_ time intervals in JavaScript

![alt text](image.png)
33 changes: 9 additions & 24 deletions prev_timer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//was trying to find out how to use channels but have them stop keeping the process alive

(function(channelNameSize){
const crypto=typeof window==="undefined"?require('node:crypto'):window.crypto;
const mapping=[], typedarray=new Uint8Array(channelNameSize), map=new Map();
for(let i=0;i<256;i++) mapping[i]=String.fromCharCode(i);
function randomChannel(input=true){
Expand All @@ -10,8 +11,6 @@
map.set(str,input);
return str;
}

//timer engine begin
const channel=randomChannel(), sender=new BroadcastChannel(channel), receiver=new BroadcastChannel(channel);
map.delete(channel); //it isn't needed to be unique towards other map entries
function manageTimer(timer,ID){
Expand All @@ -23,34 +22,20 @@
else timer.start=now;
}
}
function listener(){
if(!map.size) return deactivate();
sender.postMessage(null); //repeat the channel messaging IF TIMER(S) EXIST

//timer engine begin
receiver.addEventListener('message',function(){
sender.postMessage(null); //repeat the channel messaging
//the idea here is something that must be waited on but doesn't resolve quickly enough to hang the process
map.forEach(manageTimer);
}
let active=false;
function activate(){
if(active) return null;
console.log("activated")
receiver.addEventListener('message',listener);
sender.postMessage(null);
active=true;
}
function deactivate(){
if(!active) return null;
console.log("deactivated")
receiver.removeEventListener('message',listener);
active=false;
}
});
sender.postMessage(null); //start the channel messaging
//timer engine end

function timeout(userFN,ms){
activate();
return randomChannel({userFN,ms,repeat:false,start:performance.now()});
}
function interval(userFN,ms){
activate();
return randomChannel({userFN,ms,repeat:true,start:performance.now()});
}
async function wait(ms){
Expand All @@ -60,7 +45,7 @@
}
function clear(ID){return map.delete(ID)}


//exports
const timer={timeout,interval,wait,clear};
if(typeof window!=="undefined") window.timer=timer; //browser
Expand Down
5 changes: 2 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const test=require('node:test'), assert=require('node:assert'), timer=require('.
{interval_calls:p1,setInterval_calls:p2}
)
resolve()
},5e3)
},5e2)
return await p
})
await test("2) Several Intervals Skill Gap",async function(){
Expand All @@ -28,7 +28,7 @@ const test=require('node:test'), assert=require('node:assert'), timer=require('.
}
setTimeout(_=>{
assert.ok(p1>p2)
for(let i=0;i>t1.length;i++){
for(let i=0;i<t1.length;i++){
timer.clear(t1[i])
clearInterval(t2[i])
}
Expand All @@ -40,5 +40,4 @@ const test=require('node:test'), assert=require('node:assert'), timer=require('.
},5e3)
return await p
})
timer.wait(10).then(_=>process.exit(0))
})()
34 changes: 26 additions & 8 deletions timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
map.set(str,input);
return str;
}
const channel=randomChannel(), sender=new BroadcastChannel(channel), receiver=new BroadcastChannel(channel);

//timer engine begin
let channel=randomChannel(), sender=null, receiver=null;
map.delete(channel); //it isn't needed to be unique towards other map entries
function manageTimer(timer,ID){
//manages each timer in the map of timers
Expand All @@ -21,20 +23,36 @@
else timer.start=now;
}
}

//timer engine begin
receiver.addEventListener('message',function(){
sender.postMessage(null); //repeat the channel messaging
function listener(){
if(!map.size) return deactivate();
sender.postMessage(null); //repeat the channel messaging IF TIMER(S) EXIST
//the idea here is something that must be waited on but doesn't resolve quickly enough to hang the process
map.forEach(manageTimer);
});
sender.postMessage(null); //start the channel messaging
}
let active=false;
function activate(){
if(active) return null;
sender=new BroadcastChannel(channel);
receiver=new BroadcastChannel(channel);
receiver.addEventListener('message',listener);
sender.postMessage(null);
active=true;
}
function deactivate(){
if(!active) return null;
receiver.removeEventListener('message',listener);
receiver.close();
sender.close();
active=false;
}
//timer engine end

function timeout(userFN,ms){
activate();
return randomChannel({userFN,ms,repeat:false,start:performance.now()});
}
function interval(userFN,ms){
activate();
return randomChannel({userFN,ms,repeat:true,start:performance.now()});
}
async function wait(ms){
Expand All @@ -44,7 +62,7 @@
}
function clear(ID){return map.delete(ID)}


//exports
const timer={timeout,interval,wait,clear};
if(typeof window!=="undefined") window.timer=timer; //browser
Expand Down

0 comments on commit 5b09204

Please sign in to comment.