@microsoft/fast-element 2.0.0-beta.1 → 2.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.json +51 -0
  2. package/CHANGELOG.md +15 -1
  3. package/dist/dts/components/fast-definitions.d.ts +2 -0
  4. package/dist/dts/components/fast-element.d.ts +7 -1
  5. package/dist/dts/context.d.ts +157 -0
  6. package/dist/dts/metadata.d.ts +25 -0
  7. package/dist/dts/observation/arrays.d.ts +1 -1
  8. package/dist/dts/observation/behavior.d.ts +4 -4
  9. package/dist/dts/observation/observable.d.ts +43 -62
  10. package/dist/dts/templating/binding-signal.d.ts +38 -0
  11. package/dist/dts/templating/binding-two-way.d.ts +56 -0
  12. package/dist/dts/templating/binding.d.ts +0 -88
  13. package/dist/dts/templating/compiler.d.ts +1 -2
  14. package/dist/dts/templating/repeat.d.ts +3 -49
  15. package/dist/dts/templating/template.d.ts +10 -59
  16. package/dist/dts/templating/view.d.ts +9 -9
  17. package/dist/{tsdoc-metadata.json → dts/tsdoc-metadata.json} +0 -0
  18. package/dist/esm/components/fast-definitions.js +2 -0
  19. package/dist/esm/components/fast-element.js +10 -2
  20. package/dist/esm/context.js +159 -0
  21. package/dist/esm/metadata.js +60 -0
  22. package/dist/esm/observation/arrays.js +1 -1
  23. package/dist/esm/observation/observable.js +69 -17
  24. package/dist/esm/templating/binding-signal.js +84 -0
  25. package/dist/esm/templating/binding-two-way.js +76 -0
  26. package/dist/esm/templating/binding.js +1 -158
  27. package/dist/esm/templating/repeat.js +9 -1
  28. package/dist/esm/templating/template.js +1 -21
  29. package/dist/fast-element.api.json +5586 -7541
  30. package/dist/fast-element.d.ts +119 -329
  31. package/dist/fast-element.debug.js +92 -199
  32. package/dist/fast-element.debug.min.js +1 -1
  33. package/dist/fast-element.js +92 -199
  34. package/dist/fast-element.min.js +1 -1
  35. package/dist/fast-element.untrimmed.d.ts +120 -331
  36. package/docs/api-report.md +50 -156
  37. package/package.json +20 -4
@@ -118,34 +118,6 @@ export declare class OneTimeBinding extends UpdateBinding {
118
118
  */
119
119
  bind(source: any, context: ExecutionContext, targets: ViewBehaviorTargets): void;
120
120
  }
121
- /**
122
- * A binding behavior for signal bindings.
123
- * @public
124
- */
125
- export declare class SignalBinding extends UpdateBinding {
126
- private handlerProperty;
127
- /**
128
- * Bind this behavior to the source.
129
- * @param source - The source to bind to.
130
- * @param context - The execution context that the binding is operating within.
131
- * @param targets - The targets that behaviors in a view can attach to.
132
- */
133
- bind(source: any, context: ExecutionContext, targets: ViewBehaviorTargets): void;
134
- /**
135
- * Unbinds this behavior from the source.
136
- * @param source - The source to unbind from.
137
- * @param context - The execution context that the binding is operating within.
138
- * @param targets - The targets that behaviors in a view can attach to.
139
- */
140
- unbind(source: any, context: ExecutionContext, targets: ViewBehaviorTargets): void;
141
- private getSignal;
142
- /**
143
- * Sends the specified signal to signaled bindings.
144
- * @param signal - The signal to send.
145
- * @public
146
- */
147
- static send(signal: string): void;
148
- }
149
121
  /**
150
122
  * A binding behavior for bindings that change.
151
123
  * @public
@@ -219,46 +191,6 @@ export declare class EventBinding {
219
191
  */
220
192
  handleEvent(event: Event): void;
221
193
  }
