Browse Source

Server: Thread safety: maybe this time the ticks get awaited properly ...?

Christian Kahlau 3 years ago
parent
commit
3c89052b35
1 changed files with 3 additions and 7 deletions
  1. 3 7
      server/src/timer.class.ts

+ 3 - 7
server/src/timer.class.ts

@@ -1,7 +1,7 @@
 type Subscriber = {
 type Subscriber = {
   lastTick: number;
   lastTick: number;
   seconds: number;
   seconds: number;
-  tick: (() => void) | Promise<void>;
+  tick: () => void | Promise<void>;
   queued?: boolean;
   queued?: boolean;
 };
 };
 
 
@@ -45,16 +45,12 @@ export class Timer {
       const now = new Date();
       const now = new Date();
       const sub = this.queue.shift() as Subscriber;
       const sub = this.queue.shift() as Subscriber;
       sub.lastTick = now.getTime();
       sub.lastTick = now.getTime();
-      if (typeof sub.tick === 'function') {
-        sub.tick();
-      } else {
-        await sub.tick;
-      }
+      await sub.tick();
       sub.queued = false;
       sub.queued = false;
     }
     }
   }
   }
 
 
-  public subscribe(seconds: number, tick: (() => void) | Promise<void>) {
+  public subscribe(seconds: number, tick: () => void | Promise<void>) {
     const lastTick = new Date().getTime();
     const lastTick = new Date().getTime();
     const id =
     const id =
       Object.keys(this.subscribers)
       Object.keys(this.subscribers)