@dxos/async 0.8.4-main.ae835ea → 0.8.4-main.bc674ce
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/dist/lib/browser/index.mjs +112 -12
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +112 -12
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/cleanup.d.ts +1 -0
- package/dist/types/src/cleanup.d.ts.map +1 -1
- package/dist/types/src/debounce.d.ts +15 -0
- package/dist/types/src/debounce.d.ts.map +1 -1
- package/dist/types/src/persistent-lifecycle.d.ts +2 -2
- package/dist/types/src/persistent-lifecycle.d.ts.map +1 -1
- package/dist/types/src/task-scheduling.d.ts +28 -0
- package/dist/types/src/task-scheduling.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +11 -7
- package/src/cleanup.ts +1 -0
- package/src/debounce.ts +52 -4
- package/src/persistent-lifecycle.ts +2 -2
- package/src/task-scheduling.ts +93 -0
|
@@ -64,29 +64,55 @@ var SubscriptionSet = class {
|
|
|
64
64
|
};
|
|
65
65
|
|
|
66
66
|
// src/debounce.ts
|
|
67
|
-
var
|
|
67
|
+
var delay = (cb, delay2 = 100) => {
|
|
68
|
+
let pending = false;
|
|
69
|
+
return (...args) => {
|
|
70
|
+
if (pending) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
pending = true;
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
try {
|
|
76
|
+
cb(...args);
|
|
77
|
+
} finally {
|
|
78
|
+
pending = false;
|
|
79
|
+
}
|
|
80
|
+
}, delay2);
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
var debounce = (cb, delay2 = 100) => {
|
|
68
84
|
let t;
|
|
69
85
|
return (...args) => {
|
|
70
86
|
clearTimeout(t);
|
|
71
|
-
t = setTimeout(() => cb(...args),
|
|
87
|
+
t = setTimeout(() => cb(...args), delay2);
|
|
72
88
|
};
|
|
73
89
|
};
|
|
74
|
-
var throttle = (cb,
|
|
90
|
+
var throttle = (cb, delay2 = 100) => {
|
|
75
91
|
let lastCall = 0;
|
|
76
92
|
return (...args) => {
|
|
77
93
|
const now = Date.now();
|
|
78
|
-
if (now - lastCall >=
|
|
94
|
+
if (now - lastCall >= delay2) {
|
|
79
95
|
cb(...args);
|
|
80
96
|
lastCall = now;
|
|
81
97
|
}
|
|
82
98
|
};
|
|
83
99
|
};
|
|
84
|
-
var debounceAndThrottle = (cb,
|
|
85
|
-
|
|
86
|
-
|
|
100
|
+
var debounceAndThrottle = (cb, delay2 = 100) => {
|
|
101
|
+
let timeout2;
|
|
102
|
+
let lastCall = 0;
|
|
87
103
|
return (...args) => {
|
|
88
|
-
|
|
89
|
-
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
const delta = now - lastCall;
|
|
106
|
+
clearTimeout(timeout2);
|
|
107
|
+
if (delta >= delay2) {
|
|
108
|
+
cb(...args);
|
|
109
|
+
lastCall = now;
|
|
110
|
+
} else {
|
|
111
|
+
timeout2 = setTimeout(() => {
|
|
112
|
+
cb(...args);
|
|
113
|
+
lastCall = Date.now();
|
|
114
|
+
}, delay2 - delta);
|
|
115
|
+
}
|
|
90
116
|
};
|
|
91
117
|
};
|
|
92
118
|
|
|
@@ -1046,6 +1072,78 @@ var DeferredTask = class {
|
|
|
1046
1072
|
await this._currentTask;
|
|
1047
1073
|
}
|
|
1048
1074
|
};
|
|
1075
|
+
var AsyncTask = class {
|
|
1076
|
+
#callback;
|
|
1077
|
+
#ctx = void 0;
|
|
1078
|
+
#scheduled = false;
|
|
1079
|
+
#currentTask = null;
|
|
1080
|
+
#nextTask = new Trigger();
|
|
1081
|
+
constructor(callback) {
|
|
1082
|
+
this.#callback = callback;
|
|
1083
|
+
}
|
|
1084
|
+
get scheduled() {
|
|
1085
|
+
return this.#scheduled;
|
|
1086
|
+
}
|
|
1087
|
+
/**
|
|
1088
|
+
* Context of the resource that owns the task.
|
|
1089
|
+
* When the context is disposed, the task is cancelled and cannot be scheduled again.
|
|
1090
|
+
*/
|
|
1091
|
+
// TODO(dmaretskyi): We don't really need to pass ctx in here, since close will also signal dispose.
|
|
1092
|
+
open(ctx) {
|
|
1093
|
+
this.#ctx = ctx;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* Closes the task and waits for it to finish if it is running.
|
|
1097
|
+
*/
|
|
1098
|
+
async close() {
|
|
1099
|
+
this.#ctx = void 0;
|
|
1100
|
+
await this.join();
|
|
1101
|
+
}
|
|
1102
|
+
[Symbol.asyncDispose]() {
|
|
1103
|
+
return this.close();
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Schedule the task to run asynchronously.
|
|
1107
|
+
*/
|
|
1108
|
+
schedule() {
|
|
1109
|
+
if (!this.#ctx || this.#ctx.disposed) {
|
|
1110
|
+
throw new Error("AsyncTask not open");
|
|
1111
|
+
}
|
|
1112
|
+
if (this.#scheduled) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
scheduleTask(this.#ctx, async () => {
|
|
1116
|
+
await this.#currentTask;
|
|
1117
|
+
if (!this.#ctx || this.#ctx.disposed) {
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
this.#scheduled = false;
|
|
1121
|
+
const completionTrigger = this.#nextTask;
|
|
1122
|
+
this.#nextTask = new Trigger();
|
|
1123
|
+
this.#currentTask = runInContextAsync(this.#ctx, () => this.#callback()).then(() => {
|
|
1124
|
+
completionTrigger.wake();
|
|
1125
|
+
});
|
|
1126
|
+
});
|
|
1127
|
+
this.#scheduled = true;
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Schedule the task to run and wait for it to finish.
|
|
1131
|
+
*/
|
|
1132
|
+
async runBlocking() {
|
|
1133
|
+
if (this.#ctx?.disposed) {
|
|
1134
|
+
throw new ContextDisposedError2();
|
|
1135
|
+
}
|
|
1136
|
+
this.schedule();
|
|
1137
|
+
await this.#nextTask.wait();
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Waits for the current task to finish if it is running.
|
|
1141
|
+
* Does not schedule a new task.
|
|
1142
|
+
*/
|
|
1143
|
+
async join() {
|
|
1144
|
+
await this.#currentTask;
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1049
1147
|
var runInContext = (ctx, fn) => {
|
|
1050
1148
|
try {
|
|
1051
1149
|
fn();
|
|
@@ -1518,13 +1616,13 @@ var UpdateScheduler = class {
|
|
|
1518
1616
|
await this._promise;
|
|
1519
1617
|
if (this._params.maxFrequency) {
|
|
1520
1618
|
const now = performance.now();
|
|
1521
|
-
const
|
|
1522
|
-
if (
|
|
1619
|
+
const delay2 = this._lastUpdateTime + TIME_PERIOD / this._params.maxFrequency - now;
|
|
1620
|
+
if (delay2 > 0) {
|
|
1523
1621
|
await new Promise((resolve) => {
|
|
1524
1622
|
const timeoutId = setTimeout(() => {
|
|
1525
1623
|
clearContext();
|
|
1526
1624
|
resolve();
|
|
1527
|
-
},
|
|
1625
|
+
}, delay2);
|
|
1528
1626
|
const clearContext = this._ctx.onDispose(() => {
|
|
1529
1627
|
clearTimeout(timeoutId);
|
|
1530
1628
|
resolve();
|
|
@@ -1568,6 +1666,7 @@ var UpdateScheduler = class {
|
|
|
1568
1666
|
}
|
|
1569
1667
|
};
|
|
1570
1668
|
export {
|
|
1669
|
+
AsyncTask,
|
|
1571
1670
|
CancellableObservableProvider,
|
|
1572
1671
|
DeferredTask,
|
|
1573
1672
|
Event,
|
|
@@ -1594,6 +1693,7 @@ export {
|
|
|
1594
1693
|
combine,
|
|
1595
1694
|
debounce,
|
|
1596
1695
|
debounceAndThrottle,
|
|
1696
|
+
delay,
|
|
1597
1697
|
dumpLeaks,
|
|
1598
1698
|
interval,
|
|
1599
1699
|
latch,
|