@symbiotejs/symbiote 3.9.0 → 3.10.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 (133) hide show
  1. package/README.md +12 -0
  2. package/core/AppRouter.js +97 -58
  3. package/core/PubSub.js +42 -11
  4. package/core/Symbiote.js +70 -46
  5. package/core/itemizeProcessor-keyed.js +88 -188
  6. package/core/itemizeProcessor.js +1 -12
  7. package/core/itemizeSetup.js +26 -3
  8. package/core/ownElements.js +4 -7
  9. package/core/resolveTemplate.js +12 -0
  10. package/core/tpl-processors.js +4 -2
  11. package/docs/README.md +2 -0
  12. package/docs/html-app-vs-htmx.md +55 -0
  13. package/docs/lifecycle.md +2 -0
  14. package/docs/list-rendering.md +10 -2
  15. package/docs/pubsub.md +3 -0
  16. package/docs/routing.md +15 -4
  17. package/html-app/BaseClass.js +203 -0
  18. package/html-app/README.md +369 -0
  19. package/html-app/action-data.js +168 -0
  20. package/html-app/actions.js +366 -0
  21. package/html-app/collection-actions.js +104 -0
  22. package/html-app/collection-data.js +234 -0
  23. package/html-app/collection-items.js +45 -0
  24. package/html-app/conditions.js +42 -0
  25. package/html-app/context-data.js +185 -0
  26. package/html-app/context-requests.js +374 -0
  27. package/html-app/context-storage.js +376 -0
  28. package/html-app/declarations.js +55 -0
  29. package/html-app/examples/assets/actions.html +56 -0
  30. package/html-app/examples/assets/collections.html +72 -0
  31. package/html-app/examples/assets/components.html +25 -0
  32. package/html-app/examples/assets/conditional.html +29 -0
  33. package/html-app/examples/assets/data.html +31 -0
  34. package/html-app/examples/assets/guide.html +4 -0
  35. package/html-app/examples/assets/imports.html +11 -0
  36. package/html-app/examples/assets/not-found.html +4 -0
  37. package/html-app/examples/assets/routes.json +58 -0
  38. package/html-app/examples/assets/routing.html +27 -0
  39. package/html-app/examples/assets/state.html +32 -0
  40. package/html-app/examples/assets/storage.html +28 -0
  41. package/html-app/examples/assets/tasks.json +25 -0
  42. package/html-app/examples/full-app.html +147 -0
  43. package/html-app/examples/todo-ref.html +37 -0
  44. package/html-app/fetch-source.js +147 -0
  45. package/html-app/fragment-content.js +29 -0
  46. package/html-app/fragment-source.js +169 -0
  47. package/html-app/index.js +34 -0
  48. package/html-app/initial-data.js +88 -0
  49. package/html-app/pub-map.js +93 -0
  50. package/html-app/router-data.js +50 -0
  51. package/html-app/router-navigation.js +70 -0
  52. package/html-app/storage-revisions.js +130 -0
  53. package/html-app/sym-collection.js +142 -0
  54. package/html-app/sym-component.js +20 -0
  55. package/html-app/sym-conditional.js +141 -0
  56. package/html-app/sym-context.js +176 -0
  57. package/html-app/sym-import.js +218 -0
  58. package/html-app/sym-router.js +107 -0
  59. package/package.json +18 -6
  60. package/scripts/check-html-app-package.js +117 -0
  61. package/scripts/check-html-app-size.js +91 -0
  62. package/scripts/update-exports.js +4 -0
  63. package/skills/symbiote/SKILL.md +64 -15
  64. package/skills/symbiote/agents/openai.yaml +1 -1
  65. package/types/core/AppRouter.d.ts +13 -4
  66. package/types/core/AppRouter.d.ts.map +1 -1
  67. package/types/core/PubSub.d.ts +1 -1
  68. package/types/core/PubSub.d.ts.map +1 -1
  69. package/types/core/Symbiote.d.ts +4 -1
  70. package/types/core/Symbiote.d.ts.map +1 -1
  71. package/types/core/itemizeProcessor-keyed.d.ts.map +1 -1
  72. package/types/core/itemizeProcessor.d.ts.map +1 -1
  73. package/types/core/itemizeSetup.d.ts +1 -0
  74. package/types/core/itemizeSetup.d.ts.map +1 -1
  75. package/types/core/ownElements.d.ts +1 -1
  76. package/types/core/ownElements.d.ts.map +1 -1
  77. package/types/core/resolveTemplate.d.ts +2 -0
  78. package/types/core/resolveTemplate.d.ts.map +1 -0
  79. package/types/core/tpl-processors.d.ts.map +1 -1
  80. package/types/html-app/BaseClass.d.ts +24 -0
  81. package/types/html-app/BaseClass.d.ts.map +1 -0
  82. package/types/html-app/action-data.d.ts +15 -0
  83. package/types/html-app/action-data.d.ts.map +1 -0
  84. package/types/html-app/actions.d.ts +104 -0
  85. package/types/html-app/actions.d.ts.map +1 -0
  86. package/types/html-app/collection-actions.d.ts +12 -0
  87. package/types/html-app/collection-actions.d.ts.map +1 -0
  88. package/types/html-app/collection-data.d.ts +13 -0
  89. package/types/html-app/collection-data.d.ts.map +1 -0
  90. package/types/html-app/collection-items.d.ts +7 -0
  91. package/types/html-app/collection-items.d.ts.map +1 -0
  92. package/types/html-app/conditions.d.ts +5 -0
  93. package/types/html-app/conditions.d.ts.map +1 -0
  94. package/types/html-app/context-data.d.ts +31 -0
  95. package/types/html-app/context-data.d.ts.map +1 -0
  96. package/types/html-app/context-requests.d.ts +97 -0
  97. package/types/html-app/context-requests.d.ts.map +1 -0
  98. package/types/html-app/context-storage.d.ts +102 -0
  99. package/types/html-app/context-storage.d.ts.map +1 -0
  100. package/types/html-app/declarations.d.ts +3 -0
  101. package/types/html-app/declarations.d.ts.map +1 -0
  102. package/types/html-app/fetch-source.d.ts +23 -0
  103. package/types/html-app/fetch-source.d.ts.map +1 -0
  104. package/types/html-app/fragment-content.d.ts +6 -0
  105. package/types/html-app/fragment-content.d.ts.map +1 -0
  106. package/types/html-app/fragment-source.d.ts +14 -0
  107. package/types/html-app/fragment-source.d.ts.map +1 -0
  108. package/types/html-app/index.d.ts +9 -0
  109. package/types/html-app/index.d.ts.map +1 -0
  110. package/types/html-app/initial-data.d.ts +8 -0
  111. package/types/html-app/initial-data.d.ts.map +1 -0
  112. package/types/html-app/pub-map.d.ts +19 -0
  113. package/types/html-app/pub-map.d.ts.map +1 -0
  114. package/types/html-app/router-data.d.ts +4 -0
  115. package/types/html-app/router-data.d.ts.map +1 -0
  116. package/types/html-app/router-navigation.d.ts +3 -0
  117. package/types/html-app/router-navigation.d.ts.map +1 -0
  118. package/types/html-app/storage-revisions.d.ts +11 -0
  119. package/types/html-app/storage-revisions.d.ts.map +1 -0
  120. package/types/html-app/sym-collection.d.ts +16 -0
  121. package/types/html-app/sym-collection.d.ts.map +1 -0
  122. package/types/html-app/sym-component.d.ts +5 -0
  123. package/types/html-app/sym-component.d.ts.map +1 -0
  124. package/types/html-app/sym-conditional.d.ts +10 -0
  125. package/types/html-app/sym-conditional.d.ts.map +1 -0
  126. package/types/html-app/sym-context.d.ts +16 -0
  127. package/types/html-app/sym-context.d.ts.map +1 -0
  128. package/types/html-app/sym-import.d.ts +19 -0
  129. package/types/html-app/sym-import.d.ts.map +1 -0
  130. package/types/html-app/sym-router.d.ts +12 -0
  131. package/types/html-app/sym-router.d.ts.map +1 -0
  132. package/types/utils/setNestedProp.d.ts.map +1 -1
  133. package/utils/setNestedProp.js +8 -0
