@esportsplus/template 0.22.4 → 0.23.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.
@@ -1,3 +1,3 @@
1
- import { Element } from '../types.js';
2
- declare const _default: (anchor: Element, fn: Function) => void;
1
+ import { Element, Renderable } from '../types.js';
2
+ declare const _default: (anchor: Element, fn: (dispose?: VoidFunction) => Renderable<any>) => void;
3
3
  export default _default;
@@ -13,20 +13,23 @@ function update(anchor, value) {
13
13
  if (value == null || value === false) {
14
14
  value = '';
15
15
  }
16
+ let textnode = this.textnode;
16
17
  if (typeof value !== 'object') {
17
- let textnode = this.textnode;
18
18
  if (textnode) {
19
19
  nodeValue.call(textnode, String(value));
20
+ if (!textnode.isConnected) {
21
+ anchor.after(textnode);
22
+ }
20
23
  }
21
24
  else {
22
- textnode = this.textnode = text(String(value));
23
- }
24
- if (!textnode.isConnected) {
25
- anchor.after(textnode);
25
+ anchor.after(this.textnode = text(String(value)));
26
26
  }
27
27
  }
28
28
  else {
29
29
  let fragment = render(anchor, value), head = firstChild.call(fragment);
30
+ if (textnode && textnode.isConnected) {
31
+ remove([{ head: textnode, tail: textnode }]);
32
+ }
30
33
  if (head) {
31
34
  this.group = {
32
35
  head,
@@ -40,9 +43,21 @@ export default (anchor, fn) => {
40
43
  let context = {
41
44
  group: undefined,
42
45
  textnode: undefined
43
- }, state = STATE_HYDRATING;
44
- effect(() => {
45
- let value = fn();
46
+ }, dispose = fn.length ? () => {
47
+ let { group, textnode } = context;
48
+ if (textnode) {
49
+ group = { head: anchor, tail: textnode };
50
+ }
51
+ else if (group) {
52
+ group.head = anchor;
53
+ }
54
+ d();
55
+ if (group) {
56
+ remove([group]);
57
+ }
58
+ } : undefined, state = STATE_HYDRATING;
59
+ let d = effect(() => {
60
+ let value = fn(dispose);
46
61
  if (state === STATE_HYDRATING) {
47
62
  update.call(context, anchor, value);
48
63
  state = STATE_NONE;
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "private": false,
15
15
  "type": "module",
16
16
  "types": "./build/index.d.ts",
17
- "version": "0.22.4",
17
+ "version": "0.23.0",
18
18
  "scripts": {
19
19
  "build": "tsc && tsc-alias",
20
20
  "-": "-"
@@ -1,6 +1,6 @@
1
1
  import { effect } from '@esportsplus/reactivity';
2
2
  import { STATE_HYDRATING, STATE_NONE } from '~/constants';
3
- import { Element, SlotGroup } from '~/types';
3
+ import { Element, Renderable, SlotGroup } from '~/types';
4
4
  import { firstChild, lastChild, nodeValue } from '~/utilities/node'
5
5
  import { raf } from '~/utilities/queue'
6
6
  import { remove } from './cleanup';
@@ -18,24 +18,28 @@ function update(this: { group?: SlotGroup, textnode?: Node }, anchor: Element, v
18
18
  value = '';
19
19
  }
20
20
 
21
- if (typeof value !== 'object') {
22
- let textnode = this.textnode;
21
+ let textnode = this.textnode;
23
22
 
23
+ if (typeof value !== 'object') {
24
24
  if (textnode) {
25
25
  nodeValue.call(textnode, String(value));
26
+
27
+ if (!textnode.isConnected) {
28
+ anchor.after(textnode);
29
+ }
26
30
  }
27
31
  else {
28
- textnode = this.textnode = text( String(value) );
29
- }
30
-
31
- if (!textnode.isConnected) {
32
- anchor.after(textnode);
32
+ anchor.after( this.textnode = text( String(value) ) );
33
33
  }
34
34
  }
35
35
  else {
36
36
  let fragment = render(anchor, value),
37
37
  head = firstChild.call(fragment);
38
38
 
39
+ if (textnode && textnode.isConnected) {
40
+ remove([{ head: textnode as Element, tail: textnode as Element }]);
41
+ }
42
+
39
43
  if (head) {
40
44
  this.group = {
41
45
  head,
@@ -48,24 +52,40 @@ function update(this: { group?: SlotGroup, textnode?: Node }, anchor: Element, v
48
52
  }
49
53
 
50
54
 
51
- export default (anchor: Element, fn: Function) => {
55
+ export default (anchor: Element, fn: (dispose?: VoidFunction) => Renderable<any>) => {
52
56
  let context = {
53
57
  group: undefined as SlotGroup | undefined,
54
58
  textnode: undefined as Node | undefined
55
59
  },
60
+ dispose = fn.length ? () => {
61
+ let { group, textnode } = context;
62
+
63
+ if (textnode) {
64
+ group = { head: anchor, tail: textnode as Element };
65
+ }
66
+ else if (group) {
67
+ group.head = anchor;
68
+ }
69
+
70
+ d();
71
+
72
+ if (group) {
73
+ remove([group]);
74
+ }
75
+ } : undefined,
56
76
  state = STATE_HYDRATING;
57
77
 
58
- effect(() => {
59
- let value = fn();
78
+ let d = effect(() => {
79
+ let value = fn(dispose);
60
80
 
61
- if (state === STATE_HYDRATING) {
62
- update.call(context, anchor, value);
63
- state = STATE_NONE;
64
- }
65
- else if (state === STATE_NONE) {
66
- raf.add(() => {
81
+ if (state === STATE_HYDRATING) {
67
82
  update.call(context, anchor, value);
68
- });
69
- }
70
- });
83
+ state = STATE_NONE;
84
+ }
85
+ else if (state === STATE_NONE) {
86
+ raf.add(() => {
87
+ update.call(context, anchor, value);
88
+ });
89
+ }
90
+ });
71
91
  };