deno.land / x / nano_jsx@v0.1.0 / component.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { onNodeRemove } from './helpers.ts'import { tick, _render } from './core.ts'import { _state } from './state.ts'
export class Component<P extends Object = any, S = any> { public props: P public id: string private _elements: HTMLElement[] = [] private _skipUnmount = false private _hasUnmounted = false
constructor(props: P) { this.props = props || {} this.id = this._getHash() }
public static get isClass() { return true }
public get isClass() { return true }
setState(state: S, shouldUpdate: boolean = false) { const isObject = typeof state === 'object' && state !== null
// if state is an object, we merge the objects if (isObject && this.state !== undefined) this.state = { ...this.state, ...state } // else, we just overwrite it else this.state = state
if (shouldUpdate) this.update() }
set state(state: S) { _state.set(this.id, state) }
get state() { return _state.get(this.id) as S }
set initState(state: S) { if (this.state === undefined) this.state = state }
/** Returns all currently rendered node elements */ public get elements(): HTMLElement[] { return this._elements || [] }
public set elements(elements: HTMLElement[]) { if (!Array.isArray(elements)) elements = [elements]
elements.forEach(element => { this._elements.push(element) }) }
private _addNodeRemoveListener() { // check if didUnmount is unused if (/^[^{]+{\s+}$/gm.test(this.didUnmount.toString())) return
// listen if the root elements gets removed onNodeRemove(this.elements[0], () => { if (!this._skipUnmount) this._didUnmount() }) }
// @ts-ignore private _didMount(): any { this._addNodeRemoveListener() this.didMount() }
private _willUpdate(): any { this.willUpdate() }
private _didUpdate(): any { this.didUpdate() }
private _didUnmount(): any { if (this._hasUnmounted) return this.didUnmount() this._hasUnmounted = true }
public willMount(): any {} public didMount(): any {} public willUpdate(): any {} public didUpdate(): any {} public didUnmount(): any {}
public render(_update?: any): HTMLElement | void {}
/** Will forceRender the component */ public update(update?: any) { this._skipUnmount = true this._willUpdate() // get all current rendered node elements const oldElements = [...this.elements]
// clear this._elements = []
let el = this.render(update) el = _render(el) this.elements = el as any
// console.log('old: ', oldElements) // console.log('new: ', this.elements)
// get valid parent node const parent = oldElements[0].parentElement as HTMLElement
// make sure we have a parent if (!parent) console.warn('Component needs a parent element to get updated!')
// add all new node elements this.elements.forEach((child: HTMLElement) => { if (parent) parent.insertBefore(child, oldElements[0]) })
// remove all elements oldElements.forEach((child: HTMLElement) => { // wee keep the element if it is the same, for example if passed as a child if (!this.elements.includes(child)) { child.remove() // @ts-ignore child = null } })
// listen for node removal this._addNodeRemoveListener()
// @ts-ignore tick(() => { this._skipUnmount = false if (!this.elements[0].isConnected) this._didUnmount() else this._didUpdate() }) }
private _getHash(): any {}}
nano_jsx

Version Info

Tagged at
8 months ago