-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathevent.ts
81 lines (74 loc) · 2.23 KB
/
event.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
import {
type OverlayAsyncControllerComponent,
type OverlayControllerComponent,
} from './context/provider/content-overlay-controller';
import { type OverlayItemContext, type OverlayStore } from './context/store';
import { randomId } from './utils/random-id';
type OpenOverlayOptions<C extends OverlayItemContext> = {
overlayId?: string;
context?: C;
};
export function createOverlay(overlayStore: OverlayStore) {
function open<C extends OverlayItemContext>(
controller: OverlayControllerComponent<C>,
options?: OpenOverlayOptions<C>
) {
const overlayId = options?.overlayId ?? randomId();
const context = options?.context;
overlayStore.dispatchOverlay({
type: 'ADD',
overlay: {
id: overlayId,
isOpen: false,
controller: controller as OverlayControllerComponent<OverlayItemContext>,
context: context ?? ({} as C),
},
});
return overlayId;
}
async function openAsync<T, C extends OverlayItemContext>(
controller: OverlayAsyncControllerComponent<T, C>,
options?: OpenOverlayOptions<C>
) {
return new Promise<T>((resolve) => {
open((overlayProps, ...deprecatedLegacyContext) => {
/**
* @description close the overlay with resolve
*/
const close = (param: T) => {
resolve(param as T);
overlayProps.close();
};
/**
* @description Passing overridden methods
*/
const props = { ...overlayProps, close };
return controller(props, ...deprecatedLegacyContext);
}, options);
});
}
function close(overlayId: string) {
overlayStore.dispatchOverlay({ type: 'CLOSE', overlayId });
}
function unmount(overlayId: string) {
overlayStore.dispatchOverlay({ type: 'REMOVE', overlayId });
}
function closeAll() {
overlayStore.dispatchOverlay({ type: 'CLOSE_ALL' });
}
function unmountAll() {
overlayStore.dispatchOverlay({ type: 'REMOVE_ALL' });
}
function updateContext<C extends OverlayItemContext>(overlayId: string, context: C) {
overlayStore.dispatchOverlay({ type: 'UPDATE_CONTEXT', overlayId, context });
}
return {
open,
close,
unmount,
closeAll,
unmountAll,
openAsync,
updateContext,
};
}