@byloth/core 2.0.0-rc.6 → 2.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,17 +3,21 @@ import { TimeUnit } from "../../utils/date.js";
3
3
  import { FatalErrorException, RangeException, RuntimeException } from "../exceptions/index.js";
4
4
  import { DeferredPromise, SmartPromise } from "../promises/index.js";
5
5
 
6
+ import Publisher from "../callbacks/publisher.js";
6
7
  import GameLoop from "../game-loop.js";
7
- import Publisher from "../publisher.js";
8
+
9
+ interface CountdownEventMap
10
+ {
11
+ start: () => void;
12
+ stop: (reason: unknown) => void;
13
+ tick: (remainingTime: number) => void;
14
+ expire: () => void;
15
+ }
8
16
 
9
17
  export default class Countdown extends GameLoop
10
18
  {
11
19
  protected _deferrer?: DeferredPromise<void>;
12
-
13
- protected _expirer: Publisher;
14
- protected _starter: Publisher;
15
- protected _stopper: Publisher<[unknown]>;
16
- protected _ticker: Publisher<[number]>;
20
+ protected _publisher: Publisher<CountdownEventMap>;
17
21
 
18
22
  protected _duration: number;
19
23
  public get duration(): number
@@ -31,23 +35,19 @@ export default class Countdown extends GameLoop
31
35
  const callback = () =>
32
36
  {
33
37
  const remainingTime = this.remainingTime;
34
- this._ticker.publish(remainingTime);
38
+ this._publisher.publish("tick", remainingTime);
35
39
 
36
40
  if (remainingTime <= 0)
37
41
  {
38
42
  this._deferrerStop();
39
43
 
40
- this._expirer.publish();
44
+ this._publisher.publish("expire");
41
45
  }
42
46
  };
43
47
 
44
48
  super(callback, msIfNotBrowser);
45
49
 
46
- this._expirer = new Publisher();
47
- this._starter = new Publisher();
48
- this._stopper = new Publisher();
49
- this._ticker = new Publisher();
50
-
50
+ this._publisher = new Publisher();
51
51
  this._duration = duration;
52
52
  }
53
53
 
@@ -72,7 +72,7 @@ export default class Countdown extends GameLoop
72
72
  this._deferrer = new DeferredPromise();
73
73
  super.start(this.duration - remainingTime);
74
74
 
75
- this._starter.publish();
75
+ this._publisher.publish("start");
76
76
 
77
77
  return this._deferrer;
78
78
  }
@@ -80,31 +80,31 @@ export default class Countdown extends GameLoop
80
80
  {
81
81
  this._deferrerStop(reason);
82
82
 
83
- this._stopper.publish(reason);
83
+ this._publisher.publish("stop", reason);
84
84
  }
85
85
 
86
86
  public onExpire(callback: () => void): () => void
87
87
  {
88
- return this._expirer.subscribe(callback);
88
+ return this._publisher.subscribe("expire", callback);
89
89
  }
90
90
 
91
91
  public onStart(callback: () => void): () => void
92
92
  {
93
- return this._starter.subscribe(callback);
93
+ return this._publisher.subscribe("start", callback);
94
94
  }
95
95
  public onStop(callback: (reason?: unknown) => void): () => void
96
96
  {
97
- return this._stopper.subscribe(callback);
97
+ return this._publisher.subscribe("stop", callback);
98
98
  }
99
99
 
100
100
  public onTick(callback: (remainingTime: number) => void, tickStep = 0): () => void
101
101
  {
102
102
  if (tickStep < 0) { throw new RangeException("The tick step must be a non-negative number."); }
103
- if (tickStep === 0) { return this._ticker.subscribe(callback); }
103
+ if (tickStep === 0) { return this._publisher.subscribe("tick", callback); }
104
104
 
105
105
  let lastTick = 0;
106
106
 
107
- return this._ticker.subscribe((remainingTime: number) =>
107
+ return this._publisher.subscribe("tick", (remainingTime: number) =>
108
108
  {
109
109
  if ((lastTick - remainingTime) < tickStep) { return; }
110
110
 
@@ -11,6 +11,7 @@ export type {
11
11
  export type {
12
12
  GeneratorFunction,
13
13
  AsyncGeneratorFunction,
14
+ MaybeAsyncGeneratorFunction,
14
15
  Iteratee,
15
16
  MaybeAsyncIteratee,
16
17
  TypeGuardIteratee,
@@ -31,6 +32,7 @@ export type {
31
32
  } from "./json/types.js";
32
33
 
33
34
  export type {
35
+ LongRunningTaskOptions,
34
36
  MaybePromise,
35
37
  FulfilledHandler,
36
38
  RejectedHandler,
@@ -40,4 +42,4 @@ export type {
40
42
 
41
43
  } from "./promises/types.js";
42
44
 
43
- export type { Subscriber } from "./publisher.js";
45
+ export type { Callback } from "./callbacks/types.js";
@@ -1,9 +1,14 @@
1
1
  export function delay(milliseconds: number): Promise<void>
2
2
  {
3
- return new Promise<void>((resolve) => setTimeout(resolve, milliseconds));
3
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
4
4
  }
5
5
 
6
6
  export function nextAnimationFrame(): Promise<void>
7
7
  {
8
- return new Promise<void>((resolve) => requestAnimationFrame(() => resolve()));
8
+ return new Promise((resolve) => requestAnimationFrame(() => resolve()));
9
+ }
10
+
11
+ export function yieldToEventLoop(): Promise<void>
12
+ {
13
+ return new Promise((resolve) => setTimeout(resolve));
9
14
  }
@@ -1,6 +1,6 @@
1
1
  import Random from "./random.js";
2
2
 
3
- export { delay, nextAnimationFrame } from "./async.js";
3
+ export { delay, nextAnimationFrame, yieldToEventLoop } from "./async.js";
4
4
  export { dateDifference, dateRange, dateRound, TimeUnit } from "./date.js";
5
5
  export { loadScript } from "./dom.js";
6
6
  export { chain, count, enumerate, range, shuffle, unique, zip } from "./iterator.js";
@@ -1,39 +0,0 @@
1
- import { ReferenceException } from "./exceptions/index.js";
2
-
3
- export type Subscriber<A extends unknown[] = [], R = void> = (...args: A) => R;
4
-
5
- export default class Publisher<A extends unknown[] = [], R = void>
6
- {
7
- protected _subscribers: Subscriber<A, R>[];
8
-
9
- public constructor()
10
- {
11
- this._subscribers = [];
12
- }
13
-
14
- public subscribe(subscriber: Subscriber<A, R>): () => void
15
- {
16
- this._subscribers.push(subscriber);
17
-
18
- return () =>
19
- {
20
- const index = this._subscribers.indexOf(subscriber);
21
- if (index < 0)
22
- {
23
- throw new ReferenceException("Unable to unsubscribe the required subscriber. " +
24
- "The subscription was already unsubscribed.");
25
- }
26
-
27
- this._subscribers.splice(index, 1);
28
- };
29
- }
30
-
31
- public publish(...args: A): R[]
32
- {
33
- return this._subscribers
34
- .slice()
35
- .map((subscriber) => subscriber(...args));
36
- }
37
-
38
- public readonly [Symbol.toStringTag]: string = "Publisher";
39
- }