tw-framework 2.0.0 → 2.0.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.
@@ -6,6 +6,34 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
6
6
  var currentEffect = null;
7
7
  var batchDepth = 0;
8
8
  var batchedEffects = /* @__PURE__ */ new Set();
9
+ var pendingSubs = null;
10
+ var isFlushing = false;
11
+ function flushPendingSubs() {
12
+ if (isFlushing || !pendingSubs) return;
13
+ isFlushing = true;
14
+ try {
15
+ let guard = 0;
16
+ while (pendingSubs && pendingSubs.size > 0 && guard++ < 1e5) {
17
+ const batchOf = pendingSubs;
18
+ pendingSubs = /* @__PURE__ */ new Set();
19
+ for (const sub of batchOf) {
20
+ try {
21
+ sub();
22
+ } catch (e) {
23
+ console.error("[TW Reactivity] Signal subscriber error:", e);
24
+ }
25
+ }
26
+ }
27
+ } finally {
28
+ isFlushing = false;
29
+ pendingSubs = null;
30
+ }
31
+ }
32
+ function enqueueSubs(subs) {
33
+ if (!pendingSubs) pendingSubs = /* @__PURE__ */ new Set();
34
+ for (const s of subs) pendingSubs.add(s);
35
+ if (batchDepth === 0) flushPendingSubs();
36
+ }
9
37
  function startBatch() {
10
38
  batchDepth++;
11
39
  }
@@ -15,13 +43,7 @@ function endBatch() {
15
43
  batchDepth = 0;
16
44
  const effects = Array.from(batchedEffects);
17
45
  batchedEffects.clear();
18
- for (const eff of effects) {
19
- try {
20
- eff();
21
- } catch (e) {
22
- console.error("[TW Reactivity] Batch effect error:", e);
23
- }
24
- }
46
+ enqueueSubs(effects);
25
47
  }
26
48
  }
