UNPKG

4.36 kBTypeScriptView Raw
1import { Disposable, DisposableCollection } from './disposable';
2import { Emitter, Event } from './event';
3import { MaybePromise } from './types';
4export interface Reference<T> extends Disposable {
5 readonly object: T;
6}
7/**
8 * Abstract class for a map of reference-counted disposable objects, with the
9 * following features:
10 *
11 * - values are not inserted explicitly; instead, acquire() is used to
12 * create the value for a given key, or return the previously created
13 * value for it. How the value is created for a given key is
14 * implementation specific.
15 *
16 * - any subsquent acquire() with the same key will bump the reference
17 * count on that value. acquire() returns not the value directly but
18 * a reference object that holds the value. Calling dispose() on the
19 * reference decreases the value's effective reference count.
20 *
21 * - a contained value will have its dispose() function called when its
22 * reference count reaches zero. The key/value pair will be purged
23 * from the collection.
24 *
25 * - calling dispose() on the value directly, instead of calling it on
26 * the reference returned by acquire(), will automatically dispose
27 * all outstanding references to that value and the key/value pair
28 * will be purged from the collection.
29 *
30 * - supports synchronous and asynchronous implementations. acquire() will
31 * return a Promise if the value cannot be created immediately
32 *
33 * - functions has|keys|values|get are always synchronous and the result
34 * excludes asynchronous additions in flight.
35 *
36 * - functions values|get return the value directly and not a reference
37 * to the value. Use these functions to obtain a value without bumping
38 * its reference count.
39 *
40 * - clients can register to be notified when values are added and removed;
41 * notification for asynchronous additions happen when the creation
42 * completes, not when it's requested.
43 *
44 * - keys can be any value/object that can be successfully stringified using
45 * JSON.stringify(), sans arguments
46 *
47 * - calling dispose() on the collection will dispose all outstanding
48 * references to all contained values, which results in the disposal of
49 * the values themselves.
50 */
51export declare abstract class AbstractReferenceCollection<K, V extends Disposable> implements Disposable {
52 protected readonly _keys: Map<string, K>;
53 protected readonly _values: Map<string, V>;
54 protected readonly references: Map<string, DisposableCollection>;
55 protected readonly onDidCreateEmitter: Emitter<V>;
56 readonly onDidCreate: Event<V>;
57 protected readonly onWillDisposeEmitter: Emitter<V>;
58 readonly onWillDispose: Event<V>;
59 protected readonly toDispose: DisposableCollection;
60 constructor();
61 dispose(): void;
62 clear(): void;
63 has(args: K): boolean;
64 keys(): K[];
65 values(): V[];
66 get(args: K): V | undefined;
67 abstract acquire(args: K): MaybePromise<Reference<V>>;
68 protected doAcquire(key: string, object: V): Reference<V>;
69 protected toKey(args: K): string;
70 protected createReferences(key: string, value: V): DisposableCollection;
71}
72/**
73 * Asynchronous implementation of AbstractReferenceCollection that requires
74 * the client to provide a value factory, used to service the acquire()
75 * function. That factory may return a Promise if the value cannot be
76 * created immediately.
77 */
78export declare class ReferenceCollection<K, V extends Disposable> extends AbstractReferenceCollection<K, V> {
79 protected readonly factory: (key: K) => MaybePromise<V>;
80 constructor(factory: (key: K) => MaybePromise<V>);
81 acquire(args: K): Promise<Reference<V>>;
82 protected readonly pendingValues: Map<string, MaybePromise<V>>;
83 protected getOrCreateValue(key: string, args: K): Promise<V>;
84}
85/**
86 * Synchronous implementation of AbstractReferenceCollection that requires
87 * the client to provide a value factory, used to service the acquire()
88 * function.
89 */
90export declare class SyncReferenceCollection<K, V extends Disposable> extends AbstractReferenceCollection<K, V> {
91 protected readonly factory: (key: K) => V;
92 constructor(factory: (key: K) => V);
93 acquire(args: K): Reference<V>;
94 protected getOrCreateValue(key: string, args: K): V;
95}
96//# sourceMappingURL=reference.d.ts.map
\No newline at end of file