@@ -0,0 +1,142 @@
1
+ import BaseClass from './BaseClass.js';
2
+ import { itemizeProcessor } from '../core/itemizeProcessor.js';
3
+ import { collectionItemsProcessor } from './collection-items.js';
4
+ import { dataReference, resolveData } from './action-data.js';
5
+ import { projectCollection } from './collection-data.js';
6
+
7
+ export class SymCollection extends BaseClass {
8
+ static observedAttributes = [
9
+ ...BaseClass.observedAttributes,
10
+ 'data',
11
+ 'filter',
12
+ 'search',
13
+ 'search-fields',
14
+ 'sort',
15
+ ];
16
+
17
+ init$ = {
18
+ items: [],
19
+ count: 0,
20
+ totalCount: 0,
21
+ empty: true,
22
+ };
23
+
24
+ /** @type {{ remove(): void }[]} */
25
+ #subscriptions = [];
26
+ #active = false;
27
+
28
+ /**
29
+ * @param {string} name
30
+ * @param {string | null} previous
31
+ * @param {string | null} value
32
+ */
33
+ attributeChangedCallback(name, previous, value) {
34
+ if (name === 'pub-map') {
35
+ super.attributeChangedCallback(name, previous, value);
36
+ } else if (previous !== value && this.#active) {
37
+ this.bindSource();
38
+ }
39
+ }
40
+
41
+ source() {
42
+ let reference = this.getAttribute('data');
43
+ if (!reference) {
44
+ throw new TypeError('sym-collection requires a data context reference.');
45
+ }
46
+ let target = resolveData(this, reference);
47
+ if (target.ctx === this.localCtx && target.name === 'items') {
48
+ throw new TypeError('Collection data must be separate from its derived items.');
49
+ }
50
+ return target;
51
+ }
52
+
53
+ prepare() {
54
+ super.prepare();
55
+ this.templateProcessors.add(collectionItemsProcessor);
56
+ this.#active = true;
57
+ this.bindSource();
58
+ }
59
+
60
+ connectFoundation() {
61
+ super.connectFoundation();
62
+ if (this.isConnected && this.connectedOnce && !this.#active) {
63
+ this.#active = true;
64
+ this.bindSource();
65
+ }
66
+ }
67
+
68
+ /**
69
+ * @param {string | DocumentFragment} [template]
70
+ * @param {boolean} [shadow]
71
+ */
72
+ render(template, shadow) {
73
+ this.templateProcessors.delete(itemizeProcessor);
74
+ super.render(template, shadow);
75
+ }
76
+
77
+ bindSource() {
78
+ for (let subscription of this.#subscriptions) {
79
+ subscription.remove();
80
+ }
81
+ this.#subscriptions = [];
82
+ let reference = this.getAttribute('data');
83
+ if (!reference) {
84
+ throw new TypeError('sym-collection requires a data context reference.');
85
+ }
86
+ let source = dataReference(this, reference);
87
+ if (source?.ctx === this.localCtx && source.name === 'items') {
88
+ throw new TypeError('Collection data must be separate from its derived items.');
89
+ }
90
+ let references = [
91
+ reference,
92
+ ];
93
+ for (let name of [
94
+ 'filter',
95
+ 'search',
96
+ 'sort',
97
+ ]) {
98
+ let reference = this.getAttribute(name);
99
+ if (reference) {
100
+ dataReference(this, reference, true);
101
+ references.push(reference);
102
+ }
103
+ }
104
+ // Project before installing subscriptions so invalid data never replaces a view.
105
+ this.project();
106
+ for (let reference of references) {
107
+ this.#subscriptions.push(this.sub(reference, () => this.project(), false));
108
+ }
109
+ }
110
+
111
+ project() {
112
+ let source = dataReference(this, this.getAttribute('data'));
113
+ let data = source?.ctx.has(source.name) ? source.ctx.read(source.name) : [];
114
+ let read = (/** @type {string} */ name, /** @type {unknown} */ fallback) => {
115
+ let reference = this.getAttribute(name);
116
+ if (!reference) {
117
+ return fallback;
118
+ }
119
+ let target = dataReference(this, reference, true);
120
+ return target?.ctx.has(target.name) ? target.ctx.read(target.name) : fallback;
121
+ };
122
+ let fields = (this.getAttribute('search-fields') || '').split(',').map((field) => field.trim()).filter(Boolean);
123
+ let items = projectCollection(data, read('filter', {}), read('search', ''), fields, read('sort', null));
124
+ this.$ = {
125
+ count: items.length,
126
+ totalCount: data.length,
127
+ empty: items.length === 0,
128
+ items,
129
+ };
130
+ }
131
+
132
+ disconnectedCallback() {
133
+ this.#active = false;
134
+ for (let subscription of this.#subscriptions) {
135
+ subscription.remove();
136
+ }
137
+ this.#subscriptions = [];
138
+ super.disconnectedCallback();
139
+ }
140
+ }
141
+
142
+ export default SymCollection;
@@ -0,0 +1,20 @@
1
+ import BaseClass from './BaseClass.js';
2
+ import { registerDeclaration } from './declarations.js';
3
+
4
+ export class SymComponent extends BaseClass {
5
+ connectedCallback() {
6
+ if (this.hasAttribute('reg')) {
7
+ registerDeclaration(this, this.Symbiote);
8
+ return;
9
+ }
10
+ super.connectedCallback();
11
+ }
12
+
13
+ connectFoundation() {
14
+ if (!this.hasAttribute('reg')) {
15
+ super.connectFoundation();
16
+ }
17
+ }
18
+ }
19
+
20
+ export default SymComponent;
@@ -0,0 +1,141 @@
1
+ import BaseClass from './BaseClass.js';
2
+ import { FragmentContent, disposeContent } from './fragment-content.js';
3
+ import { dataReference } from './action-data.js';
4
+ import { conditionState } from './conditions.js';
5
+
6
+ export class SymConditional extends BaseClass {
7
+ static observedAttributes = [
8
+ ...BaseClass.observedAttributes,
9
+ 'when',
10
+ 'is',
11
+ 'is-type',
12
+ 'behavior',
13
+ ];
14
+
15
+ #reference = '';
16
+ /** @type {{ remove(): void } | undefined} */
17
+ #subscription;
18
+ /** @type {DocumentFragment | undefined} */
19
+ #blueprint;
20
+ /** @type {FragmentContent | undefined} */
21
+ #content;
22
+ #active = false;
23
+
24
+ /**
25
+ * @param {string} name
26
+ * @param {string | null} previous
27
+ * @param {string | null} value
28
+ */
29
+ attributeChangedCallback(name, previous, value) {
30
+ if (name === 'pub-map') {
31
+ super.attributeChangedCallback(name, previous, value);
32
+ } else if (previous !== value && this.#active) {
33
+ this.bindCondition();
34
+ }
35
+ }
36
+
37
+ prepare() {
38
+ super.prepare();
39
+ this.processInnerHtml = false;
40
+ }
41
+
42
+ /**
43
+ * @param {string | DocumentFragment} [template]
44
+ * @param {boolean} [shadow]
45
+ */
46
+ render(template, shadow) {
47
+ this.restoreContent();
48
+ this.#blueprint = this.ownerDocument.createDocumentFragment();
49
+ this.#blueprint.append(...this.childNodes);
50
+ let custom = this.allowCustomTemplate;
51
+ this.allowCustomTemplate = false;
52
+ try {
53
+ super.render(template, shadow);
54
+ } finally {
55
+ this.allowCustomTemplate = custom;
56
+ }
57
+ this.#active = true;
58
+ this.bindCondition();
59
+ }
60
+
61
+ connectFoundation() {
62
+ super.connectFoundation();
63
+ if (this.isConnected && this.connectedOnce && !this.#active) {
64
+ this.#active = true;
65
+ this.bindCondition();
66
+ }
67
+ }
68
+
69
+ bindCondition() {
70
+ this.#subscription?.remove();
71
+ this.#subscription = undefined;
72
+ let reference = this.getAttribute('when');
73
+ if (!reference) {
74
+ throw new TypeError('sym-conditional requires a when context reference.');
75
+ }
76
+ dataReference(this, reference, true);
77
+ this.#reference = reference;
78
+ this.updateContent();
79
+ this.#subscription = this.sub(reference, () => this.updateContent());
80
+ }
81
+
82
+ updateContent() {
83
+ if (!this.#active || !this.isConnected || !this.#reference) {
84
+ return;
85
+ }
86
+ let source = dataReference(this, this.#reference, true);
87
+ let value = source?.ctx.has(source.name) ? source.ctx.read(source.name) : undefined;
88
+ let {
89
+ visible,
90
+ dom,
91
+ } = conditionState(
92
+ value,
93
+ this.getAttribute('is'),
94
+ this.getAttribute('behavior') ?? 'show',
95
+ this.getAttribute('is-type'),
96
+ );
97
+ this.hidden = !visible;
98
+ this.inert = !visible;
99
+ if (dom && !visible) {
100
+ this.unmount();
101
+ } else if (!this.#content) {
102
+ let content = new FragmentContent();
103
+ content.context = this.localCtx;
104
+ content.style.display = 'contents';
105
+ content.append(this.#blueprint.cloneNode(true));
106
+ for (let name of [
107
+ 'use-template',
108
+ 'process-inner-html',
109
+ ]) {
110
+ if (this.hasAttribute(name)) {
111
+ content.setAttribute(name, this.getAttribute(name));
112
+ }
113
+ }
114
+ this.#content = content;
115
+ this.append(content);
116
+ }
117
+ }
118
+
119
+ unmount() {
120
+ let content = this.#content;
121
+ this.#content = undefined;
122
+ if (content) {
123
+ content.remove();
124
+ disposeContent(content);
125
+ }
126
+ }
127
+
128
+ disconnectedCallback() {
129
+ this.#active = false;
130
+ this.#subscription?.remove();
131
+ this.#subscription = undefined;
132
+ super.disconnectedCallback();
133
+ }
134
+
135
+ destroyCallback() {
136
+ this.unmount();
137
+ super.destroyCallback();
138
+ }
139
+ }
140
+
141
+ export default SymConditional;
@@ -0,0 +1,176 @@
1
+ import { PubSub } from '../core/PubSub.js';
2
+ import BaseClass from './BaseClass.js';
3
+ import { initialDefaults, isDataKey } from './initial-data.js';
4
+ import { ContextData } from './context-data.js';
5
+ import { ContextRequests } from './context-requests.js';
6
+ import { ContextStorage } from './context-storage.js';
7
+
8
+ export class SymContext extends BaseClass {
9
+ static observedAttributes = [
10
+ ...BaseClass.observedAttributes,
11
+ 'src',
12
+ 'sync',
13
+ 'sync-method',
14
+ 'sync-keys',
15
+ 'request-timeout',
16
+ 'max-bytes',
17
+ 'storage-key',
18
+ 'storage-keys',
19
+ ];
20
+ contextData = new ContextData(this);
21
+ requests = new ContextRequests(this);
22
+ storage = new ContextStorage(this);
23
+
24
+ /**
25
+ * @param {string} name
26
+ * @param {string | null} previous
27
+ * @param {string | null} value
28
+ */
29
+ attributeChangedCallback(name, previous, value) {
30
+ if (name === 'pub-map') {
31
+ super.attributeChangedCallback(name, previous, value);
32
+ } else if (previous !== value) {
33
+ if (name === 'storage-key' || name === 'storage-keys') {
34
+ this.storage.schedule();
35
+ } else {
36
+ this.requests.configure(name);
37
+ }
38
+ }
39
+ }
40
+
41
+ connectFoundation() {
42
+ super.connectFoundation();
43
+ if (this.isConnected && this.connectedOnce && !this.requests.connected) {
44
+ this.storage.connect();
45
+ this.requests.connect();
46
+ }
47
+ }
48
+
49
+ /** @type {PubSub | undefined} */
50
+ #provided;
51
+ #name = '';
52
+ #merging = false;
53
+ #active = false;
54
+ /** @type {Map<string, unknown>} */
55
+ #values = new Map();
56
+ /** @type {Set<string>} */
57
+ #watched = new Set();
58
+
59
+ get localCtx() {
60
+ return this.#provided ?? super.localCtx;
61
+ }
62
+
63
+ prepare() {
64
+ super.prepare();
65
+ let name = this.getAttribute('ctx')?.trim() || '';
66
+ if (name) {
67
+ if (PubSub.getCtx(name, false)) {
68
+ throw new TypeError('A named context already has an owner: ' + name);
69
+ }
70
+ let initial = {
71
+ ...this.init$,
72
+ ...super.localCtx.store,
73
+ };
74
+ this.#name = name;
75
+ this.#provided = PubSub.registerCtx(initial, name);
76
+ initialDefaults.set(this.#provided, initialDefaults.get(super.localCtx) || {});
77
+ }
78
+ this.requests.setup();
79
+ }
80
+
81
+ /**
82
+ * Preserve native $ merge semantics; incoming merges do not echo to pub-to.
83
+ * @param {Record<string, unknown>} values
84
+ * @param {boolean} [rewrite]
85
+ */
86
+ add$(values, rewrite = false) {
87
+ this.contextData.incoming(values);
88
+ for (let key of Object.keys(values)) {
89
+ if (isDataKey(key) && !this.#watched.has(key)) {
90
+ this.contextData.changed(key, this.localCtx.has(key) ? this.localCtx.read(key) : undefined, values[key]);
91
+ }
92
+ }
93
+ this.#merging = true;
94
+ try {
95
+ super.add$(values, rewrite);
96
+ for (let key of Object.keys(values)) {
97
+ if (this.#active && isDataKey(key) && this.localCtx.has(key)) {
98
+ this.watchData(key);
99
+ this.#values.set(key, this.localCtx.read(key));
100
+ }
101
+ }
102
+ } finally {
103
+ this.#merging = false;
104
+ }
105
+ }
106
+
107
+ renderCallback() {
108
+ super.renderCallback();
109
+ this.#active = true;
110
+ for (let key of Object.keys(this.localCtx.store)) {
111
+ if (isDataKey(key)) { this.watchData(key); }
112
+ }
113
+ }
114
+
115
+ /** @param {string} key */
116
+ watchData(key) {
117
+ this.#values.set(key, this.localCtx.read(key));
118
+ if (this.#watched.has(key)) { return; }
119
+ this.#watched.add(key);
120
+ let subscription = this.localCtx.sub(key, (value) => this.publishData(key, value), false);
121
+ if (subscription) { this.allSubs.add(subscription); }
122
+ }
123
+
124
+ /**
125
+ * @param {string} key
126
+ * @param {unknown} value
127
+ */
128
+ publishData(key, value) {
129
+ let previous = this.#values.get(key);
130
+ this.#values.set(key, value);
131
+ this.contextData.changed(key, previous, value);
132
+ let target = this.getAttribute('pub-to')?.trim();
133
+ if (this.#merging || !this.isConnected || !target || Object.is(previous, value)) { return; }
134
+ let resolved = this.Symbiote.parseProp(target, this);
135
+ if (!resolved || !resolved.ctx.has(resolved.name) || !isDataKey(resolved.name)
136
+ || resolved.ctx === this.localCtx) {
137
+ throw new TypeError('pub-to requires an existing data property in another context.');
138
+ }
139
+ let data = Object.fromEntries([...this.#values].filter(([
140
+ name,
141
+ current,
142
+ ]) => isDataKey(name) && typeof current !== 'function' && typeof current !== 'symbol'));
143
+ this.$[target] = data;
144
+ }
145
+
146
+ /** @param {Record<string, unknown>} data */
147
+ mergeSource(data) {
148
+ this.contextData.applying = true;
149
+ try {
150
+ this.$ = data;
151
+ } finally {
152
+ this.contextData.applying = false;
153
+ }
154
+ }
155
+
156
+ disconnectedCallback() {
157
+ this.storage.disconnect();
158
+ this.requests.disconnect();
159
+ super.disconnectedCallback();
160
+ }
161
+
162
+ destroyCallback() {
163
+ this.requests.destroy();
164
+ super.destroyCallback();
165
+ if (this.#provided && PubSub.getCtx(this.#name, false) === this.#provided) {
166
+ PubSub.deleteCtx(this.#name);
167
+ }
168
+ this.#active = false;
169
+ this.#provided = undefined;
170
+ this.#name = '';
171
+ this.#values.clear();
172
+ this.#watched.clear();
173
+ }
174
+ }
175
+
176
+ export default SymContext;
@@ -0,0 +1,218 @@
1
+ import BaseClass from './BaseClass.js';
2
+ import { FragmentContent, disposeContent } from './fragment-content.js';
3
+ import { fragmentSources, fragmentTemplate, importSource } from './fragment-source.js';
4
+ import { fetchSource, requestLimits, SourceError } from './fetch-source.js';
5
+
6
+ export class SymImport extends BaseClass {
7
+ static observedAttributes = [
8
+ ...BaseClass.observedAttributes,
9
+ 'src',
10
+ 'request-timeout',
11
+ 'max-bytes',
12
+ ];
13
+ #active = false;
14
+ #scheduled = false;
15
+ /** @type {AbortController | undefined} */
16
+ #controller;
17
+ /** @type {FragmentContent | undefined} */
18
+ #content;
19
+
20
+ /**
21
+ * @param {string} name
22
+ * @param {string | null} previous
23
+ * @param {string | null} value
24
+ */
25
+ attributeChangedCallback(name, previous, value) {
26
+ if (name === 'pub-map') {
27
+ super.attributeChangedCallback(name, previous, value);
28
+ } else if (previous !== value && this.#active) {
29
+ this.cancel();
30
+ this.schedule();
31
+ }
32
+ }
33
+
34
+ prepare() {
35
+ super.prepare();
36
+ this.processInnerHtml = false;
37
+ this.localCtx.add('__htmlAppImport', {}, true);
38
+ for (let key of [
39
+ 'loading',
40
+ 'loadError',
41
+ 'loadErrorMessage',
42
+ ]) {
43
+ this.localCtx.add('+' + key, () => this.$.__htmlAppImport[key], true);
44
+ }
45
+ this.status(false);
46
+ }
47
+
48
+ /**
49
+ * @param {string | DocumentFragment} [template]
50
+ * @param {boolean} [shadow]
51
+ */
52
+ render(template, shadow) {
53
+ this.restoreContent();
54
+ let fallback = this.ownerDocument.createDocumentFragment();
55
+ fallback.append(...this.childNodes);
56
+ let custom = this.allowCustomTemplate;
57
+ this.allowCustomTemplate = false;
58
+ try {
59
+ super.render(template, shadow);
60
+ } finally {
61
+ this.allowCustomTemplate = custom;
62
+ }
63
+ if (fallback.childNodes.length) {
64
+ this.mount(fallback);
65
+ }
66
+ }
67
+
68
+ connectFoundation() {
69
+ super.connectFoundation();
70
+ if (this.isConnected && this.connectedOnce && !this.#active) {
71
+ this.#active = true;
72
+ this.schedule();
73
+ }
74
+ }
75
+
76
+ /**
77
+ * @param {boolean} loading
78
+ * @param {{ operation: string, code: string, message: string, status: number | null } | null} [error]
79
+ */
80
+ status(loading, error = null) {
81
+ this.localCtx.pub('__htmlAppImport', {
82
+ loading,
83
+ loadError: error,
84
+ loadErrorMessage: error?.message ?? '',
85
+ });
86
+ }
87
+
88
+ cancel() {
89
+ this.#controller?.abort();
90
+ this.#controller = undefined;
91
+ this.#scheduled = false;
92
+ if (this.connectedOnce) {
93
+ this.status(false);
94
+ }
95
+ }
96
+
97
+ schedule() {
98
+ if (this.#scheduled || !this.#active || globalThis.__SYMBIOTE_SSR) {
99
+ return;
100
+ }
101
+ this.#scheduled = true;
102
+ queueMicrotask(() => {
103
+ if (this.#scheduled && this.isConnected) {
104
+ this.#scheduled = false;
105
+ void this.load();
106
+ }
107
+ });
108
+ }
109
+
110
+ /**
111
+ * @param {DocumentFragment} fragment
112
+ * @param {ReturnType<typeof importSource>} [source]
113
+ */
114
+ mount(fragment, source) {
115
+ let content = new FragmentContent();
116
+ content.context = this.localCtx;
117
+ content.style.display = 'contents';
118
+ content.mcpToolMode = this.mcpToolMode;
119
+ if (source) {
120
+ fragmentSources.set(content, source);
121
+ }
122
+ let parsing = this.getAttribute('process-inner-html');
123
+ if (parsing !== null) {
124
+ content.setAttribute('process-inner-html', parsing);
125
+ }
126
+ content.append(this.ownerDocument.importNode(fragment, true));
127
+ this.unmount();
128
+ this.#content = content;
129
+ this.append(content);
130
+ }
131
+
132
+ unmount() {
133
+ let content = this.#content;
134
+ this.#content = undefined;
135
+ if (content) {
136
+ content.remove();
137
+ disposeContent(content);
138
+ }
139
+ }
140
+
141
+ async load() {
142
+ this.cancel();
143
+ if (!this.isConnected || globalThis.__SYMBIOTE_SSR) {
144
+ return;
145
+ }
146
+ let src = this.getAttribute('src')?.trim();
147
+ if (!src) {
148
+ return;
149
+ }
150
+ let controller = new AbortController();
151
+ this.#controller = controller;
152
+ this.status(true);
153
+ try {
154
+ let source = importSource(this, src);
155
+ let response = await fetchSource(source.url, {
156
+ ...requestLimits(this),
157
+ signal: controller.signal,
158
+ });
159
+ if (!/^text\/html(?:;|$)/i.test(response.contentType)) {
160
+ throw new SourceError('INVALID_CONTENT_TYPE', 'Expected an HTML fragment response.');
161
+ }
162
+ let fragment = fragmentTemplate(this.ownerDocument, response.text, source.url);
163
+ if (this.#controller !== controller || !this.isConnected) {
164
+ return;
165
+ }
166
+ this.mount(fragment, source);
167
+ await new Promise(queueMicrotask);
168
+ if (this.#controller === controller && this.isConnected) {
169
+ this.status(false);
170
+ this.dispatchEvent(new CustomEvent('load', {
171
+ detail: {
172
+ ok: true,
173
+ operation: 'load',
174
+ url: source.url,
175
+ },
176
+ }));
177
+ }
178
+ } catch (failure) {
179
+ if (this.#controller !== controller || controller.signal.aborted) {
180
+ return;
181
+ }
182
+ let error = {
183
+ operation: 'load',
184
+ code: failure instanceof SourceError ? failure.code : 'INVALID_TEMPLATE',
185
+ message: failure instanceof Error ? failure.message : 'The fragment could not be loaded.',
186
+ status: failure instanceof SourceError ? failure.status : null,
187
+ };
188
+ this.status(false, error);
189
+ this.dispatchEvent(new CustomEvent('error', {
190
+ detail: error,
191
+ }));
192
+ } finally {
193
+ if (this.#controller === controller) {
194
+ this.#controller = undefined;
195
+ }
196
+ }
197
+ }
198
+
199
+ disconnectedCallback() {
200
+ this.#active = false;
201
+ this.cancel();
202
+ super.disconnectedCallback();
203
+ }
204
+
205
+ destroyCallback() {
206
+ this.unmount();
207
+ for (let key of [
208
+ 'loading',
209
+ 'loadError',
210
+ 'loadErrorMessage',
211
+ ]) {
212
+ this.localCtx.delete('+' + key);
213
+ }
214
+ super.destroyCallback();
215
+ }
216
+ }
217
+
218
+ export default SymImport;