@dxos/async 0.4.10-main.35b4a08 → 0.4.10-main.36e0428

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.
package/src/trigger.ts CHANGED
@@ -43,7 +43,8 @@ export type TriggerOptions = {
43
43
  */
44
44
  export class Trigger<T = void> {
45
45
  private _promise!: Promise<T>;
46
- private _wake!: (value: T | PromiseLike<T>) => void;
46
+ private _resolve!: (value: T | PromiseLike<T>) => void;
47
+ private _reject!: (error: Error) => void;
47
48
 
48
49
  constructor(private _options: TriggerOptions = { autoReset: false }) {
49
50
  this.reset();
@@ -62,9 +63,10 @@ export class Trigger<T = void> {
62
63
 
63
64
  /**
64
65
  * Wake blocked callers (if any).
66
+ * NOOP if the trigger is already resolved.
65
67
  */
66
68
  wake(value: T) {
67
- this._wake(value);
69
+ this._resolve(value);
68
70
  if (this._options.autoReset) {
69
71
  return this.reset();
70
72
  }
@@ -76,18 +78,21 @@ export class Trigger<T = void> {
76
78
  * Reset promise (new waiters will wait).
77
79
  */
78
80
  reset() {
79
- this._promise = new Promise<T>((resolve) => {
80
- this._wake = resolve;
81
+ this._promise = new Promise<T>((resolve, reject) => {
82
+ this._resolve = resolve;
83
+ this._reject = reject;
81
84
  });
85
+ this._promise.catch(() => {}); // Prevent unhandled promise rejections.
82
86
 
83
87
  return this;
84
88
  }
85
89
 
86
90
  /**
87
91
  * Throw error to blocked callers (if any).
92
+ * NOOP if the trigger is already resolved.
88
93
  */
89
94
  throw(error: Error) {
90
- this._wake(Promise.reject(error));
95
+ this._reject(error);
91
96
  if (this._options.autoReset) {
92
97
  return this.reset();
93
98
  }
@@ -51,7 +51,6 @@ export class UpdateScheduler {
51
51
  if (this._params.maxFrequency) {
52
52
  const now = performance.now();
53
53
  const delay = this._lastUpdateTime + TIME_PERIOD / this._params.maxFrequency - now;
54
-
55
54
  if (delay > 0) {
56
55
  await new Promise<void>((resolve) => {
57
56
  const clearContext = this._ctx.onDispose(() => {