222
- /**
223
- * The settings required to enable two-way binding.
224
- * @public
225
- */
226
- export interface TwoWaySettings {
227
- /**
228
- * Determines which event to listen to, to detect changes in the view.
229
- * @param directive - The directive to determine the change event for.
230
- * @param target - The target element to determine the change event for.
231
- */
232
- determineChangeEvent(directive: HTMLBindingDirective, target: HTMLElement): string;
233
- }
234
- /**
235
- * A binding behavior for bindings that update in two directions.
236
- * @public
237
- */
238
- export declare class TwoWayBinding extends ChangeBinding {
239
- private changeEvent;
240
- /**
241
- * Bind this behavior to the source.
242
- * @param source - The source to bind to.
243
- * @param context - The execution context that the binding is operating within.
244
- * @param targets - The targets that behaviors in a view can attach to.
245
- */
246
- bind(source: any, context: ExecutionContext, targets: ViewBehaviorTargets): void;
247
- /**
248
- * Unbinds this behavior from the source.
249
- * @param source - The source to unbind from.
250
- * @param context - The execution context that the binding is operating within.
251
- * @param targets - The targets that behaviors in a view can attach to.
252
- */
253
- unbind(source: any, context: ExecutionContext, targets: ViewBehaviorTargets): void;
254
- /** @internal */
255
- handleEvent(event: Event): void;
256
- /**
257
- * Configures two-way binding.
258
- * @param settings - The settings to use for the two-way binding system.
259
- */
260
- static configure(settings: TwoWaySettings): void;
261
- }
262
194
  /**
263
195
  * The default binding options.
264
196
  * @public
@@ -269,31 +201,11 @@ export declare type DefaultBindingOptions = AddEventListenerOptions;
269
201
  * @public
270
202
  */
271
203
  export declare const onChange: BindingConfig<AddEventListenerOptions> & BindingConfigResolver<AddEventListenerOptions>;
272
- /**
273
- * The default twoWay binding options.
274
- * @public
275
- */
276
- export declare type DefaultTwoWayBindingOptions = DefaultBindingOptions & {
277
- changeEvent?: string;
278
- fromView?: (value: any) => any;
279
- };
280
- /**
281
- * The default twoWay binding configuration.
282
- * @public
283
- */
284
- export declare const twoWay: BindingConfig<DefaultTwoWayBindingOptions> & BindingConfigResolver<DefaultTwoWayBindingOptions>;
285
204
  /**
286
205
  * The default onTime binding configuration.
287
206
  * @public
288
207
  */
289
208
  export declare const oneTime: BindingConfig<AddEventListenerOptions> & BindingConfigResolver<AddEventListenerOptions>;
290
- /**
291
- * Creates a signal binding configuration with the supplied options.
292
- * @param options - The signal name or a binding to use to retrieve the signal name.
293
- * @returns A binding configuration.
294
- * @public
295
- */
296
- export declare const signal: <T = any>(options: string | Binding<T, any, ExecutionContext<any>>) => BindingConfig<string | Binding<T, any, ExecutionContext<any>>>;
297
209
  /**
298
210
  * A directive that applies bindings.
299
211
  * @public
@@ -1,5 +1,4 @@
1
1
  import { TrustedTypesPolicy } from "../interfaces.js";
2
- import type { ExecutionContext } from "../observation/observable.js";
3
2
  import { ViewBehaviorFactory } from "./html-directive.js";
4
3
  import type { HTMLTemplateCompilationResult as TemplateCompilationResult } from "./template.js";
5
4
  /**
@@ -40,7 +39,7 @@ export declare const Compiler: {
40
39
  * it is recommended that you clone the original and pass the clone to this API.
41
40
  * @public
42
41
  */
43
- compile<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>>(html: string | HTMLTemplateElement, directives: Record<string, ViewBehaviorFactory>): TemplateCompilationResult<TSource, TParent, TContext>;
42
+ compile<TSource = any, TParent = any>(html: string | HTMLTemplateElement, directives: Record<string, ViewBehaviorFactory>): TemplateCompilationResult<TSource, TParent>;
44
43
  /**
45
44
  * Sets the default compilation strategy that will be used by the ViewTemplate whenever
46
45
  * it needs to compile a view preprocessed with the html template function.
@@ -1,9 +1,9 @@
1
1
  import type { Behavior } from "../observation/behavior.js";
2
2
  import type { Subscriber } from "../observation/notifier.js";
3
- import { Binding, ChildContext, ExecutionContext, ItemContext, RootContext } from "../observation/observable.js";
3
+ import { Binding, ExecutionContext } from "../observation/observable.js";
4
4
  import { Splice } from "../observation/arrays.js";
5
5
  import { AddViewBehaviorFactory, HTMLDirective, ViewBehaviorFactory, ViewBehaviorTargets } from "./html-directive.js";
6
- import type { CaptureType, ChildViewTemplate, ItemViewTemplate, SyntheticViewTemplate, ViewTemplate } from "./template.js";
6
+ import type { CaptureType, SyntheticViewTemplate, ViewTemplate } from "./template.js";
7
7
  /**
8
8
  * Options for configuring repeat behavior.
9
9
  * @public
@@ -113,50 +113,4 @@ export declare class RepeatDirective<TSource = any> implements HTMLDirective, Vi
113
113
  * @param options - Options used to turn on special repeat features.
114
114
  * @public
115
115
  */
