-
Notifications
You must be signed in to change notification settings - Fork 0
instance limit
awekrx edited this page May 29, 2026
·
1 revision
import { instanceLimit } from '@dev-suite/decorators/instance-limit'
class
Restrict number of class instances when resource duplication is unsafe.
- Manual static counters in constructors
- Ad-hoc guard logic spread across classes
class Scheduler {
private static created = 0;
constructor() {
Scheduler.created += 1;
if (Scheduler.created > 1) {
throw new Error('Only one Scheduler instance is allowed');
}
}
}import { instanceLimit } from '@dev-suite/decorators/instance-limit';
@instanceLimit({ maxInstances: 1, message: 'Only one Scheduler instance is allowed' })
class Scheduler {
constructor() {}
}- Removes repetitive static counter boilerplate
- Limit policy is explicit and reusable
class QueueWorker {
private static created = 0;
constructor() {
QueueWorker.created += 1;
if (QueueWorker.created > 4) throw new Error('worker pool overflow');
}
}import { instanceLimit } from '@dev-suite/decorators/instance-limit';
@instanceLimit({ maxInstances: 4 })
class QueueWorker {
constructor() {}
}- Supports bounded pools, not only singleton-style limits
- Cleaner constructor with no resource-accounting noise