wrec 0.26.0 → 0.27.1

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
@@ -152,8 +152,13 @@ to one of those strings.
152
152
  ## SSR Support
153
153
 
154
154
  Server-side rendering (SSR) is supported.
155
- This requires importing from "wrec/ssr" instead of "wrec".
156
- "wrec/ssr" should only be imported by server code.
155
+ Components can always import from "wrec".
156
+ When that import is resolved in a browser build,
157
+ it gets the normal client bundle.
158
+ When it is resolved in Node for SSR, it gets the SSR-enabled bundle.
159
+ This allows the same component source file to work in both environments.
160
+
161
+ "wrec/ssr" can still be imported explicitly by server code if desired.
157
162
  That bundle is much larger because it bundles the node-html-parser package.
158
163
  However, the size increase is not likely an issue
159
164
  because it is used on the server-side rather than downloaded to web browsers.
@@ -28,7 +28,7 @@ var r = typeof window < "u" && window.document !== void 0, i = class extends Err
28
28
  static #e = /* @__PURE__ */ new Map();
29
29
  static {
30
30
  r && window.addEventListener("beforeunload", () => {
31
- for (let [e, t] of this.#e.entries()) if (t.#i) {
31
+ for (let [e, t] of this.#e.entries()) if (t.#a) {
32
32
  let r = n(t);
33
33
  sessionStorage.setItem("wrec-state-" + e, JSON.stringify(r));
34
34
  }
@@ -37,15 +37,16 @@ var r = typeof window < "u" && window.document !== void 0, i = class extends Err
37
37
  static get(e) {
38
38
  return this.#e.get(e);
39
39
  }
40
- #t = Symbol("objectId");
41
- #n = [];
42
- #r;
40
+ #t = [];
41
+ #n = Symbol("objectId");
42
+ #r = [];
43
43
  #i;
44
44
  #a;
45
+ #o;
45
46
  constructor(n, a, o) {
46
47
  if (!n) throw new i("name cannot be empty");
47
48
  if (e.#e.has(n)) throw new i(`WrecState with name "${n}" already exists`);
48
- if (this.#r = n, this.#i = a, this.#a = t({}, this.#o.bind(this)), a && r) {
49
+ if (this.#i = n, this.#a = a, this.#o = t({}, this.#s.bind(this)), a && r) {
49
50
  let e = sessionStorage.getItem("wrec-state-" + n), t = e ? JSON.parse(e) : void 0;
50
51
  t && (o = t);
51
52
  }
@@ -53,36 +54,44 @@ var r = typeof window < "u" && window.document !== void 0, i = class extends Err
53
54
  e.#e.set(n, this);
54
55
  }
55
56
  addListener(e, t = {}) {
56
- let n = this.#n.find((t) => t.listenerRef.deref() === e);
57
+ let n = this.#r.find((t) => t.listenerRef.deref() === e);
57
58
  if (n) {
58
59
  let { propertyMap: e } = n;
59
60
  for (let [n, r] of Object.entries(t)) e[n] = r;
60
- } else this.#n.push({
61
+ } else this.#r.push({
61
62
  listenerRef: new WeakRef(e),
62
63
  propertyMap: t
63
64
  });
64
65
  }
66
+ addChangeCallback(e, t = []) {
67
+ if (this.#t.some((t) => t.callback === e)) throw new i("WrecState addChangedCallback was passed a callback that was already added");
68
+ this.#t.push({
69
+ callback: e,
70
+ statePaths: t
71
+ });
72
+ }
65
73
  addProperty(e, t) {
66
74
  Object.defineProperty(this, e, {
67
75
  enumerable: !0,
68
76
  get() {
69
- return this.#a[e];
77
+ return this.#o[e];
70
78
  },
71
79
  set(t) {
72
- this.#a[e] = t;
80
+ this.#o[e] = t;
73
81
  }
74
- }), this.#a[e] = t;
82
+ }), this.#o[e] = t;
75
83
  }
76
84
  get id() {
77
- return this.#t;
85
+ return this.#n;
78
86
  }