116
- export declare function repeat<TSource = any, TArray extends ReadonlyArray<any> = ReadonlyArray<any>>(itemsBinding: Binding<TSource, TArray, ExecutionContext<TSource>>, templateOrTemplateBinding: ViewTemplate | Binding<TSource, ViewTemplate, RootContext>, options?: {
117
- positioning: false;
118
- } | {
119
- recycle: true;
120
- } | {
121
- positioning: false;
122
- recycle: false;
123
- } | {
124
- positioning: false;
125
- recycle: true;
126
- }): CaptureType<TSource>;
127
- /**
128
- * A directive that enables list rendering.
129
- * @param itemsBinding - The array to render.
130
- * @param templateOrTemplateBinding - The template or a template binding used obtain a template
131
- * to render for each item in the array.
132
- * @param options - Options used to turn on special repeat features.
133
- * @public
134
- */
135
- export declare function repeat<TSource = any, TArray extends ReadonlyArray<any> = ReadonlyArray<any>>(itemsBinding: Binding<TSource, TArray, ExecutionContext<TSource>>, templateOrTemplateBinding: ChildViewTemplate | Binding<TSource, ChildViewTemplate, ChildContext>, options?: {
136
- positioning: false;
137
- } | {
138
- recycle: true;
139
- } | {
140
- positioning: false;
141
- recycle: false;
142
- } | {
143
- positioning: false;
144
- recycle: true;
145
- }): CaptureType<TSource>;
146
- /**
147
- * A directive that enables list rendering.
148
- * @param itemsBinding - The array to render.
149
- * @param templateOrTemplateBinding - The template or a template binding used obtain a template
150
- * to render for each item in the array.
151
- * @param options - Options used to turn on special repeat features.
152
- * @public
153
- */
154
- export declare function repeat<TSource = any, TArray extends ReadonlyArray<any> = ReadonlyArray<any>>(itemsBinding: Binding<TSource, TArray, ExecutionContext<TSource>>, templateOrTemplateBinding: ItemViewTemplate | Binding<TSource, ItemViewTemplate, ItemContext>, options: {
155
- positioning: true;
156
- } | {
157
- positioning: true;
158
- recycle: true;
159
- } | {
160
- positioning: true;
161
- recycle: false;
162
- }): CaptureType<TSource>;
116
+ export declare function repeat<TSource = any, TArray extends ReadonlyArray<any> = ReadonlyArray<any>>(itemsBinding: Binding<TSource, TArray, ExecutionContext<TSource>>, templateOrTemplateBinding: ViewTemplate | Binding<TSource, ViewTemplate>, options?: RepeatOptions): CaptureType<TSource>;
@@ -1,4 +1,4 @@
1
- import { Binding, ChildContext, ExecutionContext, ItemContext } from "../observation/observable.js";
1
+ import { Binding, ExecutionContext } from "../observation/observable.js";
2
2
  import { HTMLDirective, ViewBehaviorFactory } from "./html-directive.js";
3
3
  import type { ElementView, HTMLView, SyntheticView } from "./view.js";
4
4
  /**
@@ -6,7 +6,6 @@ import type { ElementView, HTMLView, SyntheticView } from "./view.js";
6
6
  * @public
7
7
  */
