ddd-react 1.9.1 → 1.10.0

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.
@@ -4,7 +4,9 @@ export declare abstract class Component<P = {}, S = {}, C = null> {
4
4
  vdom: VDOMNode | null;
5
5
  private hostEl;
6
6
  parent: Component | null;
7
+ private error;
7
8
  static defaultProps: any;
9
+ static getDerivedStateFromError?(error: Error): any;
8
10
  props: P & WithChildrenProps;
9
11
  state: S;
10
12
  context: C;
@@ -27,6 +29,7 @@ export declare abstract class Component<P = {}, S = {}, C = null> {
27
29
  didUpdate(prevProps: P, prevState: S): void | Promise<void>;
28
30
  willUnmount(): void | Promise<void>;
29
31
  didUnmount(): void | Promise<void>;
32
+ didCatch(error: Error, errorInfo: any): void | Promise<void>;
30
33
  shouldComponentUpdate(prevProps: P, nextProps: P): boolean;
31
34
  abstract render(): VDOMNode;
32
35
  get elements(): HTMLElement[];
@@ -40,4 +43,6 @@ export declare abstract class Component<P = {}, S = {}, C = null> {
40
43
  private updateContext;
41
44
  private subscribeToProvider;
42
45
  getDefaultProps(): P;
46
+ private handleError;
47
+ private getComponentStack;
43
48
  }
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { D as c, e as v, a as x, A as E, b as U, h as F, c as I } from "./h-BbaMkkC7.mjs";
2
- import { d as Ot, f as mt } from "./h-BbaMkkC7.mjs";
3
- function g(e, t) {
1
+ import { D as c, e as b, a as x, A as E, b as U, h as F, c as I } from "./h-BbaMkkC7.mjs";
2
+ import { d as mt, f as Ot } from "./h-BbaMkkC7.mjs";
3
+ function P(e, t) {
4
4
  if (e === null || t === null || e === void 0 || t === void 0)
5
5
  return e === t;
6
6
  if (typeof e != typeof t)
@@ -14,7 +14,7 @@ function g(e, t) {
14
14
  if (e.length !== t.length)
15
15
  return !1;
16
16
  for (let n = 0; n < e.length; ++n)
17
- if (!g(e[n], t[n]))
17
+ if (!P(e[n], t[n]))
18
18
  return !1;
19
19
  return !0;
20
20
  }
@@ -32,7 +32,7 @@ function g(e, t) {
32
32
  return !1;
33
33
  for (let n = 0; n < r.length; ++n) {
34
34
  const s = r[n];
35
- if (!(s === "_owner" && e.$$typeof) && !g(e[s], t[s]))
35
+ if (!(s === "_owner" && e.$$typeof) && !P(e[s], t[s]))
36
36
  return !1;
37
37
  }
38
38
  return !0;
@@ -62,7 +62,7 @@ function q(e = {}, t) {
62
62
  t.removeEventListener(r, n);
63
63
  });
64
64
  }
65
- function h(e) {
65
+ function p(e) {
66
66
  const { type: t } = e;
67
67
  switch (t) {
68
68
  case c.TEXT: {
@@ -96,15 +96,15 @@ function G(e) {
96
96
  }
97
97
  function _(e) {
98
98
  const { el: t, children: r, listeners: n, props: s } = e;
99
- s?.ref && typeof s.ref == "object" && "current" in s.ref && (s.ref.current = null), t && t.remove(), r && r.forEach(h), n && t && (q(n, t), delete e.listeners);
99
+ s?.ref && typeof s.ref == "object" && "current" in s.ref && (s.ref.current = null), t && t.remove(), r && r.forEach(p), n && t && (q(n, t), delete e.listeners);
100
100
  }
101
101
  function B(e) {
102
102
  const { children: t } = e;
103
- t && t.forEach(h);
103
+ t && t.forEach(p);
104
104
  }
105
105
  function X(e) {
106
106
  const { children: t } = e;
107
- t && t.forEach(h);
107
+ t && t.forEach(p);
108
108
  }
109
109
  function C(e, t, r) {
110
110
  e.style[t] = r.toString();
@@ -128,18 +128,18 @@ function W(e, t) {
128
128
  e.className = "", typeof t == "string" ? e.className = t : Array.isArray(t) && e.classList.add(...t);
129
129
  }
130
130
  function w(e, t, r) {
131
- r == null ? L(e, t) : e.setAttribute(t, String(r));
131
+ r == null ? D(e, t) : e.setAttribute(t, String(r));
132
132
  }
133
- function L(e, t) {
133
+ function D(e, t) {
134
134
  e[t] = null, e.removeAttribute(t);
135
135
  }
136
- let P = !1;
136
+ let m = !1;
137
137
  const O = [];
138
138
  function d(e) {
139
139
  O.push(e), Y();
140
140
  }
141
141
  function Y() {
142
- P || (P = !0, queueMicrotask(H));
142
+ m || (m = !0, queueMicrotask(H));
143
143
  }
144
144
  function H() {
145
145
  for (; O.length > 0; ) {
@@ -159,13 +159,13 @@ function H() {
159
159
  }
160
160
  );
161
161
  }
162
- P = !1;
162
+ m = !1;
163
163
  }
164
164
  function A(e) {
165
165
  const { on: t = {}, ...r } = e.props;
166
166
  return delete r.key, { props: r, events: t };
167
167
  }
168
- function p(e, t, r = null, n = null) {
168
+ function h(e, t, r = null, n = null) {
169
169
  switch (e.type) {
170
170
  case c.TEXT: {
171
171
  z(e, t, r);
@@ -195,19 +195,19 @@ function p(e, t, r = null, n = null) {
195
195
  }
196
196
  function z(e, t, r) {
197
197
  const { value: n } = e, s = document.createTextNode(n);
198
- e.el = s, j(s, t, r);
198
+ e.el = s, L(s, t, r);
199
199
  }
200
200
  function Q(e, t, r, n) {
201
201
  const { children: s } = e;
202
202
  e.el = t, s?.forEach((i, o) => {
203
- p(i, t, r != null ? r + o : null, n);
203
+ h(i, t, r != null ? r + o : null, n);
204
204
  });
205
205
  }
206
206
  function Z(e, t, r, n) {
207
207
  const { tag: s, children: i } = e, f = s === "svg" || t instanceof SVGElement ? document.createElementNS("http://www.w3.org/2000/svg", s) : document.createElement(s);
208
- V(f, e, n), e.el = f, i?.forEach((l) => {
209
- p(l, f, null, n);
210
- }), j(f, t, r);
208
+ V(f, e, n), e.el = f, i?.forEach((a) => {
209
+ h(a, f, null, n);
210
+ }), L(f, t, r);
211
211
  }
212
212
  function V(e, t, r) {
213
213
  const { props: n, events: s } = A(t);
@@ -217,7 +217,7 @@ function tt(e, t, r, n) {
217
217
  const s = e.tag, { props: i } = A(e), o = new s(i, n);
218
218
  o.mount(t, r), e.component = o, e.el = o.firstElement || null;
219
219
  }
220
- function j(e, t, r) {
220
+ function L(e, t, r) {
221
221
  if (r == null) {
222
222
  t.append(e);
223
223
  return;
@@ -230,10 +230,10 @@ function j(e, t, r) {
230
230
  function et(e, t) {
231
231
  const { children: r, container: n } = e;
232
232
  r?.forEach((s) => {
233
- p(s, n, null, t);
233
+ h(s, n, null, t);
234
234
  });
235
235
  }
236
- function D(e, t) {
236
+ function j(e, t) {
237
237
  if (e.type !== t.type)
238
238
  return !1;
239
239
  if (e.type === c.ELEMENT) {
@@ -272,10 +272,10 @@ function rt(e) {
272
272
  function N(e) {
273
273
  return rt(e.trim());
274
274
  }
275
- function m(e, t, r, n = null) {
276
- if (!D(e, t)) {
275
+ function v(e, t, r, n = null) {
276
+ if (!j(e, t)) {
277
277
  const s = nt(r, e.el);
278
- return h(e), p(t, r, s, n), t;
278
+ return p(e), h(t, r, s, n), t;
279
279
  }
280
280
  switch (t.el = e.el, t.type) {
281
281
  case c.TEXT:
@@ -285,11 +285,11 @@ function m(e, t, r, n = null) {
285
285
  break;
286
286
  }
287
287
  case c.COMPONENT: {
288
- lt(e, t);
288
+ at(e, t);
289
289
  break;
290
290
  }
291
291
  case c.PORTAL: {
292
- at(e, t, n);
292
+ lt(e, t, n);
293
293
  break;
294
294
  }
295
295
  }
@@ -311,20 +311,20 @@ function ot(e, t, r) {
311
311
  on: o,
312
312
  ...f
313
313
  } = e.props ?? {}, {
314
- class: l,
314
+ class: a,
315
315
  style: u,
316
- on: a,
316
+ on: l,
317
317
  ...y
318
- } = t.props ?? {}, { listeners: b } = e;
319
- it(n, f, y), ct(n, s, l), ut(
318
+ } = t.props ?? {}, { listeners: g } = e;
319
+ it(n, f, y), ct(n, s, a), ut(
320
320
  n,
321
321
  i,
322
322
  u
323
323
  ), t.listeners = ft(
324
324
  n,
325
- b,
325
+ g,
326
326
  o ?? {},
327
- a ?? {},
327
+ l ?? {},
328
328
  r
329
329
  );
330
330
  }
@@ -333,17 +333,17 @@ function it(e, t = {}, r = {}) {
333
333
  n !== s && (n && typeof n == "object" && "current" in n && (n.current = null), s && typeof s == "object" && "current" in s && (s.current = e));
334
334
  const i = { ...t }, o = { ...r };
335
335
  delete i.ref, delete o.ref;
336
- const { added: f, removed: l, updated: u } = T(i, o);
337
- for (const a of l)
338
- L(e, a);
339
- for (const a of f.concat(u))
340
- w(e, a, o[a]);
336
+ const { added: f, removed: a, updated: u } = T(i, o);
337
+ for (const l of a)
338
+ D(e, l);
339
+ for (const l of f.concat(u))
340
+ w(e, l, o[l]);
341
341
  }
342
342
  function ct(e, t, r) {
343
- const n = M(t), s = M(r), { added: i, removed: o } = U(n, s);
343
+ const n = k(t), s = k(r), { added: i, removed: o } = U(n, s);
344
344
  o.length > 0 && e.classList.remove(...o), i.length > 0 && e.classList.add(...i);
345
345
  }
346
- function M(e = "") {
346
+ function k(e = "") {
347
347
  return Array.isArray(e) ? e.filter(N) : String(e).split(/(\s+)/).filter(N);
348
348
  }
349
349
  function ut(e, t = {}, r = {}) {
@@ -356,62 +356,63 @@ function ut(e, t = {}, r = {}) {
356
356
  function ft(e, t = {}, r = {}, n = {}, s = null) {
357
357
  const { removed: i, added: o, updated: f } = T(r, n);
358
358
  for (const u of i.concat(f)) {
359
- const a = t?.[u];
360
- a && e.removeEventListener(u, a);
359
+ const l = t?.[u];
360
+ l && e.removeEventListener(u, l);
361
361
  }
362
- const l = {};
362
+ const a = {};
363
363
  for (const u of o.concat(f))
364
- l[u] = S(
364
+ a[u] = S(
365
365
  u,
366
366
  n[u],
367
367
  e,
368
368
  s
369
369
  );
370
- return l;
370
+ return a;
371
371
  }
372
372
  function R(e, t, r) {
373
- const n = v(e), s = v(t), i = e.el, o = x(n, s, D), f = r?.offset ?? 0;
374
- for (const l of o)
375
- if (l.op === E.NOOP) {
376
- const { originalIndex: u, index: a } = l;
377
- m(n[u], s[a], i, r);
373
+ const n = b(e), s = b(t), i = e.el, o = x(n, s, j), f = r?.offset ?? 0;
374
+ for (const a of o)
375
+ if (a.op === E.NOOP) {
376
+ const { originalIndex: u, index: l } = a;
377
+ v(n[u], s[l], i, r);
378
378
  }
379
- for (const l of o)
380
- switch (l.op) {
379
+ for (const a of o)
380
+ switch (a.op) {
381
381
  case E.MOVE: {
382
- const { from: u, index: a } = l, y = n[u].el, b = i.childNodes[a + f];
383
- y && (i.insertBefore(y, b), m(n[u], s[a], i, r));
382
+ const { from: u, index: l } = a, y = n[u].el, g = i.childNodes[l + f];
383
+ y && (i.insertBefore(y, g), v(n[u], s[l], i, r));
384
384
  break;
385
385
  }
386
386
  case E.REMOVE: {
387
- const { item: u } = l;
388
- h(u);
387
+ const { item: u } = a;
388
+ p(u);
389
389
  break;
390
390
  }
391
391
  case E.ADD: {
392
- const { index: u, item: a } = l;
393
- p(a, i, u + f, r);
392
+ const { index: u, item: l } = a;
393
+ h(l, i, u + f, r);
394
394
  break;
395
395
  }
396
396
  }
397
397
  }
398
- function lt(e, t) {
398
+ function at(e, t) {
399
399
  const { component: r } = e, { props: n } = A(t);
400
400
  r.updateProps(n), t.component = r, t.el = r.firstElement;
401
401
  }
402
- function at(e, t, r) {
402
+ function lt(e, t, r) {
403
403
  if (e.container !== t.container) {
404
- h(e), p(t, document.body, null, r);
404
+ p(e), h(t, document.body, null, r);
405
405
  return;
406
406
  }
407
407
  R(e, t, r);
408
408
  }
409
- const pt = (e) => e.isProvider, ht = (e) => e.isConsumer;
410
- class k {
409
+ const ht = (e) => e.isProvider, dt = (e) => e.isConsumer;
410
+ class M {
411
411
  isMounted = !1;
412
412
  vdom = null;
413
413
  hostEl = null;
414
414
  parent = null;
415
+ error = null;
415
416
  static defaultProps = {};
416
417
  props;
417
418
  state = {};
@@ -453,11 +454,14 @@ class k {
453
454
  didUnmount() {
454
455
  return Promise.resolve();
455
456
  }
457
+ didCatch(t, r) {
458
+ return console.error("Uncaught error:", t, r), this.error = t, Promise.resolve();
459
+ }
456
460
  shouldComponentUpdate(t, r) {
457
- return g(t, r);
461
+ return P(t, r);
458
462
  }
459
463
  get elements() {
460
- return this.vdom == null ? [] : this.vdom.type === c.FRAGMENT ? v(this.vdom).flatMap((t) => t.type === c.COMPONENT && t.component ? t.component.elements : t.el ? [t.el] : []) : this.vdom.el ? [this.vdom.el] : [];
464
+ return this.vdom == null ? [] : this.vdom.type === c.FRAGMENT ? b(this.vdom).flatMap((t) => t.type === c.COMPONENT && t.component ? t.component.elements : t.el ? [t.el] : []) : this.vdom.el ? [this.vdom.el] : [];
461
465
  }
462
466
  get firstElement() {
463
467
  return this.elements[0];
@@ -467,7 +471,7 @@ class k {
467
471
  }
468
472
  updateProps(t) {
469
473
  const n = { ...this.getDefaultProps(), ...this.props, ...t }, s = this.props;
470
- this.props = n, !this.shouldComponentUpdate(s, n) && (pt(this) && this.notify(), this.patch(s, this.state));
474
+ this.props = n, !this.shouldComponentUpdate(s, n) && (ht(this) && this.notify(), this.patch(s, this.state));
471
475
  }
472
476
  setState(t) {
473
477
  const r = this.state;
@@ -479,19 +483,28 @@ class k {
479
483
  mount(t, r = null) {
480
484
  if (this.isMounted)
481
485
  throw new Error("Component is already mounted");
482
- ht(this) && !this.subscribedProvider && this.subscribeToProvider(), this.updateContext(), this.vdom = this.render(), p(this.vdom, t, r, this), this.hostEl = t, this.isMounted = !0;
486
+ dt(this) && !this.subscribedProvider && this.subscribeToProvider(), this.updateContext();
487
+ try {
488
+ this.vdom = this.render(), h(this.vdom, t, r, this), this.hostEl = t, this.isMounted = !0;
489
+ } catch (n) {
490
+ this.handleError(n, "mount");
491
+ }
483
492
  }
484
493
  unmount() {
485
494
  this.isMounted && (d(() => this.willUnmount()), this.subscribedProvider && this.subscribedProvider.removeDependency({ consumer: this }), this.dependencies.forEach(({ consumer: t }) => {
486
495
  t.subscribedProvider = null;
487
- }), this.dependencies = [], this.vdom && h(this.vdom), d(() => this.didUnmount()), this.vdom = null, this.hostEl = null, this.isMounted = !1);
496
+ }), this.dependencies = [], this.vdom && p(this.vdom), d(() => this.didUnmount()), this.vdom = null, this.hostEl = null, this.isMounted = !1, this.error = null);
488
497
  }
489
498
  patch(t, r) {
490
- if (!this.isMounted || !this.hostEl || !this.vdom)
491
- return;
492
- d(() => this.willUpdate(this.props, this.state));
493
- const n = this.render();
494
- this.vdom = m(this.vdom, n, this.hostEl, this), d(() => this.didUpdate(t, r));
499
+ if (!(!this.isMounted || !this.hostEl || !this.vdom) && !this.error) {
500
+ d(() => this.willUpdate(this.props, this.state));
501
+ try {
502
+ const n = this.render();
503
+ this.vdom = v(this.vdom, n, this.hostEl, this), d(() => this.didUpdate(t, r));
504
+ } catch (n) {
505
+ this.handleError(n, "patch");
506
+ }
507
+ }
495
508
  }
496
509
  updateContext() {
497
510
  const t = Object.getPrototypeOf(this).constructor.contextType;
@@ -522,16 +535,42 @@ class k {
522
535
  getDefaultProps() {
523
536
  return this.constructor.defaultProps || {};
524
537
  }
538
+ handleError(t, r) {
539
+ const n = this.constructor;
540
+ if (n.getDerivedStateFromError) {
541
+ const s = n.getDerivedStateFromError(t);
542
+ this.state = { ...this.state, ...s };
543
+ }
544
+ d(() => {
545
+ if (this.didCatch(t, {
546
+ phase: r,
547
+ componentStack: this.getComponentStack()
548
+ }), this.hostEl && this.isMounted)
549
+ try {
550
+ const s = this.render();
551
+ s && v(this.vdom, s, this.hostEl, this);
552
+ } catch (s) {
553
+ console.error("Error during error recovery render:", s);
554
+ }
555
+ });
556
+ }
557
+ getComponentStack() {
558
+ const t = [this.constructor.name];
559
+ let r = this.parent;
560
+ for (; r; )
561
+ t.push(r.constructor.name), r = r.parent;
562
+ return t.reverse();
563
+ }
525
564
  }
526
565
  function yt(e) {
527
- class t extends k {
566
+ class t extends M {
528
567
  isProvider = !0;
529
568
  render() {
530
569
  let s = [];
531
570
  return Array.isArray(this.props.children) ? s = this.props.children : this.props.children ? s = [this.props.children] : s = [], F(s);
532
571
  }
533
572
  }
534
- class r extends k {
573
+ class r extends M {
535
574
  static contextType = {
536
575
  Provider: t,
537
576
  defaultValue: e
@@ -560,24 +599,24 @@ function yt(e) {
560
599
  function Et(e, t) {
561
600
  if (!t)
562
601
  throw new Error("Container element is not provided for rendering.");
563
- p(e, t);
602
+ h(e, t);
564
603
  }
565
- function bt(e = null) {
604
+ function vt(e = null) {
566
605
  return {
567
606
  current: e
568
607
  };
569
608
  }
570
- function vt(e, t) {
609
+ function gt(e, t) {
571
610
  const r = Array.isArray(e) ? e : [e];
572
611
  return I(r, t);
573
612
  }
574
613
  export {
575
- k as Component,
614
+ M as Component,
576
615
  yt as createContext,
577
- vt as createPortal,
578
- bt as createRef,
579
- Ot as h,
616
+ gt as createPortal,
617
+ vt as createRef,
618
+ mt as h,
580
619
  F as hFragment,
581
- mt as hString,
620
+ Ot as hString,
582
621
  Et as render
583
622
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ddd-react",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "scripts": {
5
5
  "test": "echo \"Error: no test specified\" && exit 1",
6
6
  "type-check": "tsc --noEmit",