valyrian.js 6.0.4 → 6.0.5

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,4 +1,5 @@
1
1
  /*** Vnode ***/
2
2
  import { Valyrian } from "./interfaces";
3
3
  /*** Hyperscript ***/
4
- export declare const v: Valyrian;
4
+ declare const v: Valyrian;
5
+ export default v;
@@ -4,7 +4,7 @@ export interface DomElement extends Element {
4
4
  }
5
5
  export interface Props {
6
6
  key?: string | number;
7
- data?: string;
7
+ state?: any;
8
8
  oncreate?: {
9
9
  (vnode: IVnode): never;
10
10
  };
@@ -29,12 +29,12 @@ export interface IVnode {
29
29
  dom?: DomElement;
30
30
  isSVG?: boolean;
31
31
  processed?: boolean;
32
- component?: Component | POJOComponent;
32
+ component?: ValyrianComponent;
33
33
  nodeValue?: string;
34
34
  [key: string | number | symbol]: any;
35
35
  }
36
36
  export interface Component {
37
- (props?: Record<string, any> | null, children?: Children): IVnode | Children;
37
+ (props?: Record<string, any> | null, children?: Children): any | IVnode | Children;
38
38
  [key: string | number | symbol]: any;
39
39
  }
40
40
  export interface POJOComponent {
@@ -52,25 +52,7 @@ export interface VnodeWithDom extends IVnode {
52
52
  export interface Directive {
53
53
  (value: any, vnode: VnodeWithDom, oldVnode?: VnodeWithDom): void;
54
54
  }
55
- export interface ValyrianApp {
56
- isMounted: boolean;
57
- eventListenerNames: Record<string, true>;
58
- onCleanup: Function[];
59
- onUnmount: Function[];
60
- onMount: Function[];
61
- onUpdate: Function[];
62
- eventListener?: EventListener;
63
- mainVnode?: VnodeWithDom;
64
- container?: DomElement;
65
- [key: string | number | symbol]: any;
66
- }
67
- export interface MountedValyrianApp extends ValyrianApp {
68
- eventListener: EventListener;
69
- mainVnode: VnodeWithDom;
70
- container: DomElement;
71
- }
72
55
  export interface Current {
73
- app?: ValyrianApp;
74
56
  component?: ValyrianComponent;
75
57
  vnode?: VnodeWithDom;
76
58
  oldVnode?: VnodeWithDom;
@@ -87,21 +69,25 @@ export interface Plugin {
87
69
  export interface Valyrian {
88
70
  (tagOrComponent: string | ValyrianComponent, props: Props, ...children: Children): IVnode | VnodeComponent;
89
71
  fragment: (props: Props, ...children: Children) => Children;
72
+ isNodeJs: boolean;
73
+ isMounted: boolean;
90
74
  current: Current;
75
+ container?: DomElement;
76
+ mainVnode?: VnodeWithDom;
77
+ component?: null | VnodeComponent;
91
78
  directives: Directives;
92
79
  reservedProps: ReservedProps;
93
80
  isVnode: (object?: unknown | IVnode) => object is IVnode;
94
81
  isComponent: (component?: unknown | ValyrianComponent) => component is ValyrianComponent;
95
82
  isVnodeComponent: (vnode?: unknown | VnodeComponent) => vnode is VnodeComponent;
96
- isNodeJs: boolean;
97
83
  trust: (htmlString: string) => Children;
98
84
  onCleanup: (fn: Function) => void;
99
85
  onUnmount: (fn: Function) => void;
100
86
  onMount: (fn: Function) => void;
101
87
  onUpdate: (fn: Function) => void;
102
88
  mount: (container: DomElement | string, component: ValyrianComponent | IVnode) => void | string;
103
- update: (component: ValyrianComponent | IVnode) => void | string;
104
- unmount: (component: ValyrianComponent | IVnode) => void | string;
89
+ update: () => void | string;
90
+ unmount: () => void | string;
105
91
  setAttribute: (name: string, value: any, vnode: VnodeWithDom, oldVnode?: VnodeWithDom) => void;
106
92
  directive: (name: string, directive: Directive) => void;
107
93
  use: (plugin: Plugin, options?: Record<string | number | symbol, any>) => void | any;
package/dist/index.cjs CHANGED
@@ -24,12 +24,11 @@ var __toCommonJS = /* @__PURE__ */ ((cache) => {
24
24
  // lib/index.ts
25
25
  var lib_exports = {};
26
26
  __export(lib_exports, {
27
- v: () => v
27
+ default: () => lib_default
28
28
  });
29
29
  var ComponentString = "__component__";
30
30
  var TextString = "#text";
31
31
  var isNodeJs = Boolean(typeof process !== "undefined" && process.versions && process.versions.node);
32
- var ValyrianSymbol = Symbol("Valyrian");
33
32
  var Und = void 0;
34
33
  var Vnode = function Vnode2(tag, props, children) {
35
34
  this.props = props;
@@ -49,19 +48,23 @@ function createDomElement(tag, isSVG = false) {
49
48
  return isSVG ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag);
50
49
  }
51
50
  function domToVnode(dom) {
52
- if (dom.nodeType === 1 || dom.nodeType === 3) {
53
- let vnode = v(dom.tagName.toLowerCase(), {}, ...Array.from(dom.childNodes).filter((child) => child.nodeType === 1 || child.nodeType === 3).map((child) => {
54
- if (child.nodeType === 1) {
55
- return domToVnode(child);
51
+ if (dom.nodeType === 3) {
52
+ let vnode = v(TextString, {}, []);
53
+ vnode.nodeValue = dom.nodeValue;
54
+ vnode.dom = dom;
55
+ return vnode;
56
+ }
57
+ if (dom.nodeType === 1) {
58
+ let children = [];
59
+ for (let i = 0; i < dom.childNodes.length; i++) {
60
+ let child = domToVnode(dom.childNodes[i]);
61
+ if (child) {
62
+ children.push(child);
56
63
  }
57
- let text = new Vnode(TextString, {}, []);
58
- text.nodeValue = String(child.nodeValue);
59
- text.dom = child;
60
- return text;
61
- }));
62
- if (dom.nodeType === 1) {
63
- [].forEach.call(dom.attributes, (prop) => vnode.props[prop.nodeName] = prop.nodeValue);
64
64
  }
65
+ let props = {};
66
+ [].forEach.call(dom.attributes, (prop) => props[prop.nodeName] = prop.nodeValue);
67
+ let vnode = v(dom.tagName.toLowerCase(), props, ...children);
65
68
  vnode.dom = dom;
66
69
  return vnode;
67
70
  }
@@ -86,25 +89,44 @@ var reservedProps = {
86
89
  "v-class": true,
87
90
  "v-html": true
88
91
  };
92
+ var eventListenerNames = {};
93
+ var onCleanupList = [];
94
+ var onMountList = [];
95
+ var onUpdateList = [];
96
+ var onUnmountList = [];
89
97
  var current = {};
98
+ function eventListener(e) {
99
+ let dom = e.target;
100
+ let name = `v-on${e.type}`;
101
+ while (dom) {
102
+ if (dom[name]) {
103
+ dom[name](e, dom);
104
+ if (!e.defaultPrevented) {
105
+ update();
106
+ }
107
+ return;
108
+ }
109
+ dom = dom.parentNode;
110
+ }
111
+ }
90
112
  function onCleanup(callback) {
91
- if (current.app?.onCleanup.indexOf(callback) === -1) {
92
- current.app?.onCleanup.push(callback);
113
+ if (onCleanupList.indexOf(callback) === -1) {
114
+ onCleanupList.push(callback);
93
115
  }
94
116
  }
95
117
  function onUnmount(callback) {
96
- if (current.app?.onUnmount.indexOf(callback) === -1) {
97
- current.app?.onUnmount.push(callback);
118
+ if (onUnmountList.indexOf(callback) === -1) {
119
+ onUnmountList.push(callback);
98
120
  }
99
121
  }
100
122
  function onMount(callback) {
101
- if (current.app?.onMount.indexOf(callback) === -1) {
102
- current.app?.onMount.push(callback);
123
+ if (onMountList.indexOf(callback) === -1) {
124
+ onMountList.push(callback);
103
125
  }
104
126
  }
105
127
  function onUpdate(callback) {
106
- if (current.app?.onUpdate.indexOf(callback) === -1) {
107
- current.app?.onUpdate.push(callback);
128
+ if (onUpdateList.indexOf(callback) === -1) {
129
+ onUpdateList.push(callback);
108
130
  }
109
131
  }
110
132
  function mount(container, component) {
@@ -125,102 +147,54 @@ function mount(container, component) {
125
147
  } else {
126
148
  throw new Error("Component must be a Valyrian Component or a Vnode component");
127
149
  }
128
- if (component[ValyrianSymbol]) {
129
- unmount(component);
130
- } else {
131
- let eventListener = function(e) {
132
- let dom = e.target;
133
- let name = `v-on${e.type}`;
134
- while (dom) {
135
- if (dom[name]) {
136
- dom[name](e, dom);
137
- if (!e.defaultPrevented) {
138
- update(component);
139
- }
140
- return;
141
- }
142
- dom = dom.parentNode;
143
- }
144
- };
145
- component[ValyrianSymbol] = {
146
- isMounted: false,
147
- eventListenerNames: {},
148
- onCleanup: [],
149
- onMount: [],
150
- onUpdate: [],
151
- onUnmount: []
152
- };
153
- component[ValyrianSymbol].eventListener = eventListener;
154
- }
155
- component[ValyrianSymbol].component = vnodeComponent;
156
- component[ValyrianSymbol].container = appContainer;
157
- component[ValyrianSymbol].mainVnode = domToVnode(appContainer);
158
- return update(component);
159
- }
160
- function callCleanup(valyrianApp) {
161
- for (let i = 0; i < valyrianApp.onCleanup.length; i++) {
162
- valyrianApp.onCleanup[i]();
163
- }
164
- valyrianApp.onCleanup = [];
165
- }
166
- function callUnmount(valyrianApp) {
167
- for (let i = 0; i < valyrianApp.onUnmount.length; i++) {
168
- valyrianApp.onUnmount[i]();
169
- }
170
- valyrianApp.onUnmount = [];
171
- }
172
- function callMount(valyrianApp) {
173
- for (let i = 0; i < valyrianApp.onMount.length; i++) {
174
- valyrianApp.onMount[i]();
150
+ if (v.isMounted) {
151
+ unmount();
175
152
  }
176
- valyrianApp.onMount = [];
153
+ v.component = vnodeComponent;
154
+ v.container = appContainer;
155
+ v.mainVnode = domToVnode(appContainer);
156
+ return update();
177
157
  }
178
- function callUpdate(valyrianApp) {
179
- for (let i = 0; i < valyrianApp.onUpdate.length; i++) {
180
- valyrianApp.onUpdate[i]();
158
+ function callCallbackList(list) {
159
+ for (let i = 0; i < list.length; i++) {
160
+ list[i]();
181
161
  }
182
- valyrianApp.onUpdate = [];
162
+ list = [];
183
163
  }
184
- function update(component) {
185
- if (component && component[ValyrianSymbol]) {
186
- let valyrianApp = component[ValyrianSymbol];
187
- current.app = valyrianApp;
188
- valyrianApp.onCleanup.length && callCleanup(valyrianApp);
189
- let oldVnode = valyrianApp.mainVnode;
190
- valyrianApp.mainVnode = new Vnode(valyrianApp.mainVnode.tag, valyrianApp.mainVnode.props, [valyrianApp.component]);
191
- valyrianApp.mainVnode.dom = oldVnode.dom;
192
- patch(valyrianApp.mainVnode, oldVnode, valyrianApp);
164
+ function update() {
165
+ if (v.component && v.mainVnode) {
166
+ onCleanupList.length && callCallbackList(onCleanupList);
167
+ let oldVnode = v.mainVnode;
168
+ v.mainVnode = new Vnode(v.mainVnode.tag, v.mainVnode.props, [v.component]);
169
+ v.mainVnode.dom = oldVnode.dom;
170
+ patch(v.mainVnode, oldVnode);
193
171
  oldVnode = null;
194
- if (valyrianApp.isMounted === false) {
195
- valyrianApp.onMount.length && callMount(valyrianApp);
196
- valyrianApp.isMounted = true;
172
+ if (v.isMounted === false) {
173
+ onMountList.length && callCallbackList(onMountList);
174
+ v.isMounted = true;
197
175
  } else {
198
- valyrianApp.onUpdate.length && callUpdate(valyrianApp);
176
+ onUpdateList.length && callCallbackList(onUpdateList);
199
177
  }
200
178
  if (isNodeJs) {
201
- return valyrianApp.mainVnode.dom.innerHTML;
179
+ return v.mainVnode.dom.innerHTML;
202
180
  }
203
181
  }
204
182
  }
205
- function unmount(component) {
206
- if (!component || !component[ValyrianSymbol]) {
207
- return;
208
- }
209
- let valyrianApp = component[ValyrianSymbol];
210
- if (valyrianApp.isMounted) {
211
- valyrianApp.onCleanup.length && callCleanup(valyrianApp);
212
- valyrianApp.onUnmount.length && callUnmount(valyrianApp);
213
- let oldVnode = valyrianApp.mainVnode;
214
- valyrianApp.mainVnode = new Vnode(valyrianApp.mainVnode.tag, valyrianApp.mainVnode.props, []);
215
- valyrianApp.mainVnode.dom = oldVnode.dom;
216
- valyrianApp.mainVnode.isSVG = oldVnode.isSVG;
217
- patch(valyrianApp.mainVnode, oldVnode, valyrianApp);
183
+ function unmount() {
184
+ if (v.isMounted && v.mainVnode && v.component) {
185
+ onCleanupList.length && callCallbackList(onCleanupList);
186
+ onUnmountList.length && callCallbackList(onUnmountList);
187
+ let oldVnode = v.mainVnode;
188
+ v.mainVnode = new Vnode(v.mainVnode.tag, v.mainVnode.props, []);
189
+ v.mainVnode.dom = oldVnode.dom;
190
+ v.mainVnode.isSVG = oldVnode.isSVG;
191
+ patch(v.mainVnode, oldVnode);
218
192
  oldVnode = null;
193
+ v.component = null;
194
+ v.isMounted = false;
219
195
  if (isNodeJs) {
220
- return valyrianApp.mainVnode.dom.innerHTML;
196
+ return v.mainVnode.dom.innerHTML;
221
197
  }
222
- valyrianApp = null;
223
- Reflect.deleteProperty(component, ValyrianSymbol);
224
198
  }
225
199
  }
226
200
  var emptyVnode = new Vnode("__empty__", {}, []);
@@ -238,10 +212,9 @@ function sharedSetAttribute(prop, value, vnode, oldVnode) {
238
212
  return;
239
213
  }
240
214
  if (typeof value === "function") {
241
- let valyrianApp = current.app;
242
- if (prop in valyrianApp.eventListenerNames === false) {
243
- valyrianApp.eventListenerNames[prop] = true;
244
- valyrianApp.container.addEventListener(prop.slice(2), valyrianApp.eventListener);
215
+ if (prop in eventListenerNames === false) {
216
+ eventListenerNames[prop] = true;
217
+ v.container.addEventListener(prop.slice(2), eventListener);
245
218
  }
246
219
  vnode.dom[`v-${prop}`] = value;
247
220
  return;
@@ -282,7 +255,7 @@ function setAttributes(vnode, oldVnode) {
282
255
  }
283
256
  }
284
257
  }
285
- function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
258
+ function patch(newVnode, oldVnode = emptyVnode) {
286
259
  current.vnode = newVnode;
287
260
  current.oldVnode = oldVnode === emptyVnode ? Und : oldVnode;
288
261
  let newTree = newVnode.children;
@@ -338,7 +311,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
338
311
  shouldPatch = false;
339
312
  } else {
340
313
  setAttributes(childVnode, oldChildVnode);
341
- if (valyrianApp.isMounted) {
314
+ if (v.isMounted) {
342
315
  childVnode.props.onupdate && childVnode.props.onupdate(childVnode, oldChildVnode);
343
316
  } else {
344
317
  childVnode.props.oncreate && childVnode.props.oncreate(childVnode);
@@ -355,7 +328,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
355
328
  oldTree[i] && newKeyedList[oldTree[i].props.key] === Und && onremove(oldTree[i]);
356
329
  newVnode.dom.replaceChild(childVnode.dom, newVnode.dom.childNodes[i]);
357
330
  }
358
- shouldPatch && patch(childVnode, oldChildVnode, valyrianApp);
331
+ shouldPatch && patch(childVnode, oldChildVnode);
359
332
  }
360
333
  for (let i = newTreeLength; i < oldTreeLength; i++) {
361
334
  if (newKeyedList[oldTree[i].props.key] === Und) {
@@ -390,12 +363,12 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
390
363
  continue;
391
364
  }
392
365
  setAttributes(newChildVnode, oldChildVnode);
393
- if (valyrianApp.isMounted) {
366
+ if (v.isMounted) {
394
367
  newChildVnode.props.onupdate && newChildVnode.props.onupdate(newChildVnode, oldChildVnode);
395
368
  } else {
396
369
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
397
370
  }
398
- patch(newChildVnode, oldChildVnode, valyrianApp);
371
+ patch(newChildVnode, oldChildVnode);
399
372
  continue;
400
373
  }
401
374
  newChildVnode.dom = createDomElement(newChildVnode.tag, newChildVnode.isSVG);
@@ -403,7 +376,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
403
376
  oldChildVnode.tag !== TextString && onremove(oldChildVnode);
404
377
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
405
378
  newVnode.dom.replaceChild(newChildVnode.dom, oldChildVnode.dom);
406
- patch(newChildVnode, emptyVnode, valyrianApp);
379
+ patch(newChildVnode, emptyVnode);
407
380
  continue;
408
381
  }
409
382
  if (newChildVnode.tag === TextString) {
@@ -415,7 +388,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
415
388
  setAttributes(newChildVnode);
416
389
  newVnode.dom.appendChild(newChildVnode.dom);
417
390
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
418
- patch(newChildVnode, emptyVnode, valyrianApp);
391
+ patch(newChildVnode, emptyVnode);
419
392
  }
420
393
  for (let i = newTreeLength; i < oldTreeLength; i++) {
421
394
  let oldChildVnode = oldTree[i];
@@ -573,6 +546,7 @@ v.reservedProps = reservedProps;
573
546
  v.isVnode = isVnode;
574
547
  v.isComponent = isComponent;
575
548
  v.isVnodeComponent = isVnodeComponent;
549
+ v.isMounted = false;
576
550
  v.isNodeJs = isNodeJs;
577
551
  v.trust = trust;
578
552
  v.onCleanup = onCleanup;
@@ -585,4 +559,5 @@ v.update = update;
585
559
  v.setAttribute = setAttribute;
586
560
  v.directive = directive;
587
561
  v.use = use;
562
+ var lib_default = v;
588
563
  module.exports = __toCommonJS(lib_exports);