79
87
  log() {
80
- console.log("WrecState:", this.#r);
81
- for (let [e, t] of Object.entries(this.#a)) console.log(` ${e} = ${JSON.stringify(t)}`);
88
+ console.log("WrecState:", this.#i);
89
+ for (let [e, t] of Object.entries(this.#o)) console.log(` ${e} = ${JSON.stringify(t)}`);
82
90
  }
83
- #o(e, t, n) {
91
+ #s(e, t, n) {
92
+ for (let { callback: r, statePaths: i } of this.#t) (i.length === 0 || i.includes(e)) && r(e, n, t, this);
84
93
  let i = /* @__PURE__ */ new Set();
85
- for (let a of this.#n) {
94
+ for (let a of this.#r) {
86
95
  let o = a.listenerRef.deref();
87
96
  if (!o) i.add(a);
88
97
  else if (r && o instanceof HTMLElement && !o.isConnected) i.add(a);
@@ -91,10 +100,13 @@ var r = typeof window < "u" && window.document !== void 0, i = class extends Err
91
100
  (i.length === 0 || i.includes(e)) && o.changed(e, r[e], n, t, this);
92
101
  }
93
102
  }
94
- this.#n = this.#n.filter((e) => !i.has(e));
103
+ this.#r = this.#r.filter((e) => !i.has(e));
104
+ }
105
+ removeChangeCallback(e) {
106
+ this.#t = this.#t.filter((t) => t.callback !== e);
95
107
  }
96
108
  removeListener(e) {
97
- this.#n = this.#n.filter((t) => t.listenerRef.deref() !== e);
109
+ this.#r = this.#r.filter((t) => t.listenerRef.deref() !== e);
98
110
  }
99
111
  };
100
112
  r && process.env.NODE_ENV === "development" && (window.WrecState = a);
@@ -623,7 +635,7 @@ var U = class e extends p {
623
635
  !this.#a || !N(t) || (this.#a.set(e, t), this.#s?.setFormValue(this.#a));
624
636
  }
625
637
  static ssr(e = {}) {
626
- throw new h("Import Wrec from \"wrec/ssr\" to use the ssr method.");
638
+ throw new h("SSR is not available in the browser build.");
627
639
  }
628
640
  #j(e, t, n) {
629
641
  let r = e instanceof HTMLElement ? e.localName : "CSS rule";
@@ -1,4 +1,4 @@
1
- import { a as e, i as t, n, r, t as i } from "./wrec-DjS3Bm3H.js";
1
+ import { a as e, i as t, n, r, t as i } from "./wrec-TaEXMJIq.js";
2
2
  //#region \0rolldown/runtime.js
3
3
  var a = Object.defineProperty, o = Object.getOwnPropertyDescriptor, s = Object.getOwnPropertyNames, c = Object.prototype.hasOwnProperty, l = (e, t) => () => (e && (t = e(e = 0)), t), u = (e, t) => () => (t || e((t = { exports: {} }).exports, t), t.exports), d = (e, t) => {
4
4
  let n = {};
package/dist/wrec.es.js CHANGED
@@ -1,2 +1,2 @@
1
- import { a as e, i as t, n, r, t as i } from "./wrec-DjS3Bm3H.js";
1
+ import { a as e, i as t, n, r, t as i } from "./wrec-TaEXMJIq.js";
2
2
  export { i as Wrec, e as WrecState, n as createElement, r as css, t as html };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "wrec",
3
3
  "description": "a small library that greatly simplifies building web components",
4
4
  "author": "R. Mark Volkmann",
5
- "version": "0.26.0",
5
+ "version": "0.27.1",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -19,7 +19,9 @@
19
19
  "exports": {
20
20
  ".": {
21
21
  "types": "./dist/wrec.d.ts",
22
- "import": "./dist/wrec.es.js"
22
+ "browser": "./dist/wrec.es.js",
23
+ "node": "./dist/wrec-ssr.es.js",
24
+ "default": "./dist/wrec.es.js"
23
25
  },
24
26
  "./ssr": {
25
27
  "types": "./dist/wrec-ssr.d.ts",