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
/
share-locator.ts
224 lines (204 loc) · 6.37 KB
/
share-locator.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
import { nodeHost } from '@frontmeans/dom-primitives';
import { AfterEvent } from '@proc7ts/fun-events';
import { ComponentContext } from '@wesib/wesib';
import { ShareRef } from './share-ref';
/**
* Shared value locator.
*
* Can be one of:
*
* - shared value locator specified {@link ShareLocator.Spec},
* - {@link ShareLocator.CustomWithFallback custom} shared value locator, or
* - `null`/`undefined` to locate a fallback share.
*
* A {@link shareLocator} function can be used to convert arbitrary locator to a function.
*
* @typeParam T - Shared value type.
*/
export type ShareLocator<T> =
| ShareLocator.Spec<T>
| ShareLocator.CustomWithFallback<T>
| null
| undefined;
/**
* Converts mandatory shared value locator to locator function.
*
* @typeParam T - Shared value type.
* @param locator - Shared value locator to convert.
* @param defaultOptions - Default shared value locator options.
*
* @returns Shared value locator function.
*/
export function shareLocator<T>(
locator: ShareLocator.Mandatory<T>,
defaultOptions?: ShareLocator.Options,
): ShareLocator.Fn<T>;
/**
* Converts arbitrary shared value locator to locator function.
*
* @typeParam T - Shared value type.
* @param locator - Shared value locator to convert.
* @param defaultSpec - Default shared value locator specifier including fallback share reference.
*
* @returns Shared value locator function.
*/
export function shareLocator<T>(
locator: ShareLocator<T>,
defaultSpec: ShareLocator.MandatorySpec<T>,
): ShareLocator.Fn<T>;
export function shareLocator<T>(
locator:
| Partial<ShareLocator.MandatorySpec<T>>
| ShareLocator.CustomWithFallback<T>
| null
| undefined,
defaultSpec: ShareLocator.Spec<T> = {},
): ShareLocator.Fn<T> {
if (isCustomShareLocator(locator)) {
const {
host: hostByDefault = nodeHost,
local: localByDefault = false,
share: shareByDefault,
} = defaultSpec;
return (consumer, options = {}) => {
const { share = shareByDefault!, host = hostByDefault, local = localByDefault } = options;
return locator(consumer, { share, host, local });
};
}
const {
share: shareRef = defaultSpec.share!,
host: hostByDefault = defaultSpec.host,
local: localByDefault = defaultSpec.local,
} = locator || {};
const share = shareRef.share;
return (consumer, options = {}) => {
const { host = hostByDefault, local = localByDefault } = options;
return share.valueFor(consumer, { host, local });
};
}
function isCustomShareLocator<T>(
locator:
| Partial<ShareLocator.MandatorySpec<T>>
| ShareLocator.CustomWithFallback<T>
| null
| undefined,
): locator is ShareLocator.CustomWithFallback<T> {
return typeof locator === 'function' && !('share' in locator);
}
export namespace ShareLocator {
/**
* Mandatory shared value locator.
*
* Can be one of:
*
* - component share {@link ShareRef reference},
* - shared value locator specified {@link ShareLocator.Spec}, or
* - {@link ShareLocator.Custom custom} shared value locator.
*
* A {@link shareLocator} function can be used to convert arbitrary locator to a function.
*
* @typeParam T - Shared value type.
*/
export type Mandatory<T> = ShareRef<T> | MandatorySpec<T> | Custom<T>;
/**
* Shared value location options.
*/
export interface Options {
/**
* Detects a host element of the given one.
*
* By default utilizes a `nodeHost()` function that founds parent element crossing shadow DOM bounds.
*
* A `drekHost()` can be uses to also cross a rendered fragment bounds.
*
* @param element - An element to detect a host of.
*
* @returns Either a host element, or `undefined` if no host found.
*/
readonly host?: ((this: void, element: Element) => Element | undefined) | undefined;
/**
* Whether to search locally, in consumer component itself.
*
* - `false` (by default), to start the search from consumer's parent,
* - `true` to search locally, i.e. only in consumer component, or
* - `'too'` to start the search from consumer component.
*/
readonly local?: boolean | 'too' | undefined;
}
/**
* Shared value location options with all properties present.
*/
export type FullOptions = Required<Options>;
/**
* Shared value locator specifier.
*
* @typeParam T - Share value type.
*/
export interface Spec<T> extends Options {
/**
* Target share.
*/
readonly share?: ShareRef<T> | undefined;
}
/**
* Mandatory shared value locator specifier.
*
* @typeParam T - Share value type.
*/
export interface MandatorySpec<T> extends Spec<T> {
/**
* Target share.
*/
readonly share: ShareRef<T>;
}
/**
* Shared value locator specifier with all properties set.
*
* @typeParam T - Share value type.
*/
export type FullSpec<T> = Required<MandatorySpec<T>>;
/**
* Signature of custom shared value locator.
*
* @typeParam T - Shared value type.
* @param consumer - Consumer component context.
* @param options - Full shared value location options.
*
* @returns An `AfterEvent` keeper of the shared value and its sharer context, if found.
*/
export type Custom<T> = (
this: void,
consumer: ComponentContext,
options: FullOptions,
) => AfterEvent<[] | [T, ComponentContext]>;
/**
* Signature of custom shared value locator that expects a fallback share reference to be specified.
*
* @typeParam T - Shared value type.
* @param consumer - Consumer component context.
* @param options - Full shared value location specifier, including fallback share reference.
*
* @returns An `AfterEvent` keeper of the shared value and its sharer context, if found.
*/
export type CustomWithFallback<T> = (
this: void,
consumer: ComponentContext,
spec: FullSpec<T>,
) => AfterEvent<[] | [T, ComponentContext]>;
/**
* Signature of shared value locator function.
*
* Can be constructed by {@link shareLocator} function.
*
* @typeParam T - Shared value type.
* @param consumer - Consumer component context.
* @param options - Shared value location options.
*
* @returns An `AfterEvent` keeper of the shared value and its sharer context, if found.
*/
export type Fn<T> = (
this: void,
consumer: ComponentContext,
defaultSpec?: Spec<T>,
) => AfterEvent<[] | [T, ComponentContext]>;
}