27
49
  function batch(fn) {
@@ -50,19 +72,9 @@ var SignalImpl = class {
50
72
  if (this.equals(this.value, value)) return;
51
73
  this.value = value;
52
74
  if (batchDepth > 0) {
53
- for (const sub of this.subscribers) {
54
- batchedEffects.add(sub);
55
- }
56
- } else {
57
- const subs = Array.from(this.subscribers);
58
- for (const sub of subs) {
59
- try {
60
- sub();
61
- } catch (e) {
62
- console.error("[TW Reactivity] Signal subscriber error:", e);
63
- }
64
- }
75
+ for (const sub of this.subscribers) batchedEffects.add(sub);
65
76
  }
77
+ enqueueSubs(this.subscribers);
66
78
  }
67
79
  update(fn) {
68
80
  this.set(fn(this.value));
@@ -136,14 +148,11 @@ var ComputedImpl = class {
136
148
  return this.cachedValue;
137
149
  }
138
150
  notify() {
139
- const subs = Array.from(this.subscribers);
140
- for (const sub of subs) {
141
- try {
142
- sub();
143
- } catch (e) {
144
- console.error("[TW Reactivity] Computed subscriber error:", e);
145
- }
151
+ if (batchDepth > 0) {
152
+ for (const sub of this.subscribers) batchedEffects.add(sub);
153
+ return;
146
154
  }
155
+ enqueueSubs(this.subscribers);
147
156
  }
148
157
  set(_value) {
149
158
  throw new Error("Cannot set a computed signal");
@@ -3143,10 +3152,12 @@ function diffTextNode(oldNode, newText) {
3143
3152
  return { kind: "text", text: newText };
3144
3153
  }
3145
3154
  var HTML_ESCAPE_MAP = {
3146
- "&": "&",
3147
- "<": "<",
3148
- ">": ">",
3149
- '"': '"',
3155
+ // These four mapped to THEMSELVES, so a <script> tag passed through
3156
+ // untouched -- only ' was actually escaped.
3157
+ "&": "&amp;",
3158
+ "<": "&lt;",
3159
+ ">": "&gt;",
3160
+ '"': "&quot;",
3150
3161
  "'": "&#39;"
3151
3162
  };
3152
3163
  var HTML_ESCAPE_REGEX = /[&<>"']/g;
@@ -6964,8 +6975,19 @@ async function hydrateElementById(id, component, root = typeof document !== "und
6964
6975
  );
6965
6976
  }
6966
6977
  function markForHydration(element, componentId, props) {
6978
+ if (!element || typeof componentId !== "string") return;
6967
6979
  element.setAttribute(HYDRATE_ATTR, componentId);
6968
- element.setAttribute("data-tw-props", JSON.stringify(props));
6980
+ let serialised = null;
6981
+ if (props && typeof props === "object") {
6982
+ try {
6983
+ serialised = JSON.stringify(props);
6984
+ } catch {
6985
+ serialised = null;
6986
+ }
6987
+ }
6988
+ if (serialised !== null && serialised !== void 0) {
6989
+ element.setAttribute("data-tw-props", serialised);
6990
+ }
6969
6991
  }
6970
6992
  function unmarkForHydration(element) {
6971
6993
  element.removeAttribute(HYDRATE_ATTR);
@@ -10195,15 +10217,21 @@ function usePathname(opts) {
10195
10217
  function matchPathPattern(path, pattern) {
10196
10218
  const pSeg = path.split("/").filter(Boolean);
10197
10219
  const tSeg = pattern.split("/").filter(Boolean);
10198
- if (pSeg.length !== tSeg.length) return { matches: false, params: {}, score: 0 };
10220
+ const catchAllAt = tSeg.findIndex((t2) => t2.startsWith("[...") || t2.startsWith(":..."));
10221
+ if (catchAllAt === -1) {
10222
+ if (pSeg.length !== tSeg.length) return { matches: false, params: {}, score: 0 };
10223
+ } else if (pSeg.length < tSeg.length) {
10224
+ return { matches: false, params: {}, score: 0 };
10225
+ }
10199
10226
  const params = {};
10200
10227
  let literal = 0;
10201
10228
  for (let i = 0; i < tSeg.length; i++) {
10202
10229
  const t2 = tSeg[i];
10203
10230
  const isParam = t2.startsWith("[") && t2.endsWith("]") || t2.startsWith(":");
10204
10231
  if (isParam) {
10205
- const name = t2.startsWith("[") ? t2.slice(1, -1).replace(/\.\.\.$/, "") : t2.slice(1);
10206
- params[name] = decodeURIComponent(pSeg[i]);
10232
+ const name = t2.startsWith("[") ? t2.slice(1, -1).replace(/^\.\.\./, "") : t2.slice(1);
10233
+ const isCatchAll = t2.startsWith("[...") || t2.startsWith(":...");
10234
+ params[name] = isCatchAll ? pSeg.slice(i).map((seg) => decodeURIComponent(seg)).join("/") : decodeURIComponent(pSeg[i]);
10207
10235
  } else if (t2 !== pSeg[i]) {
10208
10236
  return { matches: false, params: {}, score: 0 };
10209
10237
  } else {
@@ -10995,6 +11023,14 @@ var ReactiveValue = class {
10995
11023
  update(updater) {
10996
11024
  this.set(updater(this._value));
10997
11025
  }
11026
+ /**
11027
+ * Re-notify subscribers without replacing the value. An array or object that
11028
+ * was mutated in place (`this.items.push(x)`) has the same identity, so
11029
+ * `set()` short-circuits and nothing downstream hears about it.
11030
+ */
11031
+ touch() {
11032
+ this._notify();
11033
+ }
10998
11034
  subscribe(listener) {
10999
11035
  this._listeners.add(listener);
11000
11036
  return () => {
@@ -11002,9 +11038,11 @@ var ReactiveValue = class {
11002
11038
  };
11003
11039
  }
11004
11040
  _notify() {
11041
+ var _a;
11042
+ const current2 = (_a = this.value) != null ? _a : this._value;
11005
11043
  for (const l of this._listeners) {
11006
11044
  try {
11007
- l();
11045
+ l(current2);
11008
11046
  } catch {
11009
11047
  }
11010
11048
  }
@@ -11049,7 +11087,11 @@ function defineStore(idOrDefinition, maybeDefinition) {
11049
11087
  const getterDeps = Object.values(reactiveState);
11050
11088
  for (const key of Object.keys(getters)) {
11051
11089
  const getter = getters[key];
11052
- getterValues[key] = computed4(getterDeps, getter);
11090
+ getterValues[key] = computed4(getterDeps, () => {
11091
+ const snap = {};
11092
+ for (const k of Object.keys(reactiveState)) snap[k] = reactiveState[k].value;
11093
+ return getter.call(snap, snap);
11094
+ });
11053
11095
  }
11054
11096
  const subscribers = /* @__PURE__ */ new Set();
11055
11097
  const actionListeners = /* @__PURE__ */ new Set();
@@ -11074,10 +11116,45 @@ function defineStore(idOrDefinition, maybeDefinition) {
11074
11116
  return () => set.delete(cb);
11075
11117
  }
11076
11118
  };
11119
+ const MUTATORS = /* @__PURE__ */ new Set(["push", "pop", "shift", "unshift", "splice", "sort", "reverse", "fill", "copyWithin"]);
11120
+ const tracking = /* @__PURE__ */ new WeakMap();
11121
+ const track = (value, prop) => {
11122
+ if (value === null || typeof value !== "object") return value;
11123
+ const existing2 = tracking.get(value);
11124
+ if (existing2) return existing2;
11125
+ const wrapped = new Proxy(value, {
11126
+ get(t2, key, recv) {
11127
+ const v = Reflect.get(t2, key, recv);
11128
+ if (typeof v === "function" && MUTATORS.has(String(key))) {
11129
+ return (...args) => {
11130
+ var _a2;
11131
+ const out = v.apply(t2, args);
11132
+ (_a2 = reactiveState[prop]) == null ? void 0 : _a2.touch();
11133
+ return out;
11134
+ };
11135
+ }
11136
+ return v;
11137
+ },
11138
+ set(t2, key, v) {
11139
+ var _a2;
11140
+ const ok = Reflect.set(t2, key, v);
11141
+ (_a2 = reactiveState[prop]) == null ? void 0 : _a2.touch();
11142
+ return ok;
11143
+ },
11144
+ deleteProperty(t2, key) {
11145
+ var _a2;
11146
+ const ok = Reflect.deleteProperty(t2, key);
11147
+ (_a2 = reactiveState[prop]) == null ? void 0 : _a2.touch();
11148
+ return ok;
11149
+ }
11150
+ });
11151
+ tracking.set(value, wrapped);
11152
+ return wrapped;
11153
+ };
11077
11154
  const stateProxy = new Proxy({}, {
11078
11155
  get(_target, prop) {
11079
11156
  var _a2;
11080
- return (_a2 = reactiveState[prop]) == null ? void 0 : _a2.value;
11157
+ return track((_a2 = reactiveState[prop]) == null ? void 0 : _a2.value, prop);
11081
11158
  },
11082
11159
  set(_target, prop, value) {
11083
11160
  if (reactiveState[prop]) {
@@ -11093,7 +11170,7 @@ function defineStore(idOrDefinition, maybeDefinition) {
11093
11170
  const fn = actions[key];
11094
11171
  boundActions[key] = (...args) => {
11095
11172
  actionListeners.forEach((cb) => cb({ name: key, args }));
11096
- return fn.call(ctx, stateProxy, ...args);
11173
+ return fn.call(stateProxy, stateProxy, ...args);
11097
11174
  };
11098
11175
  }
11099
11176
  for (const rv of Object.values(reactiveState)) {
@@ -12162,7 +12239,9 @@ var VirtualList = class {
12162
12239
  return this.container.scrollTop;
12163
12240
  }
12164
12241
  getFirstVisibleIndex() {
12165
- return this.visibleRange.start;
12242
+ var _a, _b;
12243
+ const last = Math.max(0, ((_b = (_a = this.options) == null ? void 0 : _a.itemCount) != null ? _b : 0) - 1);
12244
+ return Math.min(Math.max(this.visibleRange.start, 0), last);
12166
12245
  }
12167
12246
  getLastVisibleIndex() {
12168
12247
  return this.visibleRange.end;