This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.ts
109 lines (94 loc) · 3.16 KB
/
builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { CxAccessor, CxEntry, CxRequest, CxValues } from '@proc7ts/context-values';
import { lazyValue } from '@proc7ts/primitives';
import { Supply } from '@proc7ts/supply';
import { CxBuilder$BoundPeer, CxBuilder$Cache } from './impl';
import { CxPeer } from './peer';
import { CxPeerBuilder } from './peer-builder';
/**
* Context builder.
*
* Provides value assets for the context.
*
* @typeParam TContext - A type of context to build.
*/
export class CxBuilder<TContext extends CxValues = CxValues>
extends CxPeerBuilder<TContext>
implements CxValues {
/**
* @internal
*/
private readonly _cx: () => TContext;
/**
* @internal
*/
private readonly _cache = new CxBuilder$Cache();
/**
* @internal
*/
private readonly _bound: () => CxPeer = lazyValue(
() => new CxBuilder$BoundPeer(this, this.cache),
);
/**
* Constructs context builder.
*
* @param createContext - Context creator function. Accepts context value accessor and the builder itself as
* parameters, and returns created context.
* @param peers - Context peers to apply assets from. These assets applied before the ones provided {@link provide
* explicitly}. Peers listed later have lesser {@link CxAsset.Provided.rank rank values} than the ones listed earlier.
*/
constructor(
createContext: (this: void, getValue: CxAccessor, builder: CxBuilder<TContext>) => TContext,
...peers: CxPeer<TContext>[]
) {
super(...peers);
this._cx = lazyValue(() => createContext((entry, request) => this.get(entry, request), this));
}
/**
* Context to build.
*/
override get context(): TContext {
return this._cx();
}
protected override get cache(): CxBuilder.Cache {
return this._cache;
}
/**
* A peer providing assets bound to {@link context}.
*
* Unlike the builder itself, this peer may provide assets for any context, as they constructed in compatible one.
*/
get boundPeer(): CxPeer {
return this._bound();
}
get<TValue>(entry: CxEntry<TValue, any>, request?: CxRequest.WithoutFallback<TValue>): TValue;
get<TValue>(entry: CxEntry<TValue, any>, request: CxRequest.WithFallback<TValue>): TValue;
get<TValue>(entry: CxEntry<TValue, any>, request?: CxRequest<TValue>): TValue | null;
get<TValue>(entry: CxEntry<TValue, any>, request?: CxRequest<TValue>): TValue | null {
return this._record(entry).get(request);
}
}
export namespace CxBuilder {
/**
* Context cache the {@link CxPeer context peer} may use to store intermediate data.
*
* There is only one cache instance exists per context.
*/
export interface Cache {
/**
* Obtains a value previously {@link put cached} under the given `key`.
*
* @param key - Cached value key.
*
* @returns Either cached value, or `undefined` if the value did not cached.
*/
get(key: unknown): unknown | undefined;
/**
* Caches the `value` under the given `key`.
*
* @param key - Cached value key.
* @param value - A value to cache.
* @param supply - Value supply. The value will be removed from cache once this supply cut off.
*/
put(key: unknown, value: unknown, supply: Supply): void;
}
}