![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@internetarchive/promised-singleton
Advanced tools
A generic Typescript class for lazy-loading singletons.
PromisedSingleton
is a generic wrapper for an asynchronously generated object that you only
want one instance of (a singleton).
We often want to generate a single instance of an object, but when that instance is asynchronously generated, you may have many consumers requesting that instance at the same time. The Promises don't automatically chain so you have to do some gatekeeping to make sure only one instance gets created.
PromisedSingleton
ensures that no matter how many callers request the object,
only one instance gets created.
It gets initialized with a generator Promise
that generates the singleton.
When get()
is first called, it executes the Promise, caches its results and
returns it to the caller.
If more callers call it in the interim, it chains the promises and when the singleton is created, it resolves them all.
npm i @internetarchive/promised-singleton
// foo-service-provider.ts
import { PromisedSingleton } from '@internetarchive/promised-singleton';
export class FooServiceProvider {
// Using a Promise
fooService = new PromisedSingleton<FooService>({
generator: (): Promise<FooService> => new Promise(resolve => {
const service = new FooService();
service.setup().then(service => resolve(service))
})
});
// Using an async function
barService = new PromisedSingleton<BarService>({
generator: async (): Promise<BarService> => {
const service = new BarService();
await service.setup();
return service;
})
});
}
// consumer.ts
import { FooServiceProvider } from './foo-service-provider';
export class Consumer1 {
// Passing the same FooServiceProvider into the consumers
constructor(serviceProvider: FooServiceProvider) {
this.serviceProvider = serviceProvider
}
async setup() {
// Call `.get()` on the property to fetch an instance of `FooService`
// Many consumers can call `get()` and they will all receive the same instance
try {
const service = await this.serviceProvider.fooService.get();
} catch (error) {
console.error('error fetching service singleton', error);
}
const result = await service.fetchFoos();
}
}
To scan the project for linting errors, run
npm run lint
You can lint with ESLint and Prettier individually as well
npm run lint:eslint
npm run lint:prettier
To automatically fix many linting errors, run
npm run format
You can format using ESLint and Prettier individually as well
npm run format:eslint
npm run format:prettier
To run the suite of karma tests, run
npm run test
To run the tests in watch mode (for TDD, for example), run
npm run test:watch
For most of the tools, the configuration is in the package.json
to reduce the amount of files in your project.
If you customize the configuration a lot, you can consider moving them to individual files.
FAQs
A class for lazy-loading singletons.
We found that @internetarchive/promised-singleton demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 12 open source maintainers 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.