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

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 (113) hide show
  1. package/CHANGELOG.json +488 -0
  2. package/CHANGELOG.md +180 -1
  3. package/dist/dts/components/attributes.d.ts +15 -0
  4. package/dist/dts/components/{controller.d.ts → element-controller.d.ts} +74 -28
  5. package/dist/dts/components/fast-definitions.d.ts +41 -9
  6. package/dist/dts/components/fast-element.d.ts +14 -26
  7. package/dist/dts/components/hydration.d.ts +14 -0
  8. package/dist/{esm/observation/behavior.js → dts/components/install-hydration.d.ts} +0 -0
  9. package/dist/dts/context.d.ts +1 -1
  10. package/dist/dts/di/di.d.ts +894 -0
  11. package/dist/dts/dom-policy.d.ts +68 -0
  12. package/dist/dts/dom.d.ts +100 -0
  13. package/dist/dts/index.d.ts +5 -4
  14. package/dist/dts/index.rollup.d.ts +0 -1
  15. package/dist/dts/index.rollup.debug.d.ts +0 -1
  16. package/dist/dts/interfaces.d.ts +60 -79
  17. package/dist/dts/observation/observable.d.ts +99 -54
  18. package/dist/dts/pending-task.d.ts +20 -0
  19. package/dist/dts/platform.d.ts +7 -0
  20. package/dist/dts/polyfills.d.ts +0 -8
  21. package/dist/dts/state/exports.d.ts +3 -0
  22. package/dist/dts/state/reactive.d.ts +8 -0
  23. package/dist/dts/state/state.d.ts +141 -0
  24. package/dist/dts/state/visitor.d.ts +6 -0
  25. package/dist/dts/state/watch.d.ts +10 -0
  26. package/dist/dts/styles/css-directive.d.ts +2 -2
  27. package/dist/dts/styles/css.d.ts +0 -5
  28. package/dist/dts/styles/element-styles.d.ts +10 -17
  29. package/dist/dts/styles/host.d.ts +68 -0
  30. package/dist/dts/styles/style-strategy.d.ts +42 -0
  31. package/dist/dts/templating/binding-signal.d.ts +12 -27
  32. package/dist/dts/templating/binding-two-way.d.ts +22 -37
  33. package/dist/dts/templating/binding.d.ts +76 -208
  34. package/dist/dts/templating/children.d.ts +1 -1
  35. package/dist/dts/templating/compiler.d.ts +11 -13
  36. package/dist/dts/templating/html-directive.d.ts +91 -97
  37. package/dist/dts/templating/node-observation.d.ts +15 -6
  38. package/dist/dts/templating/ref.d.ts +7 -11
  39. package/dist/dts/templating/render.d.ts +296 -0
  40. package/dist/dts/templating/repeat.d.ts +23 -34
  41. package/dist/dts/templating/slotted.d.ts +1 -1
  42. package/dist/dts/templating/template.d.ts +92 -14
  43. package/dist/dts/templating/view.d.ts +81 -11
  44. package/dist/dts/templating/when.d.ts +3 -3
  45. package/dist/dts/testing/exports.d.ts +3 -0
  46. package/dist/dts/testing/fakes.d.ts +14 -0
  47. package/dist/dts/testing/fixture.d.ts +84 -0
  48. package/dist/dts/testing/timeout.d.ts +7 -0
  49. package/dist/dts/utilities.d.ts +53 -18
  50. package/dist/esm/components/attributes.js +28 -5
  51. package/dist/esm/components/{controller.js → element-controller.js} +239 -137
  52. package/dist/esm/components/fast-definitions.js +38 -30
  53. package/dist/esm/components/fast-element.js +27 -16
  54. package/dist/esm/components/hydration.js +35 -0
  55. package/dist/esm/components/install-hydration.js +2 -0
  56. package/dist/esm/context.js +5 -1
  57. package/dist/esm/debug.js +40 -5
  58. package/dist/esm/di/di.js +1430 -0
  59. package/dist/esm/dom-policy.js +337 -0
  60. package/dist/esm/dom.js +101 -0
  61. package/dist/esm/index.js +4 -2
  62. package/dist/esm/index.rollup.debug.js +3 -1
  63. package/dist/esm/index.rollup.js +3 -1
  64. package/dist/esm/interfaces.js +52 -0
  65. package/dist/esm/observation/arrays.js +303 -2
  66. package/dist/esm/observation/observable.js +88 -142
  67. package/dist/esm/observation/update-queue.js +2 -2
  68. package/dist/esm/pending-task.js +16 -0
  69. package/dist/esm/platform.js +27 -2
  70. package/dist/esm/polyfills.js +3 -61
  71. package/dist/esm/state/exports.js +3 -0
  72. package/dist/esm/state/reactive.js +34 -0
  73. package/dist/esm/state/state.js +148 -0
  74. package/dist/esm/state/visitor.js +28 -0
  75. package/dist/esm/state/watch.js +36 -0
  76. package/dist/esm/styles/css.js +4 -9
  77. package/dist/esm/styles/element-styles.js +14 -33
  78. package/dist/esm/styles/host.js +1 -0
  79. package/dist/esm/styles/style-strategy.js +1 -0
  80. package/dist/esm/templating/binding-signal.js +67 -62
  81. package/dist/esm/templating/binding-two-way.js +72 -39
  82. package/dist/esm/templating/binding.js +142 -286
  83. package/dist/esm/templating/children.js +8 -4
  84. package/dist/esm/templating/compiler.js +59 -43
  85. package/dist/esm/templating/html-directive.js +56 -75
  86. package/dist/esm/templating/node-observation.js +20 -13
  87. package/dist/esm/templating/ref.js +4 -12
  88. package/dist/esm/templating/render.js +402 -0
  89. package/dist/esm/templating/repeat.js +88 -75
  90. package/dist/esm/templating/template.js +132 -60
  91. package/dist/esm/templating/view.js +113 -29
  92. package/dist/esm/templating/when.js +5 -4
  93. package/dist/esm/testing/exports.js +3 -0
  94. package/dist/esm/testing/fakes.js +107 -0
  95. package/dist/esm/testing/fixture.js +86 -0
  96. package/dist/esm/testing/timeout.js +24 -0
  97. package/dist/esm/utilities.js +95 -95
  98. package/dist/fast-element.api.json +9487 -8326
  99. package/dist/fast-element.d.ts +847 -644
  100. package/dist/fast-element.debug.js +1993 -1166
  101. package/dist/fast-element.debug.min.js +1 -1
  102. package/dist/fast-element.js +1903 -1111
  103. package/dist/fast-element.min.js +1 -1
  104. package/dist/fast-element.untrimmed.d.ts +911 -701
  105. package/docs/api-report.md +329 -252
  106. package/package.json +38 -16
  107. package/dist/dts/hooks.d.ts +0 -20
  108. package/dist/dts/observation/behavior.d.ts +0 -19
  109. package/dist/dts/observation/splice-strategies.d.ts +0 -13
  110. package/dist/dts/templating/dom.d.ts +0 -41
  111. package/dist/esm/hooks.js +0 -32
  112. package/dist/esm/observation/splice-strategies.js +0 -400
  113. package/dist/esm/templating/dom.js +0 -49
