@cossackframework/renderer 0.7.4 → 0.7.6

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.
package/README.md CHANGED
@@ -4,7 +4,9 @@ Cossack Renderer is a Lit-compatible rendering engine designed for **Light DOM**
4
4
 
5
5
  ## Installation
6
6
 
7
- This package is intended to use with the Cossack Framework via `create-cossack-app`, but can also be used standalone in any project that needs a lightweight rendering solution.
7
+ This package is included by projects created with `cossack create`, but can
8
+ also be used standalone in any project that needs a lightweight rendering
9
+ solution.
8
10
 
9
11
  ```bash
10
12
  pnpm add @cossackframework/renderer
@@ -0,0 +1,4 @@
1
+ //#region src/component.ts
2
+ var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT";
3
+ //#endregion
4
+ export { e as isComponentResult };
@@ -0,0 +1,9 @@
1
+ //#region src/context.ts
2
+ function e(e) {
3
+ return {
4
+ id: Math.random().toString(36).slice(2),
5
+ defaultValue: e
6
+ };
7
+ }
8
+ //#endregion
9
+ export { e as createContext };
@@ -0,0 +1,166 @@
1
+ import { getFinalizedStyles as e } from "./css.js";
2
+ import { TemplateResult as t, html as n, hydrate as r, isTemplateResult as i, render as a } from "./cossack-html.js";
3
+ //#region src/cossack-element.ts
4
+ var o = [], s = (e) => o.push(e), c = () => o.pop(), l = /* @__PURE__ */ new WeakMap(), u = (e) => {
5
+ let n = [e];
6
+ return n.raw = n, new t(n, []);
7
+ }, d = (e, t) => {
8
+ let n = l.get(e);
9
+ if (!n) {
10
+ let r = t.cssText.replace(/<\/style/gi, "<\\/style");
11
+ n = u(`<style data-cossack-style="${t.scopeId}">${r}</style>`), l.set(e, n);
12
+ }
13
+ return n;
14
+ }, f = (e, t) => n`${e}${t}`, p = class {
15
+ static properties = {};
16
+ static _isCossackElement = !0;
17
+ static components = {};
18
+ static styles;
19
+ children = null;
20
+ props = {};
21
+ __parent = null;
22
+ _id = "";
23
+ _childCounter = 0;
24
+ __providedContexts = /* @__PURE__ */ new Map();
25
+ __properties = /* @__PURE__ */ new Map();
26
+ __updatePromise = null;
27
+ __changedProperties = /* @__PURE__ */ new Map();
28
+ __renderListeners = /* @__PURE__ */ new Set();
29
+ __controllers = /* @__PURE__ */ new Set();
30
+ __hasConnected = !1;
31
+ __eventListeners = /* @__PURE__ */ new Map();
32
+ constructor() {
33
+ this.initialize();
34
+ }
35
+ static get currentRenderingInstance() {
36
+ return o.length > 0 ? o[o.length - 1] : null;
37
+ }
38
+ resetRenderState() {
39
+ this._childCounter = 0;
40
+ }
41
+ addController(e) {
42
+ this.__controllers.add(e), this.__hasConnected && e.hostConnected && e.hostConnected();
43
+ }
44
+ removeController(e) {
45
+ this.__controllers.delete(e);
46
+ }
47
+ get updateComplete() {
48
+ return this.__updatePromise || Promise.resolve(!0);
49
+ }
50
+ initialize() {
51
+ let e = this.constructor.properties;
52
+ if (e) for (let [t, n] of Object.entries(e)) this.createProperty(t, n);
53
+ }
54
+ createProperty(e, t) {
55
+ Object.prototype.hasOwnProperty.call(this, e) && this.__properties.set(e, this[e]), Object.defineProperty(this, e, {
56
+ get() {
57
+ return this.__properties.get(e);
58
+ },
59
+ set(t) {
60
+ let n = this.__properties.get(e);
61
+ n !== t && (this.__properties.set(e, t), this.requestUpdate(e, n));
62
+ },
63
+ configurable: !0,
64
+ enumerable: !0
65
+ });
66
+ }
67
+ provide(e, t) {
68
+ this.__providedContexts.set(e, t);
69
+ }
70
+ consume(e) {
71
+ let t = this.__parent;
72
+ for (; t;) {
73
+ if (t.__providedContexts.has(e)) return t.__providedContexts.get(e);
74
+ t = t.__parent;
75
+ }
76
+ return e.defaultValue;
77
+ }
78
+ requestUpdate(e, t) {
79
+ return e !== void 0 && (this.__changedProperties.has(e) || this.__changedProperties.set(e, t)), this.__updatePromise ||= this.performUpdate(), this.__updatePromise;
80
+ }
81
+ async performUpdate() {
82
+ await Promise.resolve();
83
+ let e = !1;
84
+ try {
85
+ if (e = this.shouldUpdate(this.__changedProperties), e) {
86
+ this.__controllers.forEach((e) => e.hostUpdate && e.hostUpdate()), this.willUpdate(this.__changedProperties), this.resetRenderState(), s(this);
87
+ let e = this._finalizeRenderOutput(this.render());
88
+ this.__notifyListeners(e), c(), this.updated(this.__changedProperties), this.__controllers.forEach((e) => e.hostUpdated && e.hostUpdated());
89
+ }
90
+ } catch (e) {
91
+ console.error("Error during update:", e), o[o.length - 1] === this && c();
92
+ }
93
+ return this.__changedProperties = /* @__PURE__ */ new Map(), this.__updatePromise = null, e;
94
+ }
95
+ __notifyListeners(e) {
96
+ this.__renderListeners.forEach((t) => t(e));
97
+ }
98
+ addRenderListener(e) {
99
+ this.__renderListeners.add(e);
100
+ }
101
+ removeRenderListener(e) {
102
+ this.__renderListeners.delete(e);
103
+ }
104
+ _getStyleScopeId() {
105
+ return e(this.constructor)?.scopeId;
106
+ }
107
+ _finalizeRenderOutput(t) {
108
+ let n = e(this.constructor);
109
+ return this._claimTemplateOwnership(t, n?.scopeId), n ? f(d(this.constructor, n), t) : t;
110
+ }
111
+ _claimTemplateOwnership(e, t) {
112
+ if (!(!e || typeof e != "object")) {
113
+ if (e._type === "COMPONENT") {
114
+ let n = e;
115
+ n.parent = this, this._claimTemplateOwnership(n.children, t);
116
+ return;
117
+ }
118
+ if (i(e)) {
119
+ if (e.__cossackOwner && e.__cossackOwner !== this) return;
120
+ e.__cossackOwner = this, e.__cossackScope = t;
121
+ for (let n of e.values) this._claimTemplateOwnership(n, t);
122
+ return;
123
+ }
124
+ if (Array.isArray(e)) for (let n of e) this._claimTemplateOwnership(n, t);
125
+ }
126
+ }
127
+ mount(e, t = !1) {
128
+ let o = t;
129
+ this.addRenderListener((t) => {
130
+ if (t) {
131
+ let s = i(t) ? t : n`${t}`;
132
+ o ? (o = !1, r(s, e)) : a(s, e);
133
+ }
134
+ }), this.requestUpdate();
135
+ }
136
+ addEventListener(e, t, n) {
137
+ t && (this.__eventListeners.has(e) || this.__eventListeners.set(e, /* @__PURE__ */ new Set()), this.__eventListeners.get(e).add(t));
138
+ }
139
+ removeEventListener(e, t, n) {
140
+ if (!t) return;
141
+ let r = this.__eventListeners.get(e);
142
+ r && r.delete(t);
143
+ }
144
+ dispatchEvent(e) {
145
+ let t = this.__eventListeners.get(e.type);
146
+ return t && t.forEach((t) => {
147
+ typeof t == "function" ? t.call(this, e) : t.handleEvent(e);
148
+ }), !e.defaultPrevented;
149
+ }
150
+ shouldUpdate(e) {
151
+ return !0;
152
+ }
153
+ willUpdate(e) {}
154
+ render() {
155
+ return null;
156
+ }
157
+ updated(e) {}
158
+ connectedCallback() {
159
+ this.__hasConnected = !0, this.__controllers.forEach((e) => e.hostConnected && e.hostConnected());
160
+ }
161
+ disconnectedCallback() {
162
+ this.__hasConnected = !1, this.__controllers.forEach((e) => e.hostDisconnected && e.hostDisconnected());
163
+ }
164
+ };
165
+ //#endregion
166
+ export { p as CossackElement, o as instanceStack, c as popCurrentInstance, s as pushCurrentInstance };