1 /* Logizomai - Reference Counting for TypeScript
2 * Copyright (C) 2017 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 export abstract class Resource {
23 private retainers: number;
24 public finalizes: Array<() => void>;
31 public retain(): void {
35 public release(): void {
36 if (--this.retainers === 0)
40 protected finalize(): void {
41 for (let prototype = this; prototype !== null; prototype = Object.getPrototypeOf(prototype))
42 if (prototype.hasOwnProperty("@resource"))
43 for (const property of (prototype as any)["@resource"] as string[])
44 (this as any)[property] = null;
45 for (const finalizes of this.finalizes)
50 export function resource(target: any, property: string): void {
51 let resources = target["@resource"];
52 if (resources === undefined)
53 target["@resource"] = resources = [];
54 resources.push(property);
56 const mangled = "@resource " + property;
58 Object.defineProperty(target, mangled, {
64 Object.defineProperty(target, property, {
68 return (this as any)[mangled] as Resource | null;
71 set: function(value) {
72 const old = (this as any)[mangled] as Resource | null;
77 (this as any)[mangled] = value;
84 export function using<Type extends Resource, Value>(resource: Type, code: (resource: Type) => Value): Value {
88 const value = code(resource);
89 if (!(value instanceof Promise))
92 return (value as any as Promise<any>).then((value) => {
102 export class ResourceSet<Value extends Resource> extends Resource {
103 private readonly set: Set<Value>;
105 constructor() { super();
106 this.set = new Set<Value>();
109 protected finalize(): void {
114 public clear(): void {
115 for (const value of this.set.values())
117 return this.set.clear();
120 public has(value: Value): boolean {
121 return this.set.has(value);
124 public add(value: Value): this {
125 // .add() should return a boolean
126 // this is simply incompetence :/
127 if (!this.set.has(value)) {
134 public delete(value: Value): boolean {
135 const deleted = this.set.delete(value);
141 public values(): IterableIterator<Value> {
142 return this.set.values();
145 public get size(): number {
146 return this.set.size;
149 public [Symbol.iterator](): IterableIterator<Value> {
150 return this.set[Symbol.iterator]();
154 export class FutureSet<Value extends Resource> extends ResourceSet<Value> {
155 private waiters: Set<(value: Value | null) => void> | null;
157 constructor() { super();
161 protected finalize(): void {
166 public cancel(): void {
167 const waiters = this.waiters;
169 if (waiters !== null)
170 for (const waiter of waiters)
174 public get(code?: () => void): Promise<Value> {
175 return new Promise<Value>((resolve, reject) => {
177 resolve(this.values().next().value);
179 if (this.waiters === null)
180 this.waiters = new Set<(value: Value | null) => void>();
181 this.waiters.add((value: Value | null) => {
187 if (code !== undefined)
193 public add(value: Value): this {
194 const result = super.add(value);
195 const waiters = this.waiters;
197 if (waiters !== null)
198 for (const waiter of waiters)
204 export class ResourceMap<Key, Value extends Resource> extends Resource {
205 private readonly map: Map<Key, Value>;
207 constructor() { super();
208 this.map = new Map<Key, Value>();
211 protected finalize(): void {
216 public clear(): void {
217 for (const value of this.map.values())
219 return this.map.clear();
222 public has(key: Key): boolean {
223 return this.map.has(key);
226 public get(key: Key): Value | undefined {
227 return this.map.get(key);
230 public set(key: Key, value: Value): this {
231 // .set() should return old value
232 // this is simply incompetence :/
233 const old = this.map.get(key);
235 if (value !== undefined && value !== null)
237 this.map.set(key, value);
238 if (old !== undefined && old !== null)
244 public vet(key: Key, code: () => Value): Value {
245 const old = this.map.get(key);
246 if (old !== undefined)
248 const value = code();
251 this.map.set(key, value);
255 public delete(key: Key): boolean {
256 // .delete() should return old value
257 // since undefined is also a *value*
258 // you can't use .get() to .delete()
259 // this is all stupid incompetent :/
260 const old = this.map.get(key);
261 const deleted = this.map.delete(key);
262 if (old !== undefined)
267 public values(): IterableIterator<Value> {
268 return this.map.values();
271 public get size(): number {
272 return this.map.size;
276 export class ResourceArray<Value extends Resource | null> extends Resource {
277 private readonly array: Value[];
279 constructor(size: number = 0) { super();
280 this.array = new Array(size).fill(null);
283 protected finalize(): void {
284 for (const value of this.array)
287 this.array.length = 0;
290 public fill(value: Value): this {
291 const array = this.array;
292 for (let index = 0; index !== array.length; ++index) {
293 const old = array[index];
299 array[index] = value;
307 public map<T>(code: (value: Value) => T): T[] {
308 return this.array.map(code);
311 public get length(): number {
312 return this.array.length;
315 public get(index: number): Value {
316 if (index < 0 || (index | 0) !== index) throw new Error();
317 if (index >= this.array.length) throw new Error();
318 return this.array[index];
321 public set(index: number, value: Value): void {
322 if (index < 0 || (index | 0) !== index) throw new Error();
323 if (index >= this.array.length) throw new Error();
324 const old = this.array[index];
327 this.array[index] = value;
332 public [Symbol.iterator](): IterableIterator<Value> {
333 return this.array[Symbol.iterator]();
337 export class Scoped<T> extends Resource {
338 public readonly value: T;
339 private readonly remove: () => void;
341 constructor(value: T, remove: () => void) { super();
343 this.remove = remove;
346 protected finalize(): void {