@@ -1,4 +1,5 @@
1
- // ensure FAST global - duplicated in polyfills.ts and debug.ts
1
+ import "./polyfills.js";
2
+ // ensure FAST global - duplicated debug.ts
2
3
  const propConfig = {
3
4
  configurable: false,
4
5
  enumerable: false,
@@ -26,7 +27,7 @@ if (FAST.error === void 0) {
26
27
  Object.assign(FAST, {
27
28
  warn() { },
28
29
  error(code) {
29
- return new Error(`Code ${code}`);
30
+ return new Error(`Error ${code}`);
30
31
  },
31
32
  addMessages() { },
32
33
  });
@@ -57,7 +58,31 @@ export function createTypeRegistry() {
57
58
  return typeToDefinition.get(key);
58
59
  },
59
60
  getForInstance(object) {
61
+ if (object === null || object === void 0) {
62
+ return void 0;
63
+ }
60
64
  return typeToDefinition.get(object.constructor);
61
65
  },
62
66
  });
63
67
  }
68
+ /**
69
+ * Creates a function capable of locating metadata associated with a type.
70
+ * @returns A metadata locator function.
71
+ * @internal
72
+ */
73
+ export function createMetadataLocator() {
74
+ const metadataLookup = new WeakMap();
75
+ return function (target) {
76
+ let metadata = metadataLookup.get(target);
77
+ if (metadata === void 0) {
78
+ let currentTarget = Reflect.getPrototypeOf(target);
79
+ while (metadata === void 0 && currentTarget !== null) {
80
+ metadata = metadataLookup.get(currentTarget);
81
+ currentTarget = Reflect.getPrototypeOf(currentTarget);
82
+ }
83
+ metadata = metadata === void 0 ? [] : metadata.slice(0);
84
+ metadataLookup.set(target, metadata);
85
+ }
86
+ return metadata;
87
+ };
88
+ }
@@ -1,10 +1,13 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
1
2
  (function ensureGlobalThis() {
2
3
  if (typeof globalThis !== "undefined") {
3
4
  // We're running in a modern environment.
4
5
  return;
5
6
  }
7
+ // @ts-ignore
6
8
  if (typeof global !== "undefined") {
7
9
  // We're running in NodeJS
10
+ // @ts-ignore
8
11
  global.globalThis = global;
9
12
  }
10
13
  else if (typeof self !== "undefined") {
@@ -22,64 +25,3 @@
22
25
  result.globalThis = result;
23
26
  }
24
27
  })();
