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;
29 public retain(): void {
33 public release(): void {
34 if (--this.retainers === 0)
38 public finalize(): void {
39 for (let prototype = this; prototype !== null; prototype = Object.getPrototypeOf(prototype))
40 if (prototype.hasOwnProperty("@resource"))
41 for (const property of (prototype as any)["@resource"] as string[])
42 (this as any)[property] = null;
46 export function resource(target: any, property: string): void {
47 let resources = target["@resource"];
48 if (resources === undefined)
49 target["@resource"] = resources = [];
50 resources.push(property);
52 const mangled = "@resource " + property;
54 Object.defineProperty(target, mangled, {
60 Object.defineProperty(target, property, {
64 return (this as any)[mangled] as Resource | null;
67 set: function(value) {
68 const old = (this as any)[mangled] as Resource | null;
73 (this as any)[mangled] = value;
80 export function using<Type extends Resource, Value>(resource: Type, code: (resource: Type) => Value): Value {
84 const value = code(resource);
85 if (!(value instanceof Promise))
88 return (value as any as Promise<any>).then((value) => {
98 export class ResourceSet<Value extends Resource> extends Resource {
99 private readonly set: Set<Value>;
101 constructor() { super();
102 this.set = new Set<Value>();
105 public finalize(): void {
110 public clear(): void {
111 for (const value of this.set.values())
113 return this.set.clear();
116 public has(value: Value): boolean {
117 return this.set.has(value);
120 public add(value: Value): this {
121 // .add() should return a boolean
122 // this is simply incompetence :/
123 if (!this.set.has(value)) {
130 public delete(value: Value): boolean {
131 const deleted = this.set.delete(value);
137 public values(): IterableIterator<Value> {
138 return this.set.values();
141 public get size(): number {
142 return this.set.size;
145 public [Symbol.iterator](): IterableIterator<Value> {
146 return this.set[Symbol.iterator]();
150 export class FutureSet<Value extends Resource> extends ResourceSet<Value> {
151 private waiters: Set<(value: Value | null) => void> | null;
153 constructor() { super();
157 public finalize(): void {
162 public cancel(): void {
163 const waiters = this.waiters;
165 if (waiters !== null)
166 for (const waiter of waiters)
170 public get(code?: () => void): Promise<Value> {
171 return new Promise<Value>((resolve, reject) => {
173 resolve(this.values().next().value);
175 if (this.waiters === null)
176 this.waiters = new Set<(value: Value | null) => void>();
177 this.waiters.add((value: Value | null) => {
183 if (code !== undefined)
189 public add(value: Value): this {
190 const result = super.add(value);
191 const waiters = this.waiters;
193 if (waiters !== null)
194 for (const waiter of waiters)
200 export class ResourceMap<Key, Value extends Resource> extends Resource {
201 private readonly map: Map<Key, Value>;
203 constructor() { super();
204 this.map = new Map<Key, Value>();
207 public finalize(): void {
212 public clear(): void {
213 for (const value of this.map.values())
215 return this.map.clear();
218 public has(key: Key): boolean {
219 return this.map.has(key);
222 public get(key: Key): Value | undefined {
223 return this.map.get(key);
226 public set(key: Key, value: Value): this {
227 // .set() should return old value
228 // this is simply incompetence :/
229 const old = this.map.get(key);
231 if (value !== undefined && value !== null)
233 this.map.set(key, value);
234 if (old !== undefined && old !== null)
240 public vet(key: Key, code: () => Value): Value {
241 const old = this.map.get(key);
242 if (old !== undefined)
244 const value = code();
247 this.map.set(key, value);
251 public delete(key: Key): boolean {
252 // .delete() should return old value
253 // since undefined is also a *value*
254 // you can't use .get() to .delete()
255 // this is all stupid incompetent :/
256 const old = this.map.get(key);
257 const deleted = this.map.delete(key);
258 if (old !== undefined)
263 public values(): IterableIterator<Value> {
264 return this.map.values();
267 public get size(): number {
268 return this.map.size;
272 export class ResourceArray<Value extends Resource | null> extends Resource {
273 private readonly array: Value[];
275 constructor(size: number = 0) { super();
276 this.array = new Array(size).fill(null);
279 public finalize(): void {
280 for (const value of this.array)
283 this.array.length = 0;
286 public fill(value: Value): this {
287 const array = this.array;
288 for (let index = 0; index !== array.length; ++index) {
289 const old = array[index];
295 array[index] = value;
303 public map<T>(code: (value: Value) => T): T[] {
304 return this.array.map(code);
307 public get length(): number {
308 return this.array.length;
311 public get(index: number): Value {
312 if (index < 0 || (index | 0) !== index) throw new Error();
313 if (index >= this.array.length) throw new Error();
314 return this.array[index];
317 public set(index: number, value: Value): void {
318 if (index < 0 || (index | 0) !== index) throw new Error();
319 if (index >= this.array.length) throw new Error();
320 const old = this.array[index];
323 this.array[index] = value;
328 public [Symbol.iterator](): IterableIterator<Value> {
329 return this.array[Symbol.iterator]();
333 export class Scoped<T> extends Resource {
334 public readonly value: T;
335 private readonly remove: () => void;
337 constructor(value: T, remove: () => void) { super();
339 this.remove = remove;
342 public finalize(): void {