mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
23 lines
504 B
TypeScript
23 lines
504 B
TypeScript
import { assert } from './assert'
|
|
|
|
export class Limiter {
|
|
private message: string
|
|
private base = 0
|
|
private limit: number
|
|
constructor (resource: string, limit: number) {
|
|
this.message = `${resource} limit exceeded`
|
|
this.limit = limit
|
|
}
|
|
use (count: number) {
|
|
if (+count > 0) {
|
|
assert(this.base + +count <= this.limit, this.message)
|
|
this.base += +count
|
|
}
|
|
}
|
|
check (count: number) {
|
|
if (+count > 0) {
|
|
assert(+count <= this.limit, this.message)
|
|
}
|
|
}
|
|
}
|