25
- // API-only Polyfill for trustedTypes
26
- if (!globalThis.trustedTypes) {
27
- globalThis.trustedTypes = {
28
- createPolicy: (n, r) => r,
29
- };
30
- }
31
- // ensure FAST global - duplicated in platform.ts
32
- const propConfig = {
33
- configurable: false,
34
- enumerable: false,
35
- writable: false,
36
- };
37
- if (globalThis.FAST === void 0) {
38
- Reflect.defineProperty(globalThis, "FAST", Object.assign({ value: Object.create(null) }, propConfig));
39
- }
40
- const FAST = globalThis.FAST;
41
- if (FAST.getById === void 0) {
42
- const storage = Object.create(null);
43
- Reflect.defineProperty(FAST, "getById", Object.assign({ value(id, initialize) {
44
- let found = storage[id];
45
- if (found === void 0) {
46
- found = initialize ? (storage[id] = initialize()) : null;
47
- }
48
- return found;
49
- } }, propConfig));
50
- }
51
- // duplicated from DOM
52
- const supportsAdoptedStyleSheets = Array.isArray(document.adoptedStyleSheets) &&
53
- "replace" in CSSStyleSheet.prototype;
54
- function usableStyleTarget(target) {
55
- return target === document ? document.body : target;
56
- }
57
- let id = 0;
58
- const nextStyleId = () => `fast-${++id}`;
59
- export class StyleElementStrategy {
60
- constructor(styles) {
61
- this.styles = styles;
62
- this.styleClass = nextStyleId();
63
- }
64
- addStylesTo(target) {
65
- target = usableStyleTarget(target);
66
- const styles = this.styles;
67
- const styleClass = this.styleClass;
68
- for (let i = 0; i < styles.length; i++) {
69
- const element = document.createElement("style");
70
- element.innerHTML = styles[i];
71
- element.className = styleClass;
72
- target.append(element);
73
- }
74
- }
75
- removeStylesFrom(target) {
76
- const styles = target.querySelectorAll(`.${this.styleClass}`);
77
- target = usableStyleTarget(target);
78
- for (let i = 0, ii = styles.length; i < ii; ++i) {
79
- target.removeChild(styles[i]);
80
- }
81
- }
82
- }
83
- if (!supportsAdoptedStyleSheets) {
84
- FAST.getById(/* KernelServiceId.styleSheetStrategy */ 5, () => StyleElementStrategy);
85
- }
@@ -0,0 +1,3 @@
1
+ export { reactive } from "./reactive.js";
2
+ export { watch } from "./watch.js";
3
+ export * from "./state.js";
@@ -0,0 +1,34 @@
1
+ import { noop } from "../interfaces.js";
2
+ import { Observable } from "../observation/observable.js";
3
+ import { visitObject } from "./visitor.js";
4
+ const observed = new WeakSet();
5
+ const makeObserverVisitor = {
6
+ visitObject: noop,
7
+ visitArray: noop,
8
+ visitProperty(object, propertyName, value) {
9
+ Reflect.defineProperty(object, propertyName, {
10
+ enumerable: true,
11
+ get() {
12
+ Observable.track(object, propertyName);
13
+ return value;
14
+ },
15
+ set(newValue) {
16
+ if (value !== newValue) {
17
+ value = newValue;
18
+ Observable.notify(object, propertyName);
19
+ }
20
+ },
21
+ });
22
+ },
23
+ };
24
+ /**
25
+ * Converts a plain object to a reactive, observable object.
26
+ * @param object - The object to make reactive.
27
+ * @param deep - Indicates whether or not to deeply convert the oject.
28
+ * @returns The converted object.
29
+ * @beta
30
+ */
31
+ export function reactive(object, deep = false) {
32
+ visitObject(object, deep, makeObserverVisitor, void 0, observed);
33
+ return object;
34
+ }
@@ -0,0 +1,148 @@
1
+ // Inspired by https://www.starbeamjs.com/
2
+ import { isFunction, isString } from "../interfaces.js";
3
+ import { Observable } from "../observation/observable.js";
4
+ import { reactive } from "./reactive.js";
5
+ const defaultStateOptions = {
6
+ deep: false,
7
+ };
8
+ /**
9
+ * Creates a reactive state value.
10
+ * @param value - The initial state value.
11
+ * @param options - Options to customize the state or a friendly name.
12
+ * @returns A State instance.
13
+ * @beta
14
+ */
15
+ export function state(value, options = defaultStateOptions) {
16
+ var _a;
17
+ if (isString(options)) {
18
+ options = { deep: false, name: options };
19
+ }
20
+ const host = reactive({ value }, options.deep);
21
+ const state = (() => host.value);
22
+ Object.defineProperty(state, "current", {
23
+ get: () => host.value,
24
+ set: (value) => (host.value = value),
25
+ });
26
+ Object.defineProperty(state, "name", {
27
+ value: (_a = options.name) !== null && _a !== void 0 ? _a : "SharedState",
28
+ });
29
+ state.set = (value) => (host.value = value);
30
+ state.asReadonly = () => {
31
+ const readonlyState = (() => host.value);
32
+ Object.defineProperty(readonlyState, "current", {
33
+ get: () => host.value,
34
+ });
35
+ Object.defineProperty(readonlyState, "name", {
36
+ value: `${state.name} (Readonly)`,
37
+ });
38
+ return Object.freeze(readonlyState);
39
+ };
40
+ return state;
41
+ }
42
+ /**
43
+ * Creates a reactive state that has its value associated with a specific owner.
44
+ * @param value - The initial value or a factory that provides an initial value for each owner.
45
+ * @param options - Options to customize the state or a friendly name.
46
+ * @returns An OwnedState instance.
47
+ * @beta
48
+ */
49
+ export function ownedState(value, options = defaultStateOptions) {
50
+ var _a;
51
+ if (isString(options)) {
52
+ options = { deep: false, name: options };
53
+ }
54
+ if (!isFunction(value)) {
55
+ const v = value;
56
+ value = () => v;
57
+ }
58
+ const storage = new WeakMap();
59
+ const getHost = (owner) => {
60
+ let host = storage.get(owner);
61
+ if (host === void 0) {
62
+ host = reactive({ value: value() }, options.deep);
63
+ storage.set(owner, host);
64
+ }
65
+ return host;
66
+ };
67
+ const state = ((owner) => getHost(owner).value);
68
+ Object.defineProperty(state, "name", {
69
+ value: (_a = options.name) !== null && _a !== void 0 ? _a : "OwnedState",
70
+ });
71
+ state.set = (owner, value) => (getHost(owner).value = value);
72
+ state.asReadonly = () => {
73
+ const readonlyState = ((owner) => getHost(owner).value);
74
+ Object.defineProperty(readonlyState, "name", {
75
+ value: `${state.name} (Readonly)`,
76
+ });
77
+ return Object.freeze(readonlyState);
78
+ };
79
+ return state;
80
+ }
81
+ /**
82
+ * Creates a ComputedState.
83
+ * @param initialize - The initialization callback.
84
+ * @param name - A friendly name for this computation.
85
+ * @returns A ComputedState
86
+ * @beta
87
+ */
88
+ export function computedState(initialize, name = "ComputedState") {
89
+ let setupCallback = null;
90
+ const builder = {
91
+ on: {
92
+ setup(callback) {
93
+ setupCallback = callback;
94
+ },
95
+ },
96
+ };
97
+ const computer = initialize(builder);
98
+ const host = reactive({ value: null }, false);
99
+ const output = (() => host.value);
100
+ Object.defineProperty(output, "current", {
101
+ get: () => host.value,
102
+ });
103
+ Object.defineProperty(output, "name", {
104
+ value: name,
105
+ });
106
+ // eslint-disable-next-line prefer-const
107
+ let computedNotifier;
108
+ const computedSubscriber = {
109
+ handleChange() {
110
+ host.value = computedNotifier.observe(null);
111
+ },
112
+ };
113
+ computedNotifier = Observable.binding(computer, computedSubscriber);
114
+ computedNotifier.setMode(false);
115
+ let cleanup;
116
+ let setupNotifier;
117
+ if (setupCallback) {
118
+ const setupSubscriber = {
119
+ handleChange() {
120
+ if (cleanup) {
121
+ cleanup();
122
+ }
123
+ cleanup = setupNotifier.observe(null);
124
+ host.value = computer();
125
+ },
126
+ };
127
+ setupNotifier = Observable.binding(setupCallback, setupSubscriber);
128
+ setupNotifier.setMode(false);
129
+ cleanup = setupNotifier.observe(null);
130
+ }
131
+ host.value = computedNotifier.observe(null);
132
+ output.dispose = () => {
133
+ if (cleanup) {
134
+ cleanup();
135
+ }
136
+ if (setupNotifier) {
137
+ setupNotifier.dispose();
138
+ }
139
+ computedNotifier.dispose();
140
+ };
141
+ output.subscribe = (subscriber) => {
142
+ computedNotifier.subscribe(subscriber);
143
+ };
144
+ output.unsubscribe = (subscriber) => {
145
+ computedNotifier.unsubscribe(subscriber);
146
+ };
147
+ return output;
148
+ }
@@ -0,0 +1,28 @@
1
+ function shouldTraverse(value, traversed) {
2
+ return (value !== null &&
3
+ value !== void 0 &&
4
+ typeof value === "object" &&
5
+ !traversed.has(value));
6
+ }
7
+ export function visitObject(object, deep, visitor, data, traversed) {
8
+ if (!shouldTraverse(object, traversed)) {
9
+ return;
10
+ }
11
+ traversed.add(object);
12
+ if (Array.isArray(object)) {
13
+ visitor.visitArray(object, data);
14
+ for (const item of object) {
15
+ visitObject(item, deep, visitor, data, traversed);
16
+ }
17
+ }
18
+ else {
19
+ visitor.visitObject(object, data);
20
+ for (const key in object) {
21
+ const value = object[key];
22
+ visitor.visitProperty(object, key, value, data);
23
+ if (deep) {
24
+ visitObject(value, deep, visitor, data, traversed);
25
+ }
26
+ }
27
+ }
28
+ }
@@ -0,0 +1,36 @@
1
+ import { isFunction, noop } from "../interfaces.js";
2
+ import { ArrayObserver } from "../observation/arrays.js";
3
+ import { Observable } from "../observation/observable.js";
4
+ import { visitObject } from "./visitor.js";
5
+ function watchObject(object, data) {
6
+ const notifier = Observable.getNotifier(object);
7
+ notifier.subscribe(data.subscriber);
8
+ data.notifiers.push(notifier);
9
+ }
10
+ const watchVisitor = {
11
+ visitProperty: noop,
12
+ visitObject: watchObject,
13
+ visitArray: watchObject,
14
+ };
15
+ /**
16
+ * Deeply subscribes to changes in existing observable objects.
17
+ * @param object - The observable object to watch.
18
+ * @param subscriber - The handler to call when changes are made to the object.
19
+ * @returns A disposable that can be used to unsubscribe from change updates.
20
+ * @beta
21
+ */
22
+ export function watch(object, subscriber) {
23
+ const data = {
24
+ notifiers: [],
25
+ subscriber: isFunction(subscriber) ? { handleChange: subscriber } : subscriber,
26
+ };
27
+ ArrayObserver.enable();
28
+ visitObject(object, true, watchVisitor, data, new Set());
29
+ return {
30
+ dispose() {
31
+ for (const n of data.notifiers) {
32
+ n.unsubscribe(data.subscriber);
33
+ }
34
+ },
35
+ };
36
+ }
@@ -71,11 +71,11 @@ class CSSPartial {
71
71
  }
