UNPKG

@stnekroman/tstools

Version:

Set of handy tools for TypeScript development

75 lines (74 loc) 2.37 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RefSet = void 0; const decorators_1 = require("../decorators"); const RefCountedValue_1 = require("./RefCountedValue"); class RefSet { constructor() { this.map = new Map(); } has(value) { return this.map.has(value); } add(value) { let refCountedValue = this.map.get(value); if (!refCountedValue) { refCountedValue = new RefCountedValue_1.RefCountedValue(void 0); this.map.set(value, refCountedValue); return refCountedValue.getRefCount(); } else { return refCountedValue.increment(); } } delete(value, allRefs = false) { if (allRefs) { this.map.delete(value); return 0; } else { const refCountedValue = this.map.get(value)?.decrement() ?? 0; if (refCountedValue <= 0) { this.map.delete(value); } return refCountedValue; } } clear() { this.map.clear(); } refs(value) { const refCountedValue = this.map.get(value); return refCountedValue?.getRefCount() ?? 0; } size() { return this.map.size; } keys() { return this.map.keys(); } *[Symbol.iterator]() { for (const key of this.map.keys()) { yield key; } } } exports.RefSet = RefSet; __decorate([ (decorators_1.Implements) ], RefSet.prototype, "has", null); __decorate([ (decorators_1.Implements) ], RefSet.prototype, "add", null); __decorate([ (decorators_1.Implements) ], RefSet.prototype, "delete", null); __decorate([ (decorators_1.Implements) ], RefSet.prototype, "clear", null);