This repository has been archived by the owner on Sep 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.aspect.ts
308 lines (249 loc) · 7.51 KB
/
role.aspect.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import {
AfterEvent,
AfterEvent__symbol,
EventKeeper,
mapAfter_,
trackValue,
} from '@proc7ts/fun-events';
import { noop } from '@proc7ts/primitives';
import { Supply, SupplyPeer } from '@proc7ts/supply';
import { InAspect, InAspect__symbol } from '../aspect';
import { InControl } from '../control';
const InRole__aspect: InAspect<InRole<any>, 'role'> = {
applyTo<TValue>(control: InControl<TValue>): InAspect.Applied<TValue, InRole<TValue>> {
return {
instance: new InControlRole<TValue>(control),
convertTo: noop,
};
},
};
/**
* A role of input control.
*
* Contains arbitrary named roles. A special role `'default'` considered active when no other roles activated.
*
* Roles may be used to conditionally activate other input aspects of the control.
*
* @typeParam TValue - Input value type.
*/
export abstract class InRole<TValue> implements EventKeeper<[InRole.Active]> {
static get [InAspect__symbol](): InAspect<InRole<any>, 'role'> {
return InRole__aspect;
}
/**
* An `AfterEvent` keeper of active roles.
*
* The `[AfterEvent__symbol]` method returns this value.
*/
abstract readonly read: AfterEvent<[InRole.Active]>;
[AfterEvent__symbol](): AfterEvent<[InRole.Active]> {
return this.read;
}
/**
* Adds named role to control.
*
* The named role becomes activate. To deactivate it the returned supply has to be cut off.
*
* The same role may be added multiple times. In that case the role will be deactivated once all role supplies cut
* off.
*
* @param role - A name of the role to add.
*
* @returns A role supply. Removes the added role once cut off.
*/
abstract add(role: string): Supply;
/**
* Registers an activator of the given role.
*
* The given activator would be issued once the given role {@link add activated}. A supply instance returned
* by activator call will be cut off once the role deactivated.
*
* @param role - Target role name.
* @param activator - Role activator.
*
* @returns Activator supply. Removes the registered activator once cut off.
*/
abstract when(role: string, activator: InRole.Activator<TValue>): Supply;
}
export namespace InRole {
/**
* An activator signature of input control role.
*
* @typeParam TValue - Input value type.
* @param control - A control the role is activated for.
* @param role - Activated role name.
* @param active - Active control role.
*
* @returns Activation supply peer. Its supply will be cut off once the role deactivated or activator removed. It is
* expected that this supply performs deactivation once cut off.
*/
export type Activator<TValue> = (
this: void,
control: InControl<TValue>,
role: string,
active: Active,
) => SupplyPeer;
/**
* Active input control role.
*
* Contains all roles {@link InRole.add added} to control.
*
* Implements an `Iterable` interface by iterating over all active role names.
*/
export interface Active extends Iterable<string> {
/**
* Checks whether the given role is active.
*
* @param role - Target role name.
*
* @returns `true` if the given role is {@link InRole.add added} to controller, or `false` otherwise.
*/
has(role: string): boolean;
}
}
class InRole$Active {
static create(): InRole$Active {
const result = new InRole$Active(new Map());
result.add('default', true);
return result;
}
readonly active: InRole.Active;
private _activate: (this: void) => void = noop;
private _defaultSupply!: Supply;
private constructor(readonly roles: Map<string, InRole$Named>) {
this.active = {
[Symbol.iterator]() {
return roles.keys();
},
has(role: string) {
return roles.has(role);
},
};
}
modify(): InRole$Active {
const result = new InRole$Active(this.roles);
result._defaultSupply = this._defaultSupply;
return result;
}
add(role: string, isDefault: boolean): Supply | undefined {
const named = this.roles.get(role);
if (named) {
// Already active.
++named.active;
return;
}
const supply = new Supply();
this.roles.set(role, {
active: 1,
supply,
});
if (isDefault) {
this._defaultSupply = supply.whenOff(() => {
const toRemove = this.roles.get(role)!;
if (!--toRemove.active) {
this.roles.delete(role);
}
});
} else {
const defaultSupply = this._defaultSupply;
this.activateBy(() => defaultSupply.off());
}
return supply;
}
remove(role: string, reason: unknown): void {
const named = this.roles.get(role)!;
if (--named.active) {
// Still active.
return;
}
this.roles.delete(role);
this.activateBy(() => named.supply.off(reason));
}
activateBy(activator: () => void): void {
const prevActivator = this._activate;
this._activate = () => {
prevActivator();
activator();
};
}
activate(): void {
const activator = this._activate;
this._activate = noop;
activator();
}
}
interface InRole$Named {
active: number;
readonly supply: Supply;
}
class InControlRole<TValue> extends InRole<TValue> {
private readonly _active = trackValue<InRole$Active>(InRole$Active.create());
private readonly _activators = new Map<string, Map<Supply, InRole.Activator<TValue>>>();
readonly read: AfterEvent<[InRole.Active]>;
constructor(private readonly _control: InControl<TValue>) {
super();
this._active.on(active => active.activate());
this._active.supply.needs(_control);
this.read = this._active.read.do(mapAfter_(({ active }) => active));
}
add(role: string): Supply {
const active = this._active.it.modify();
this._add(active, role);
this._active.it = active;
return new Supply(reason => {
const active = this._active.it.modify();
active.remove(role, reason);
if (!active.roles.size) {
// No more active roles left.
// Enable `default` role.
this._add(active, 'default', true);
}
this._active.it = active;
});
}
when(role: string, activator: InRole.Activator<TValue>): Supply {
let activators = this._activators.get(role);
if (!activators) {
activators = new Map();
this._activators.set(role, activators);
}
const supply = new Supply().needs(this._control);
activators.set(supply, (control, role, active) => activator(control, role, active).supply.needs(supply));
supply.whenOff(() => {
activators!.delete(supply);
if (!activators!.size) {
this._activators.delete(role);
}
});
const named = this._active.it.roles.get(role);
if (named) {
// The role already active.
// Issue activator immediately.
named.supply.cuts(activator(this._control, role, this._active.it.active));
}
return supply;
}
private _add(active: InRole$Active, role: string, isDefault = false): void {
const activatedSupply = active.add(role, isDefault);
if (activatedSupply) {
// Role activated
const activators = this._activators.get(role);
if (activators) {
// Issue activators
for (const activator of activators.values()) {
activatedSupply.cuts(activator(this._control, role, this._active.it.active));
}
}
}
}
}
declare module '../aspect' {
export namespace InAspect.Application {
export interface Map<TInstance, TValue> {
/**
* Input role application type.
*/
role(): InRole<TValue>;
}
}
}