
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
async-service-helper
Advanced tools
A helper class for services that need to start and stop asynchronously
Maintain state of a class that needs to asynchronously start and stop.
I've often found myself re-implementing logic for services that asynchronously start and stop and I need to manage state and wait for them to start before calling other methods, and I always make mistakes with race conditions. This helper class adds some basic state management for starting and stopping asynchronously that follows these rules:
stop()
then it
will end up stopped)start()
when the service is "stopped" calls the _start()
method
and resolves when it completes.start()
when the service is "starting" (e.g. start()
has been
called but has not completed) will not call _start()
again, but will
resolve once the service has startedstart()
when the service is "started" will resolve immediately
and do nothing._start()
or _stop()
throw, then the service is left in an
unrecoverable "error" state.start()
or stop()
when the service is in "error" state will
throw with the error from the error stateTo wait for the service to be in the "started" state from other methods, use
await this.started()
. Note that if the services is "stopping" or "stopped"
then this will await (e.g. queue) until next start.
Logic for calling stop()
and stopped()
follows the inverse of start()
and started()
.
npm install async-service-helper
const AsyncService = require('async-service-helper')
class MyService extends AsyncService {
async _start() {
await new Promise(res => setTimeout(res, 100))
}
async _stop() {
await new Promise(res => setTimeout(res, 100))
}
async doAfterStart() {
await this.started()
// Guaranteed that _start() has resolved and _stop() has not been called
}
}
const service = new MyService()
service.start() // no need to await, because we have a guard in doAfterStart()
doAfterStart().then(() => {})
Override this method in your implementing class. Must return a Promise.
Override this method in your implementing class. Must return a Promise.
Start service. If the service is starting or started, will resolve when the
service is started, and will not call _start()
more than once. If the
service is in the process of stopping, will wait until it stops before
starting and will not call _stop()
more than once. Any parameters are passed through to _start()
.
Returns [Promise][35]<void> Resolves when service is started
Stop the service.
Returns [Promise][35]<void> Resolves when service is stopped
Will resolve when the service is in started state. E.g. to ensure an async method only runs when the service is in "started" state, use:
await this.started()
Will reject if the service is in "error" state.
Note: If the service is in "stopping" or "stopped" state this will queue
until the next time the service starts. If this is not desirable behaviour,
check this.#state.value
first
Returns [Promise][35]<void>
Will resolve when the service is in stopped state. Less useful than
started()
E.g. to ensure an async method only runs when the service is in
"stopped" state, use:
await this.stopped()
Will reject if the service is in "error" state.
Note: If the service is in "starting" or "started" state this will queue
until the next time the service stops. If this is not desirable behaviour,
check this.#state.value
first
Returns [Promise][35]<void>
Attach a listener function to changes of state. listener
will be calls with the current service state (see below)
Remove a listener to changes of state.
PRs accepted.
Small note: If editing the README, please conform to the standard-readme specification.
MIT © 2022 Gregor MacLennan
FAQs
A helper class for services that need to start and stop asynchronously
The npm package async-service-helper receives a total of 1 weekly downloads. As such, async-service-helper popularity was classified as not popular.
We found that async-service-helper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.