|
1 | | -const hyperHTML = require('hyperhtml') |
2 | 1 | const onload = require('on-load') |
| 2 | +const html = require('hyperrender').html |
| 3 | +const svg = require('hyperrender').svg |
3 | 4 | const slice = Array.prototype.slice |
4 | 5 |
|
5 | | -const WIRE_OR_BOUND_NODE = /(update|hyperHTML)$/ |
6 | | -const ONLOAD_ATTR = /^data-onloadid/ |
7 | | - |
8 | 6 | module.exports = function hypercomponent (component) { |
9 | | - const render = hyperHTML.wire() |
10 | | - const renderer = typeof component === 'function' |
11 | | - ? component |
12 | | - : component.render |
13 | | - |
| 7 | + const symbol = { |
| 8 | + render: typeof component === 'function' ? component : component.render, |
| 9 | + load: component && component.load, |
| 10 | + unload: component && component.unload |
| 11 | + } |
14 | 12 | return function wireComponent () { |
15 | | - const onloadHandler = component.onload |
16 | | - const onunloadHandler = component.onunload |
17 | | - const args = slice.call(arguments) |
18 | | - |
19 | | - let isMounted = false |
20 | | - let el = null |
21 | | - |
22 | | - if ( |
23 | | - typeof args[0] === 'function' && |
24 | | - WIRE_OR_BOUND_NODE.test(args[0].name) |
25 | | - ) { |
26 | | - el = renderer.apply(renderer, args) |
27 | | - } else { |
28 | | - args.unshift(render) // asign default renderer |
29 | | - el = renderer.apply(renderer, args) |
30 | | - } |
31 | | - |
32 | | - if (!onloadHandler && !onunloadHandler) return el |
33 | | - |
34 | | - if (Array.isArray(el)) { |
35 | | - return el // A root elelemnt is required if you want to use mount/unmmount |
36 | | - } |
37 | | - |
38 | | - let len = (el.attributes && el.attributes.length) || 0 |
39 | | - while (len--) { |
40 | | - if (ONLOAD_ATTR.test(el.attributes[len].name)) { |
41 | | - isMounted = true |
42 | | - break |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - if (!isMounted) { |
47 | | - return onload(el, onloadHandler, onunloadHandler) |
48 | | - } |
49 | | - |
50 | | - return el |
| 13 | + const instance = new Component() |
| 14 | + instance._symbol = symbol |
| 15 | + instance._loaded = !(symbol.load || symbol.unload) |
| 16 | + instance._defaultArgs = slice.call(arguments) |
| 17 | + return instance |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function Component () { |
| 22 | + const self = this |
| 23 | + |
| 24 | + function wire () { |
| 25 | + return wire.html.apply(self, arguments) |
| 26 | + } |
| 27 | + |
| 28 | + wire.html = html(this) |
| 29 | + wire.svg = svg(this) |
| 30 | + |
| 31 | + this._wire = wire |
| 32 | +} |
| 33 | + |
| 34 | +Component.prototype.render = function render () { |
| 35 | + const self = this |
| 36 | + let args = [this._wire] // first arg is always our wire |
| 37 | + |
| 38 | + for (var |
| 39 | + i = 0, |
| 40 | + length = arguments.length; |
| 41 | + i < length; i++ |
| 42 | + ) { |
| 43 | + args[i + 1] = arguments[i] === undefined |
| 44 | + ? this._defaultArgs[i] // assign default arg if incomming is undefined |
| 45 | + : arguments[i] |
| 46 | + } |
| 47 | + |
| 48 | + if (this._loaded === false) { |
| 49 | + return onload(this._symbol.render.apply(this, args), load, unload) |
| 50 | + } |
| 51 | + |
| 52 | + return this._symbol.render.apply(this, args) |
| 53 | + |
| 54 | + function load () { |
| 55 | + self._loaded = true |
| 56 | + self._symbol.load.apply(null, arguments) |
| 57 | + } |
| 58 | + |
| 59 | + function unload () { |
| 60 | + self._loaded = false |
| 61 | + self._symbol.unload.apply(null, arguments) |
51 | 62 | } |
52 | 63 | } |
0 commit comments