8
8
  export interface ElementViewTemplate<TSource = any, TParent = any> {
9
- type: "element";
10
9
  /**
11
10
  * Creates an ElementView instance based on this template definition.
12
11
  * @param hostBindingTarget - The element that host behaviors will be bound to.
@@ -25,57 +24,29 @@ export interface ElementViewTemplate<TSource = any, TParent = any> {
25
24
  * A template capable of rendering views not specifically connected to custom elements.
26
25
  * @public
27
26
  */
28
- export interface SyntheticViewTemplate<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>> {
29
- type: "synthetic";
27
+ export interface SyntheticViewTemplate<TSource = any, TParent = any> {
30
28
  /**
31
29
  * Creates a SyntheticView instance based on this template definition.
32
30
  */
33
- create(): SyntheticView<TSource, TParent, TContext>;
34
- }
35
- /**
36
- * A template capable of rendering child views not specifically connected to custom elements.
37
- * @public
38
- */
39
- export interface ChildViewTemplate<TSource = any, TParent = any> {
40
- type: "child";
41
- /**
42
- * Creates a SyntheticView instance based on this template definition.
43
- */
44
- create(): SyntheticView<TSource, TParent, ChildContext<TParent>>;
45
- }
46
- /**
47
- * A template capable of rendering item views not specifically connected to custom elements.
48
- * @public
49
- */
50
- export interface ItemViewTemplate<TSource = any, TParent = any> {
51
- type: "item";
52
- /**
53
- * Creates a SyntheticView instance based on this template definition.
54
- */
55
- create(): SyntheticView<TSource, TParent, ItemContext<TParent>>;
31
+ create(): SyntheticView<TSource, TParent>;
56
32
  }
57
33
  /**
58
34
  * The result of a template compilation operation.
59
35
  * @public
60
36
  */
61
- export interface HTMLTemplateCompilationResult<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>> {
37
+ export interface HTMLTemplateCompilationResult<TSource = any, TParent = any> {
62
38
  /**
63
39
  * Creates a view instance.
64
40
  * @param hostBindingTarget - The host binding target for the view.
65
41
  */
66
- createView(hostBindingTarget?: Element): HTMLView<TSource, TParent, TContext>;
42
+ createView(hostBindingTarget?: Element): HTMLView<TSource, TParent>;
67
43
  }
68
44
  /**
69
45
  * A template capable of creating HTMLView instances or rendering directly to DOM.
70
46
  * @public
71
47
  */
72
- export declare class ViewTemplate<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext> implements ElementViewTemplate<TSource, TParent>, SyntheticViewTemplate<TSource, TParent, TContext> {
48
+ export declare class ViewTemplate<TSource = any, TParent = any> implements ElementViewTemplate<TSource, TParent>, SyntheticViewTemplate<TSource, TParent> {
73
49
  private result;
74
- /**
75
- * Used for TypeScript purposes only.
76
- * Do not use.
77
- */
78
- type: any;
79
50
  /**
80
51
  * The html representing what this template will
81
52
  * instantiate, including placeholders for directives.
@@ -95,7 +66,7 @@ export declare class ViewTemplate<TSource = any, TParent = any, TContext extends
95
66
  * Creates an HTMLView instance based on this template definition.
96
67
  * @param hostBindingTarget - The element that host behaviors will be bound to.
97
68
  */
98
- create(hostBindingTarget?: Element): HTMLView<TSource, TParent, TContext>;
69
+ create(hostBindingTarget?: Element): HTMLView<TSource, TParent>;
99
70
  /**
100
71
  * Creates an HTMLView from this template, binds it to the source, and then appends it to the host.
101
72
  * @param source - The data source to bind the template to.
@@ -103,7 +74,7 @@ export declare class ViewTemplate<TSource = any, TParent = any, TContext extends
103
74
  * @param hostBindingTarget - An HTML element to target the host bindings at if different from the
104
75
  * host that the template is being attached to.
105
76
  */
106
- render(source: TSource, host: Node, hostBindingTarget?: Element, context?: TContext): HTMLView<TSource, TParent, TContext>;
77
+ render(source: TSource, host: Node, hostBindingTarget?: Element, context?: ExecutionContext): HTMLView<TSource, TParent>;
107
78
  }
108
79
  /**
109
80
  * A marker interface used to capture types when interpolating Directive helpers
@@ -116,7 +87,7 @@ export interface CaptureType<TSource> {
116
87
  * Represents the types of values that can be interpolated into a template.
117
88
  * @public
118
89
  */
119
- export declare type TemplateValue<TSource, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>> = Binding<TSource, any, TContext> | HTMLDirective | CaptureType<TSource>;
90
+ export declare type TemplateValue<TSource, TParent = any> = Binding<TSource, any, TParent> | HTMLDirective | CaptureType<TSource>;
120
91
  /**
121
92
  * Transforms a template literal string into a ViewTemplate.
122
93
  * @param strings - The string fragments that are interpolated with the values.
@@ -126,24 +97,4 @@ export declare type TemplateValue<TSource, TParent = any, TContext extends Execu
126
97
  * other template instances, and Directive instances.
127
98
  * @public
128
99
  */
129
- export declare function html<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>>(strings: TemplateStringsArray, ...values: TemplateValue<TSource, TParent, TContext>[]): ViewTemplate<TSource, TParent>;
130
- /**
131
- * Transforms a template literal string into a ChildViewTemplate.
132
- * @param strings - The string fragments that are interpolated with the values.
133
- * @param values - The values that are interpolated with the string fragments.
134
- * @remarks
135
- * The html helper supports interpolation of strings, numbers, binding expressions,
136
- * other template instances, and Directive instances.
137
- * @public
138
- */
139
- export declare const child: <TChild = any, TParent = any>(strings: TemplateStringsArray, ...values: TemplateValue<TChild, TParent, ChildContext<TParent>>[]) => ChildViewTemplate<TChild, TParent>;
140
- /**
141
- * Transforms a template literal string into an ItemViewTemplate.
142
- * @param strings - The string fragments that are interpolated with the values.
143
- * @param values - The values that are interpolated with the string fragments.
144
- * @remarks
145
- * The html helper supports interpolation of strings, numbers, binding expressions,
146
- * other template instances, and Directive instances.
147
- * @public
148
- */
149
- export declare const item: <TItem = any, TParent = any>(strings: TemplateStringsArray, ...values: TemplateValue<TItem, TParent, ItemContext<TParent>>[]) => ItemViewTemplate<TItem, TParent>;
100
+ export declare function html<TSource = any, TParent = any>(strings: TemplateStringsArray, ...values: TemplateValue<TSource, TParent>[]): ViewTemplate<TSource, TParent>;
@@ -1,15 +1,15 @@
1
1
  import type { Disposable } from "../interfaces.js";
2
- import type { ExecutionContext, RootContext } from "../observation/observable.js";
2
+ import type { ExecutionContext } from "../observation/observable.js";
3
3
  import type { ViewBehaviorFactory, ViewBehaviorTargets } from "./html-directive.js";
4
4
  /**
5
5
  * Represents a collection of DOM nodes which can be bound to a data source.
6
6
  * @public
7
7
  */
8
- export interface View<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>> extends Disposable {
8
+ export interface View<TSource = any, TParent = any> extends Disposable {
9
9
  /**
10
10
  * The execution context the view is running within.
11
11
  */
12
- readonly context: TContext | null;
12
+ readonly context: ExecutionContext<TParent> | null;
13
13
  /**
14
14
  * The data that the view is bound to.
15
15
  */
@@ -19,7 +19,7 @@ export interface View<TSource = any, TParent = any, TContext extends ExecutionCo
19
19
  * @param source - The binding source for the view's binding behaviors.
20
20
  * @param context - The execution context to run the view within.
21
21
  */
22
- bind(source: TSource, context: TContext): void;
22
+ bind(source: TSource, context: ExecutionContext<TParent>): void;
23
23
  /**
24
24
  * Unbinds a view's behaviors from its binding source and context.
25
25
  */
@@ -29,7 +29,7 @@ export interface View<TSource = any, TParent = any, TContext extends ExecutionCo
29
29
  * A View representing DOM nodes specifically for rendering the view of a custom element.
30
30
  * @public
31
31
  */
32
- export interface ElementView<TSource = any, TParent = any> extends View<TSource, TParent, RootContext> {
32
+ export interface ElementView<TSource = any, TParent = any> extends View<TSource, TParent> {
33
33
  /**
34
34
  * Appends the view's DOM nodes to the referenced node.
35
35
  * @param node - The parent node to append the view's DOM nodes to.
@@ -40,7 +40,7 @@ export interface ElementView<TSource = any, TParent = any> extends View<TSource,
40
40
  * A view representing a range of DOM nodes which can be added/removed ad hoc.
41
41
  * @public
42
42
  */
43
- export interface SyntheticView<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>> extends View<TSource, TParent, TContext> {
43
+ export interface SyntheticView<TSource = any, TParent = any> extends View<TSource, TParent> {
44
44
  /**
45
45
  * The first DOM node in the range of nodes that make up the view.
46
46
  */
@@ -64,7 +64,7 @@ export interface SyntheticView<TSource = any, TParent = any, TContext extends Ex
64
64
  * The standard View implementation, which also implements ElementView and SyntheticView.
65
65
  * @public
66
66
  */
67
- export declare class HTMLView<TSource = any, TParent = any, TContext extends ExecutionContext<TParent> = ExecutionContext<TParent>> implements ElementView<TSource, TParent>, SyntheticView<TSource, TParent, TContext> {
67
+ export declare class HTMLView<TSource = any, TParent = any> implements ElementView<TSource, TParent>, SyntheticView<TSource, TParent> {
68
68
  private fragment;
69
69
  private factories;
70
70
  private targets;
@@ -76,7 +76,7 @@ export declare class HTMLView<TSource = any, TParent = any, TContext extends Exe
76
76
  /**
77
77
  * The execution context the view is running within.
78
78
  */
79
- context: TContext | null;
79
+ context: ExecutionContext<TParent> | null;
80
80
  /**
81
81
  * The first DOM node in the range of nodes that make up the view.
82
82
  */
@@ -116,7 +116,7 @@ export declare class HTMLView<TSource = any, TParent = any, TContext extends Exe
116
116
  * @param source - The binding source for the view's binding behaviors.
117
117
  * @param context - The execution context to run the behaviors within.
118
118
  */
119
- bind(source: TSource, context: TContext): void;
119
+ bind(source: TSource, context: ExecutionContext<TParent>): void;
120
120
  /**
121
121
  * Unbinds a view's behaviors from its binding source.
122
122
  */
@@ -66,6 +66,8 @@ export class FASTElementDefinition {
66
66
  /**
67
67
  * Defines a custom element based on this definition.
68
68
  * @param registry - The element registry to define the element in.
69
+ * @remarks
70
+ * This operation is idempotent per registry.
69
71
  */
70
72
  define(registry = customElements) {
71
73
  const type = this.type;
@@ -43,7 +43,15 @@ export const FASTElement = Object.assign(createFASTElement(HTMLElement), {
43
43
  * that describes the element to define.
44
44
  */
45
45
  define(type, nameOrDef) {
46
- return new FASTElementDefinition(type, nameOrDef).define().type;
46
+ return this.metadata(type, nameOrDef).define().type;
47
+ },
48
+ /**
49
+ * Defines metadata for a FASTElement which can be used to later define the element.
50
+ * IMPORTANT: This API will be renamed to "compose" in a future beta.
51
+ * @public
52
+ */
53
+ metadata(type, nameOrDef) {
54
+ return new FASTElementDefinition(type, nameOrDef);
47
55
  },
48
56
  });
49
57
  /**
@@ -55,6 +63,6 @@ export const FASTElement = Object.assign(createFASTElement(HTMLElement), {
55
63
  export function customElement(nameOrDef) {
56
64
  /* eslint-disable-next-line @typescript-eslint/explicit-function-return-type */
57
65
  return function (type) {
58
- new FASTElementDefinition(type, nameOrDef).define();
66
+ FASTElement.define(type, nameOrDef);
59
67
  };
60
68
  }
@@ -0,0 +1,159 @@
1
+ import { Metadata } from "./metadata.js";
2
+ const contextEventType = "context-request";
3
+ let requestStrategy;
4
+ /**
5
+ * Enables using the {@link https://github.com/webcomponents-cg/community-protocols/blob/main/proposals/context.md | W3C Community Context protocol.}
6
+ * @beta
7
+ */
8
+ export const Context = Object.freeze({
9
+ /**
10
+ * The event type used for W3C Context Protocol requests.
11
+ */
12
+ eventType: contextEventType,
13
+ /**
14
+ * Creates a W3C Community Protocol-based Context object to use in requesting/providing
15
+ * context through the DOM.
16
+ * @param name - The name to use for the connext. Useful in debugging.
17
+ * @param initialValue - An optional initial value to use if a context handler isn't found.
18
+ */
19
+ create(name, initialValue) {
20
+ const Interface = function (target, property, index) {
21
+ if (target == null || new.target !== undefined) {
22
+ throw new Error(`No registration for context: '${Interface.name}'`);
23
+ }
24
+ if (property) {
25
+ Context.defineProperty(target, property, Interface);
26
+ }
27
+ else {
28
+ const types = Metadata.getOrCreateAnnotationParamTypes(target);
29
+ types[index] = Interface;
30
+ }
31
+ };
32
+ Interface.$isInterface = true;
33
+ Interface.initialValue = initialValue;
34
+ Reflect.defineProperty(Interface, "name", { value: name });
35
+ Interface.handle = (target, callback) => Context.handle(target, callback, Interface);
36
+ Interface.provide = (target, value) => Context.provide(target, Interface, value);
37
+ Interface.get = (target) => Context.get(target, Interface);
38
+ Interface.request = (target, callback, multiple) => Context.request(target, Interface, callback, multiple);
39
+ Interface.toString = () => `Context<${Interface.name}>`;
40
+ return Interface;
41
+ },
42
+ /**
43
+ * Sets the strategy used by all FAST-specific context requests made through the
44
+ * Context.request, Context.get, Context.defineProperty, and ContextDecorator APIs.
45
+ * @param strategy - The strategy to use. By default, the strategy is Context.dispatch.
46
+ */
47
+ setDefaultRequestStrategy(strategy) {
48
+ requestStrategy = strategy;
49
+ },
50
+ /**
51
+ * Gets the context value for the target node or returns the initial value if
52
+ * a context handler is not found.
53
+ * @param target - The target to get the context for.
54
+ * @param context - The context to locate.
55
+ * @returns The context value.
56
+ * @remarks
57
+ * Uses the default request strategy to locate the context. If no context is found
58
+ * then the initial value of the context is returned.
59
+ */
60
+ get(target, context) {
61
+ var _a;
62
+ let value;
63
+ requestStrategy(target, context, found => (value = found), false);
64
+ return (_a = value) !== null && _a !== void 0 ? _a : context.initialValue;
65
+ },
66
+ /**
67
+ * Requests the context value for the target node.
68
+ * @param target - The target to request the context for.
69
+ * @param context - The context to locate.
70
+ * @param callback - A callback to invoke with the context value.
71
+ * @param multiple - Whether the context requestor expects to handle updates
72
+ * to the context value after the initial request.
73
+ * @remarks
74
+ * Uses the default request strategy to locate the context.
75
+ */
76
+ request(target, context, callback, multiple = false) {
77
+ requestStrategy(target, context, callback, multiple);
78
+ },
79
+ /**
80
+ *
81
+ * @param target - The target to dispatch the context event on.
82
+ * @param context - The context to locate.
83
+ * @param callback - The callback to invoke with the context value.
84
+ * @param multiple - Whether the context requestor expects to handle updates
85
+ * to the context value after the initial request.
86
+ * @remarks
87
+ * This API does NOT use the default request strategy. It always dispatches
88
+ * an event through the DOM.
89
+ */
90
+ dispatch(target, context, callback, multiple = false) {
91
+ target.dispatchEvent(new ContextEvent(context, callback, multiple));
92
+ },
93
+ provide(target, context, value) {
94
+ this.handle(target, (event) => {
95
+ event.stopImmediatePropagation();
96
+ event.callback(value);
97
+ }, context);
98
+ },
99
+ /**
100
+ *
101
+ * @param target - The target on which to handle context requests.
102
+ * @param callback - The callback to invoke when a context request is received.
103
+ * @param context - The context to handle requests for.
104
+ * @remarks
105
+ * If a context is not provided then the callback will be invoked for all context
106
+ * requests that are received on the target.
107
+ */
108
+ handle(target, callback, context) {
109
+ if (context) {
110
+ target.addEventListener(contextEventType, (event) => {
111
+ if (event.context === context) {
112
+ callback(event);
113
+ }
114
+ });
115
+ }
116
+ else {
117
+ target.addEventListener(contextEventType, callback);
118
+ }
119
+ },
120
+ /**
121
+ * Defines a getter-only property on the target that will return the context
122
+ * value for the target.
123
+ * @param target The target to define the property on.
124
+ * @param propertyName The name of the property to define.
125
+ * @param context The context that will be used to retrieve the property value.
126
+ * @remarks
127
+ * Uses the default request strategy to locate the context and will return the
128
+ * initialValue if the context isn't handled.
129
+ */
130
+ defineProperty(target, propertyName, context) {
131
+ const field = `$di_${propertyName}`;
132
+ Reflect.defineProperty(target, propertyName, {
133
+ get: function () {
134
+ var _a;
135
+ return (_a = this[field]) !== null && _a !== void 0 ? _a : (this[field] = Context.get(this, context));
136
+ },
137
+ });
138
+ },
139
+ });
140
+ Context.setDefaultRequestStrategy(Context.dispatch);
141
+ /**
142
+ * An event fired by a context requester to signal it desires a named context.
143
+ *
144
+ * A provider should inspect the `context` property of the event to determine if it has a value that can
145
+ * satisfy the request, calling the `callback` with the requested value if so.
146
+ *
147
+ * If the requested context event contains a truthy `multiple` value, then a provider can call the callback
148
+ * multiple times if the value is changed, if this is the case the provider should pass a `dispose`
149
+ * method to the callback which requesters can invoke to indicate they no longer wish to receive these updates.
150
+ * @public
151
+ */
152
+ export class ContextEvent extends Event {
153
+ constructor(context, callback, multiple) {
154
+ super(contextEventType, { bubbles: true, composed: true });
155
+ this.context = context;
156
+ this.callback = callback;
157
+ this.multiple = multiple;
158
+ }
159
+ }
@@ -0,0 +1,60 @@
1
+ import { emptyArray } from "./platform.js";
2
+ // Tiny polyfill for TypeScript's Reflect metadata API.
3
+ const metadataByTarget = new Map();
4
+ if (!("metadata" in Reflect)) {
5
+ Reflect.metadata = function (key, value) {
6
+ return function (target) {
7
+ Reflect.defineMetadata(key, value, target);
8
+ };
9
+ };
10
+ Reflect.defineMetadata = function (key, value, target) {
11
+ let metadata = metadataByTarget.get(target);
12
+ if (metadata === void 0) {
13
+ metadataByTarget.set(target, (metadata = new Map()));
14
+ }
15
+ metadata.set(key, value);
16
+ };
17
+ Reflect.getOwnMetadata = function (key, target) {
18
+ const metadata = metadataByTarget.get(target);
19
+ if (metadata !== void 0) {
20
+ return metadata.get(key);
21
+ }
22
+ return void 0;
23
+ };
24
+ }
25
+ /**
26
+ * Provides basic metadata capabilities used by Context and Dependency Injection.
27
+ */
28
+ export const Metadata = Object.freeze({
29
+ /**
30
+ * Gets the "design:paramtypes" metadata for the specified type.
31
+ * @param Type - The type to get the metadata for.
32
+ * @returns The metadata array or a frozen empty array if no metadata is found.
33
+ */
34
+ getDesignParamTypes: Type => {
35
+ var _a;
36
+ return (_a = Reflect.getOwnMetadata("design:paramtypes", Type)) !== null && _a !== void 0 ? _a : emptyArray;
37
+ },
38
+ /**
39
+ * Gets the "annotation:paramtypes" metadata for the specified type.
40
+ * @param Type - The type to get the metadata for.
41
+ * @returns The metadata array or a frozen empty array if no metadata is found.
42
+ */
43
+ getAnnotationParamTypes: Type => {
44
+ var _a;
45
+ return (_a = Reflect.getOwnMetadata("annotation:paramtypes", Type)) !== null && _a !== void 0 ? _a : emptyArray;
46
+ },
47
+ /**
48
+ *
49
+ * @param Type - Gets the "annotation:paramtypes" metadata for the specified type. If none is found,
50
+ * an empty, mutable metadata array is created and added.
51
+ * @returns The metadata array.
52
+ */
53
+ getOrCreateAnnotationParamTypes(Type) {
54
+ let types = this.getAnnotationParamTypes(Type);
55
+ if (types === emptyArray) {
56
+ Reflect.defineMetadata("annotation:paramtypes", (types = []), Type);
57
+ }
58
+ return types;
59
+ },
60
+ });
@@ -255,7 +255,7 @@ export const ArrayObserver = Object.freeze({
255
255
  * @returns The length of the array.
256
256
  * @public
257
257
  */
258
- export function length(array) {
258
+ export function lengthOf(array) {
259
259
  if (!array) {
260
260
  return 0;
261
261
  }