| 1 |
// utilities which declutter |
| 2 |
// promises which hold back releases |
| 3 |
// of certain async events |
| 4 |
// for internal usecases only |
| 5 |
import { equals, reject } from "rambda/dist/rambda.esm.js"; |
| 6 |
import { clearArray } from "./clear-array"; |
| 7 |
|
| 8 |
export class EventPromises { |
| 9 |
constructor() { |
| 10 |
this.timeoutTimers = []; |
| 11 |
|
| 12 |
this.startPromise = undefined; |
| 13 |
this.startResolver = undefined; |
| 14 |
|
| 15 |
this.stopPromise = undefined; |
| 16 |
this.stopResolver = undefined; |
| 17 |
|
| 18 |
this.pausePromise = undefined; |
| 19 |
this.pauseResolver = undefined; |
| 20 |
|
| 21 |
this.resumePromise = undefined; |
| 22 |
this.resumeResolver = undefined; |
| 23 |
|
| 24 |
this.createStartPromise = this.createStartPromise.bind(this); |
| 25 |
this.releaseStartPromise = this.releaseStartPromise.bind(this); |
| 26 |
|
| 27 |
this.createStopPromise = this.createStopPromise.bind(this); |
| 28 |
this.releaseStopPromise = this.releaseStopPromise.bind(this); |
| 29 |
|
| 30 |
this.createPausePromise = this.createPausePromise.bind(this); |
| 31 |
this.releasePausePromise = this.releasePausePromise.bind(this); |
| 32 |
|
| 33 |
this.createResumePromise = this.createResumePromise.bind(this); |
| 34 |
this.releaseResumePromise = this.releaseResumePromise.bind(this); |
| 35 |
|
| 36 |
this.waitForStart = this.waitForStart.bind(this); |
| 37 |
this.waitForStop = this.waitForStop.bind(this); |
| 38 |
|
| 39 |
this.isWaitingToStart = this.isWaitingToStart.bind(this); |
| 40 |
this.isWaitingToStop = this.isWaitingToStop.bind(this); |
| 41 |
} |
| 42 |
|
| 43 |
isWaitingToStart() { |
| 44 |
return typeof this.startPromise !== "undefined"; |
| 45 |
} |
| 46 |
|
| 47 |
isWaitingToStop() { |
| 48 |
return typeof this.stopPromise !== "undefined"; |
| 49 |
} |
| 50 |
|
| 51 |
isWaiting(eventAsking) { |
| 52 |
if (typeof this.startPromise !== "undefined") { |
| 53 |
console.error(`cannot ${eventAsking} while starting, did you forget to 'await'?`); |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
if (typeof this.stopPromise !== "undefined") { |
| 58 |
console.error(`cannot ${eventAsking} while stopping, did you forget to 'await'?`); |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
if (typeof this.pausePromise !== "undefined") { |
| 63 |
console.error(`cannot ${eventAsking} while pausing, did you forget to 'await'?`); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
if (typeof this.resumePromise !== "undefined") { |
| 68 |
console.error(`cannot ${eventAsking} while resuming, did you forget to 'await'?`); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
async waitForStart() { |
| 76 |
return this.startPromise ? await this.startPromise : -1; |
| 77 |
} |
| 78 |
|
| 79 |
async waitForStop() { |
| 80 |
return this.stopPromise ? this.stopPromise : -1; |
| 81 |
} |
| 82 |
|
| 83 |
async waitForPause() { |
| 84 |
return this.pausePromise ? this.pausePromise : -1; |
| 85 |
} |
| 86 |
|
| 87 |
async waitForResume() { |
| 88 |
return this.resumePromise ? this.resumePromise : -1; |
| 89 |
} |
| 90 |
|
| 91 |
createStartPromise() { |
| 92 |
if (!this.startPromise) { |
| 93 |
this.startPromise = new Promise((resolve) => { |
| 94 |
this.startResolver = resolve; |
| 95 |
const timer = setTimeout(() => { |
| 96 |
this.timeoutTimers = reject(equals(timer), this.timeoutTimers); |
| 97 |
if (this.startPromise) { |
| 98 |
console.warn("start promise timed out"); |
| 99 |
this.startResolver(); |
| 100 |
delete this.startResolver; |
| 101 |
this.startPromise && delete this.startPromise; |
| 102 |
} |
| 103 |
}, 2000); |
| 104 |
this.timeoutTimers.push(timer); |
| 105 |
}); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
releaseStartPromise() { |
| 110 |
// first timer cleanup |
| 111 |
try { |
| 112 |
this.timeoutTimers.forEach(clearTimeout); |
| 113 |
clearArray(this.timeoutTimers); |
| 114 |
} catch (error) { |
| 115 |
console.error(error); |
| 116 |
} |
| 117 |
// then resolve |
| 118 |
if (this.startResolver) { |
| 119 |
this.startResolver(); |
| 120 |
delete this.startResolver; |
| 121 |
} |
| 122 |
if (this.startPromise) { |
| 123 |
delete this.startPromise; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
createStopPromise() { |
| 128 |
if (!this.stopPromise) { |
| 129 |
this.stopPromise = new Promise((resolve) => { |
| 130 |
this.stopResolver = resolve; |
| 131 |
const timer = setTimeout(() => { |
| 132 |
this.timeoutTimers = reject(equals(timer), this.timeoutTimers); |
| 133 |
if (this.stopPromise) { |
| 134 |
console.warn("stop promise timed out"); |
| 135 |
this.stopResolver(); |
| 136 |
delete this.stopResolver; |
| 137 |
this.stopPromise && delete this.stopPromise; |
| 138 |
} |
| 139 |
}, 2000); |
| 140 |
this.timeoutTimers.push(timer); |
| 141 |
}); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
releaseStopPromise() { |
| 146 |
// first timer cleanup |
| 147 |
try { |
| 148 |
this.timeoutTimers.forEach(clearTimeout); |
| 149 |
clearArray(this.timeoutTimers); |
| 150 |
} catch (error) { |
| 151 |
console.error(error); |
| 152 |
} |
| 153 |
// then resolve |
| 154 |
if (this.stopResolver) { |
| 155 |
this.stopResolver(); |
| 156 |
delete this.stopResolver; |
| 157 |
} |
| 158 |
if (this.stopPromise) { |
| 159 |
delete this.stopPromise; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
createPausePromise() { |
| 164 |
if (!this.pausePromise) { |
| 165 |
this.pausePromise = new Promise((resolve) => { |
| 166 |
this.pauseResolver = resolve; |
| 167 |
const timer = setTimeout(() => { |
| 168 |
this.timeoutTimers = reject(equals(timer), this.timeoutTimers); |
| 169 |
if (this.pausePromise) { |
| 170 |
console.warn("pause promise timed out"); |
| 171 |
this.pauseResolver(); |
| 172 |
delete this.pauseResolver; |
| 173 |
this.pausePromise && delete this.pausePromise; |
| 174 |
} |
| 175 |
}, 2000); |
| 176 |
this.timeoutTimers.push(timer); |
| 177 |
}); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
releasePausePromise() { |
| 182 |
// first timer cleanup |
| 183 |
try { |
| 184 |
this.timeoutTimers.forEach(clearTimeout); |
| 185 |
clearArray(this.timeoutTimers); |
| 186 |
} catch (error) { |
| 187 |
console.error(error); |
| 188 |
} |
| 189 |
// then resolve |
| 190 |
if (this.pauseResolver) { |
| 191 |
this.pauseResolver(); |
| 192 |
delete this.pauseResolver; |
| 193 |
} |
| 194 |
if (this.pausePromise) { |
| 195 |
delete this.pausePromise; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
createResumePromise() { |
| 200 |
if (!this.resumePromise) { |
| 201 |
this.resumePromise = new Promise((resolve) => { |
| 202 |
this.resumeResolver = resolve; |
| 203 |
const timer = setTimeout(() => { |
| 204 |
this.timeoutTimers = reject(equals(timer), this.timeoutTimers); |
| 205 |
if (this.resumePromise) { |
| 206 |
console.warn("resume promise timed out"); |
| 207 |
this.resumeResolver(); |
| 208 |
delete this.resumeResolver; |
| 209 |
this.resumePromise && delete this.resumePromise; |
| 210 |
} |
| 211 |
}, 2000); |
| 212 |
this.timeoutTimers.push(timer); |
| 213 |
}); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
releaseResumePromise() { |
| 218 |
// first timer cleanup |
| 219 |
try { |
| 220 |
this.timeoutTimers.forEach(clearTimeout); |
| 221 |
clearArray(this.timeoutTimers); |
| 222 |
} catch (error) { |
| 223 |
console.error(error); |
| 224 |
} |
| 225 |
// then resolve |
| 226 |
if (this.resumeResolver) { |
| 227 |
this.resumeResolver(); |
| 228 |
delete this.resumeResolver; |
| 229 |
} |
| 230 |
if (this.resumePromise) { |
| 231 |
delete this.resumePromise; |
| 232 |
} |
| 233 |
} |
| 234 |
} |