72
72
  return this.css;
73
73
  }
74
- bind(el) {
75
- el.$fastController.addStyles(this.styles);
74
+ addedCallback(controller) {
75
+ controller.addStyles(this.styles);
76
76
  }
77
- unbind(el) {
78
- el.$fastController.removeStyles(this.styles);
77
+ removedCallback(controller) {
78
+ controller.removeStyles(this.styles);
79
79
  }
80
80
  }
81
81
  CSSDirective.define(CSSPartial);
@@ -83,8 +83,3 @@ css.partial = (strings, ...values) => {
83
83
  const { styles, behaviors } = collectStyles(strings, values);
84
84
  return new CSSPartial(styles, behaviors);
85
85
  };
86
- /**
87
- * @deprecated Use css.partial instead.
88
- * @public
89
- */
90
- export const cssPartial = css.partial;
@@ -1,6 +1,3 @@
1
- import { FAST } from "../platform.js";
2
- import "../interfaces.js";
3
- const styleSheetCache = new Map();
4
1
  let DefaultStyleStrategy;
5
2
  function reduceStyles(styles) {
6
3
  return styles
@@ -71,39 +68,23 @@ export class ElementStyles {
71
68
  static setDefaultStrategy(Strategy) {
72
69
  DefaultStyleStrategy = Strategy;
73
70
  }
71
+ /**
72
+ * Normalizes a set of composable style options.
73
+ * @param styles - The style options to normalize.
74
+ * @returns A singular ElementStyles instance or undefined.
75
+ */
76
+ static normalize(styles) {
77
+ return styles === void 0
78
+ ? void 0
79
+ : Array.isArray(styles)
80
+ ? new ElementStyles(styles)
81
+ : styles instanceof ElementStyles
82
+ ? styles
83
+ : new ElementStyles([styles]);
84
+ }
74
85
  }
75
86
  /**
76
87
  * Indicates whether the DOM supports the adoptedStyleSheets feature.
77
88
  */
78
89
  ElementStyles.supportsAdoptedStyleSheets = Array.isArray(document.adoptedStyleSheets) &&
79
90
  "replace" in CSSStyleSheet.prototype;
80
- /**
81
- * https://wicg.github.io/construct-stylesheets/
82
- * https://developers.google.com/web/updates/2019/02/constructable-stylesheets
83
- *
84
- * @internal
85
- */
86
- export class AdoptedStyleSheetsStrategy {
87
- constructor(styles) {
88
- this.sheets = styles.map((x) => {
89
- if (x instanceof CSSStyleSheet) {
90
- return x;
91
- }
92
- let sheet = styleSheetCache.get(x);
93
- if (sheet === void 0) {
94
- sheet = new CSSStyleSheet();
95
- sheet.replaceSync(x);
96
- styleSheetCache.set(x, sheet);
97
- }
98
- return sheet;
99
- });
100
- }
101
- addStylesTo(target) {
102
- target.adoptedStyleSheets = [...target.adoptedStyleSheets, ...this.sheets];
103
- }
104
- removeStylesFrom(target) {
105
- const sheets = this.sheets;
106
- target.adoptedStyleSheets = target.adoptedStyleSheets.filter((x) => sheets.indexOf(x) === -1);
107
- }
108
- }
109
- ElementStyles.setDefaultStrategy(FAST.getById(5 /* KernelServiceId.styleSheetStrategy */, () => AdoptedStyleSheetsStrategy));
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,84 +1,89 @@
1
- import { isString } from "../interfaces.js";
2
- import { BindingMode, UpdateBinding } from "./binding.js";
3
- const signals = Object.create(null);
4
- /**
5
- * A binding behavior for signal bindings.
6
- * @public
7
- */
8
- export class SignalBinding extends UpdateBinding {
9
- constructor() {
10
- super(...arguments);
11
- this.handlerProperty = `${this.directive.id}-h`;
12
- }
13
- /**
14
- * Bind this behavior to the source.
15
- * @param source - The source to bind to.
16
- * @param context - The execution context that the binding is operating within.
17
- * @param targets - The targets that behaviors in a view can attach to.
18
- */
19
- bind(source, context, targets) {
20
- const directive = this.directive;
21
- const target = targets[directive.nodeId];
22
- const signal = this.getSignal(source, context);
23
- const handler = (target[this.handlerProperty] = () => {
24
- this.updateTarget(target, directive.targetAspect, directive.binding(source, context), source, context);
25
- });
26
- handler();
27
- const found = signals[signal];
1
+ import { isString, noop } from "../interfaces.js";
2
+ import { Binding } from "./html-directive.js";
3
+ const subscribers = Object.create(null);
4
+ export const Signal = Object.freeze({
5
+ subscribe(signal, subscriber) {
6
+ const found = subscribers[signal];
28
7
  if (found) {
29
- Array.isArray(found)
30
- ? found.push(handler)
31
- : (signals[signal] = [found, handler]);
8
+ found instanceof Set
9
+ ? found.add(subscriber)
10
+ : (subscribers[signal] = new Set([found, subscriber]));
32
11
  }
33
12
  else {
34
- signals[signal] = handler;
13
+ subscribers[signal] = subscriber;
35
14
  }
36
- }
37
- /**
38
- * Unbinds this behavior from the source.
39
- * @param source - The source to unbind from.
40
- * @param context - The execution context that the binding is operating within.
41
- * @param targets - The targets that behaviors in a view can attach to.
42
- */
43
- unbind(source, context, targets) {
44
- const signal = this.getSignal(source, context);
45
- const found = signals[signal];
46
- if (found && Array.isArray(found)) {
47
- const directive = this.directive;
48
- const target = targets[directive.nodeId];
49
- const handler = target[this.handlerProperty];
50
- const index = found.indexOf(handler);
51
- if (index !== -1) {
52
- found.splice(index, 1);
53
- }
15
+ },
16
+ unsubscribe(signal, subscriber) {
17
+ const found = subscribers[signal];
18
+ if (found && found instanceof Set) {
19
+ found.delete(subscriber);
54
20
  }
55
21
  else {
56
- signals[signal] = void 0;
22
+ subscribers[signal] = void 0;
57
23
  }
58
- }
59
- getSignal(source, context) {
60
- const options = this.directive.options;
61
- return isString(options) ? options : options(source, context);
62
- }
24
+ },
63
25
  /**
64
26
  * Sends the specified signal to signaled bindings.
65
27
  * @param signal - The signal to send.
66
28
  * @public
67
29
  */
68
- static send(signal) {
69
- const found = signals[signal];
30
+ send(signal) {
31
+ const found = subscribers[signal];
70
32
  if (found) {
71
- Array.isArray(found) ? found.forEach(x => x()) : found();
33
+ found instanceof Set
34
+ ? found.forEach(x => x.handleChange(found, signal))
35
+ : found.handleChange(this, signal);
36
+ }
37
+ },
38
+ });
39
+ class SignalObserver {
40
+ constructor(dataBinding, subscriber) {
41
+ this.dataBinding = dataBinding;
42
+ this.subscriber = subscriber;
43
+ this.isNotBound = true;
44
+ /**
45
+ * Opts out of JSON stringification.
46
+ * @internal
47
+ */
48
+ this.toJSON = noop;
49
+ }
50
+ bind(controller) {
51
+ if (this.isNotBound) {
52
+ Signal.subscribe(this.getSignal(controller), this);
53
+ controller.onUnbind(this);
54
+ this.isNotBound = false;
72
55
  }
56
+ return this.dataBinding.evaluate(controller.source, controller.context);
57
+ }
58
+ unbind(controller) {
59
+ this.isNotBound = true;
60
+ Signal.unsubscribe(this.getSignal(controller), this);
61
+ }
62
+ handleChange() {
63
+ this.subscriber.handleChange(this.dataBinding.evaluate, this);
64
+ }
65
+ getSignal(controller) {
66
+ const options = this.dataBinding.options;
67
+ return isString(options)
68
+ ? options
69
+ : options(controller.source, controller.context);
70
+ }
71
+ }
72
+ class SignalBinding extends Binding {
73
+ createObserver(directive, subscriber) {
74
+ return new SignalObserver(this, subscriber);
73
75
  }
74
76
  }
75
- const signalMode = BindingMode.define(SignalBinding);
76
77
  /**
77
78
  * Creates a signal binding configuration with the supplied options.
79
+ * @param expression - The binding to refresh when signaled.
78
80
  * @param options - The signal name or a binding to use to retrieve the signal name.
81
+ * @param policy - The security policy to associate with th binding.
79
82
  * @returns A binding configuration.
80
83
  * @public
81
84
  */
82
- export const signal = (options) => {
83
- return { mode: signalMode, options };
84
- };
85
+ export function signal(expression, options, policy) {
86
+ const binding = new SignalBinding(expression, policy);
87
+ binding.options = options;
88
+ return binding;
89
+ }