valyrian.js 6.0.2 → 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,18 +48,26 @@ 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
- let vnode = v(dom.tagName.toLowerCase(), {}, ...Array.from(dom.childNodes).filter((child) => child.nodeType === 1 || child.nodeType === 3).map((child) => {
53
- if (child.nodeType === 1) {
54
- 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);
63
+ }
55
64
  }
56
- let text = new Vnode(TextString, {}, []);
57
- text.nodeValue = String(child.nodeValue);
58
- text.dom = child;
59
- return text;
60
- }));
61
- [].forEach.call(dom.attributes, (prop) => vnode.props[prop.nodeName] = prop.nodeValue);
62
- vnode.dom = dom;
63
- return vnode;
65
+ let props = {};
66
+ [].forEach.call(dom.attributes, (prop) => props[prop.nodeName] = prop.nodeValue);
67
+ let vnode = v(dom.tagName.toLowerCase(), props, ...children);
68
+ vnode.dom = dom;
69
+ return vnode;
70
+ }
64
71
  }
65
72
  var trust = (htmlString) => {
66
73
  let div = createDomElement("div");
@@ -82,25 +89,44 @@ var reservedProps = {
82
89
  "v-class": true,
83
90
  "v-html": true
84
91
  };
92
+ var eventListenerNames = {};
93
+ var onCleanupList = [];
94
+ var onMountList = [];
95
+ var onUpdateList = [];
96
+ var onUnmountList = [];
85
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
+ }
86
112
  function onCleanup(callback) {
87
- if (current.app?.onCleanup.indexOf(callback) === -1) {
88
- current.app?.onCleanup.push(callback);
113
+ if (onCleanupList.indexOf(callback) === -1) {
114
+ onCleanupList.push(callback);
89
115
  }
90
116
  }
91
117
  function onUnmount(callback) {
92
- if (current.app?.onUnmount.indexOf(callback) === -1) {
93
- current.app?.onUnmount.push(callback);
118
+ if (onUnmountList.indexOf(callback) === -1) {
119
+ onUnmountList.push(callback);
94
120
  }
95
121
  }
96
122
  function onMount(callback) {
97
- if (current.app?.onMount.indexOf(callback) === -1) {
98
- current.app?.onMount.push(callback);
123
+ if (onMountList.indexOf(callback) === -1) {
124
+ onMountList.push(callback);
99
125
  }
100
126
  }
101
127
  function onUpdate(callback) {
102
- if (current.app?.onUpdate.indexOf(callback) === -1) {
103
- current.app?.onUpdate.push(callback);
128
+ if (onUpdateList.indexOf(callback) === -1) {
129
+ onUpdateList.push(callback);
104
130
  }
105
131
  }
106
132
  function mount(container, component) {
@@ -121,102 +147,54 @@ function mount(container, component) {
121
147
  } else {
122
148
  throw new Error("Component must be a Valyrian Component or a Vnode component");
123
149
  }
124
- if (component[ValyrianSymbol]) {
125
- unmount(component);
126
- } else {
127
- let eventListener = function(e) {
128
- let dom = e.target;
129
- let name = `v-on${e.type}`;
130
- while (dom) {
131
- if (dom[name]) {
132
- dom[name](e, dom);
133
- if (!e.defaultPrevented) {
134
- update(component);
135
- }
136
- return;
137
- }
138
- dom = dom.parentNode;
139
- }
140
- };
141
- component[ValyrianSymbol] = {
142
- isMounted: false,
143
- eventListenerNames: {},
144
- onCleanup: [],
145
- onMount: [],
146
- onUpdate: [],
147
- onUnmount: []
148
- };
149
- component[ValyrianSymbol].eventListener = eventListener;
150
- }
151
- component[ValyrianSymbol].component = vnodeComponent;
152
- component[ValyrianSymbol].container = appContainer;
153
- component[ValyrianSymbol].mainVnode = domToVnode(appContainer);
154
- return update(component);
155
- }
156
- function callCleanup(valyrianApp) {
157
- for (let i = 0; i < valyrianApp.onCleanup.length; i++) {
158
- valyrianApp.onCleanup[i]();
159
- }
160
- valyrianApp.onCleanup = [];
161
- }
162
- function callUnmount(valyrianApp) {
163
- for (let i = 0; i < valyrianApp.onUnmount.length; i++) {
164
- valyrianApp.onUnmount[i]();
165
- }
166
- valyrianApp.onUnmount = [];
167
- }
168
- function callMount(valyrianApp) {
169
- for (let i = 0; i < valyrianApp.onMount.length; i++) {
170
- valyrianApp.onMount[i]();
150
+ if (v.isMounted) {
151
+ unmount();
171
152
  }
172
- valyrianApp.onMount = [];
153
+ v.component = vnodeComponent;
154
+ v.container = appContainer;
155
+ v.mainVnode = domToVnode(appContainer);
156
+ return update();
173
157
  }
174
- function callUpdate(valyrianApp) {
175
- for (let i = 0; i < valyrianApp.onUpdate.length; i++) {
176
- valyrianApp.onUpdate[i]();
158
+ function callCallbackList(list) {
159
+ for (let i = 0; i < list.length; i++) {
160
+ list[i]();
177
161
  }
178
- valyrianApp.onUpdate = [];
162
+ list = [];
179
163
  }
180
- function update(component) {
181
- if (component && component[ValyrianSymbol]) {
182
- let valyrianApp = component[ValyrianSymbol];
183
- current.app = valyrianApp;
184
- valyrianApp.onCleanup.length && callCleanup(valyrianApp);
185
- let oldVnode = valyrianApp.mainVnode;
186
- valyrianApp.mainVnode = new Vnode(valyrianApp.mainVnode.tag, valyrianApp.mainVnode.props, [valyrianApp.component]);
187
- valyrianApp.mainVnode.dom = oldVnode.dom;
188
- 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);
189
171
  oldVnode = null;
190
- if (valyrianApp.isMounted === false) {
191
- valyrianApp.onMount.length && callMount(valyrianApp);
192
- valyrianApp.isMounted = true;
172
+ if (v.isMounted === false) {
173
+ onMountList.length && callCallbackList(onMountList);
174
+ v.isMounted = true;
193
175
  } else {
194
- valyrianApp.onUpdate.length && callUpdate(valyrianApp);
176
+ onUpdateList.length && callCallbackList(onUpdateList);
195
177
  }
196
178
  if (isNodeJs) {
197
- return valyrianApp.mainVnode.dom.innerHTML;
179
+ return v.mainVnode.dom.innerHTML;
198
180
  }
199
181
  }
200
182
  }
201
- function unmount(component) {
202
- if (!component || !component[ValyrianSymbol]) {
203
- return;
204
- }
205
- let valyrianApp = component[ValyrianSymbol];
206
- if (valyrianApp.isMounted) {
207
- valyrianApp.onCleanup.length && callCleanup(valyrianApp);
208
- valyrianApp.onUnmount.length && callUnmount(valyrianApp);
209
- let oldVnode = valyrianApp.mainVnode;
210
- valyrianApp.mainVnode = new Vnode(valyrianApp.mainVnode.tag, valyrianApp.mainVnode.props, []);
211
- valyrianApp.mainVnode.dom = oldVnode.dom;
212
- valyrianApp.mainVnode.isSVG = oldVnode.isSVG;
213
- 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);
214
192
  oldVnode = null;
193
+ v.component = null;
194
+ v.isMounted = false;
215
195
  if (isNodeJs) {
216
- return valyrianApp.mainVnode.dom.innerHTML;
196
+ return v.mainVnode.dom.innerHTML;
217
197
  }
218
- valyrianApp = null;
219
- Reflect.deleteProperty(component, ValyrianSymbol);
220
198
  }
221
199
  }
222
200
  var emptyVnode = new Vnode("__empty__", {}, []);
@@ -234,10 +212,9 @@ function sharedSetAttribute(prop, value, vnode, oldVnode) {
234
212
  return;
235
213
  }
236
214
  if (typeof value === "function") {
237
- let valyrianApp = current.app;
238
- if (prop in valyrianApp.eventListenerNames === false) {
239
- valyrianApp.eventListenerNames[prop] = true;
240
- 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);
241
218
  }
242
219
  vnode.dom[`v-${prop}`] = value;
243
220
  return;
@@ -278,7 +255,7 @@ function setAttributes(vnode, oldVnode) {
278
255
  }
279
256
  }
280
257
  }
281
- function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
258
+ function patch(newVnode, oldVnode = emptyVnode) {
282
259
  current.vnode = newVnode;
283
260
  current.oldVnode = oldVnode === emptyVnode ? Und : oldVnode;
284
261
  let newTree = newVnode.children;
@@ -334,7 +311,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
334
311
  shouldPatch = false;
335
312
  } else {
336
313
  setAttributes(childVnode, oldChildVnode);
337
- if (valyrianApp.isMounted) {
314
+ if (v.isMounted) {
338
315
  childVnode.props.onupdate && childVnode.props.onupdate(childVnode, oldChildVnode);
339
316
  } else {
340
317
  childVnode.props.oncreate && childVnode.props.oncreate(childVnode);
@@ -351,7 +328,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
351
328
  oldTree[i] && newKeyedList[oldTree[i].props.key] === Und && onremove(oldTree[i]);
352
329
  newVnode.dom.replaceChild(childVnode.dom, newVnode.dom.childNodes[i]);
353
330
  }
354
- shouldPatch && patch(childVnode, oldChildVnode, valyrianApp);
331
+ shouldPatch && patch(childVnode, oldChildVnode);
355
332
  }
356
333
  for (let i = newTreeLength; i < oldTreeLength; i++) {
357
334
  if (newKeyedList[oldTree[i].props.key] === Und) {
@@ -386,12 +363,12 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
386
363
  continue;
387
364
  }
388
365
  setAttributes(newChildVnode, oldChildVnode);
389
- if (valyrianApp.isMounted) {
366
+ if (v.isMounted) {
390
367
  newChildVnode.props.onupdate && newChildVnode.props.onupdate(newChildVnode, oldChildVnode);
391
368
  } else {
392
369
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
393
370
  }
394
- patch(newChildVnode, oldChildVnode, valyrianApp);
371
+ patch(newChildVnode, oldChildVnode);
395
372
  continue;
396
373
  }
397
374
  newChildVnode.dom = createDomElement(newChildVnode.tag, newChildVnode.isSVG);
@@ -399,7 +376,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
399
376
  oldChildVnode.tag !== TextString && onremove(oldChildVnode);
400
377
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
401
378
  newVnode.dom.replaceChild(newChildVnode.dom, oldChildVnode.dom);
402
- patch(newChildVnode, emptyVnode, valyrianApp);
379
+ patch(newChildVnode, emptyVnode);
403
380
  continue;
404
381
  }
405
382
  if (newChildVnode.tag === TextString) {
@@ -411,7 +388,7 @@ function patch(newVnode, oldVnode = emptyVnode, valyrianApp) {
411
388
  setAttributes(newChildVnode);
412
389
  newVnode.dom.appendChild(newChildVnode.dom);
413
390
  newChildVnode.props.oncreate && newChildVnode.props.oncreate(newChildVnode);
414
- patch(newChildVnode, emptyVnode, valyrianApp);
391
+ patch(newChildVnode, emptyVnode);
415
392
  }
416
393
  for (let i = newTreeLength; i < oldTreeLength; i++) {
417
394
  let oldChildVnode = oldTree[i];
@@ -569,6 +546,7 @@ v.reservedProps = reservedProps;
569
546
  v.isVnode = isVnode;
570
547
  v.isComponent = isComponent;
571
548
  v.isVnodeComponent = isVnodeComponent;
549
+ v.isMounted = false;
572
550
  v.isNodeJs = isNodeJs;
573
551
  v.trust = trust;
574
552
  v.onCleanup = onCleanup;
@@ -581,4 +559,5 @@ v.update = update;
581
559
  v.setAttribute = setAttribute;
582
560
  v.directive = directive;
583
561
  v.use = use;
562
+ var lib_default = v;
584
563
  module.exports = __toCommonJS(lib_exports);