/
PageHandlerRegistry.ts
94 lines (82 loc) · 2.24 KB
/
PageHandlerRegistry.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
import { PageHandler } from './PageHandler';
import { GenericError } from '../../error/GenericError';
import { Execution } from '../../execution/Execution';
import { SerialBatch } from '../../execution/SerialBatch';
import { ManagedPage, PageAction } from '../PageTypes';
export class PageHandlerRegistry extends PageHandler {
protected _pageHandlers: PageHandler[];
protected _preManageHandlers?: Execution;
protected _postManageHandlers?: Execution;
static ExecutionMethod = SerialBatch;
/**
* Creates an instance of HandlerRegistry and creates `SerialBatch`
* instance for pre-handlers and post-handlers.
* @memberof HandlerRegistry
*/
constructor(...pageHandlers: PageHandler[]) {
super();
this._pageHandlers = pageHandlers;
}
/**
* @inheritDoc
*/
init() {
this._pageHandlers.forEach(handler => handler.init());
this._preManageHandlers = Reflect.construct(
PageHandlerRegistry.ExecutionMethod,
[
this._pageHandlers.map(handler =>
handler.handlePreManagedState.bind(handler)
),
]
);
this._postManageHandlers = Reflect.construct(
PageHandlerRegistry.ExecutionMethod,
[
this._pageHandlers.map(handler =>
handler.handlePostManagedState.bind(handler)
),
]
);
}
/**
* Executes the pre-manage handlers with given arguments
*/
handlePreManagedState(
managedPage: ManagedPage | null,
nextManagedPage: ManagedPage,
action: PageAction
) {
if (!this._preManageHandlers) {
throw new GenericError('You must call init first.');
}
return this._preManageHandlers.execute(
managedPage,
nextManagedPage,
action
);
}
/**
* Executes the post-manage handlers with given arguments
*/
handlePostManagedState(
managedPage: ManagedPage | null,
previousManagedPage: ManagedPage,
action: PageAction
) {
return this?._postManageHandlers?.execute(
managedPage,
previousManagedPage,
action
) as Promise<unknown>;
}
/**
* @inheritDoc
*/
destroy() {
this._pageHandlers.forEach(handler => handler.destroy());
this._preManageHandlers = undefined;
this._postManageHandlers = undefined;
this._pageHandlers = [];
}
}