pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/WebReflection/hyperHTML/commit/66b703f899691c105bd19c0d3658e9eadf28ecc6

="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-7a1ad343bd40328c.css" /> Declarative hyperHTML.Component · WebReflection/hyperHTML@66b703f · GitHub
Skip to content

Commit 66b703f

Browse files
author
Andrea Giammarchi
committed
Declarative hyperHTML.Component
1 parent caa5f2e commit 66b703f

11 files changed

Lines changed: 1984 additions & 1569 deletions

File tree

cjs/classes/Component.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,59 @@
11
'use strict';
2+
const { UID } = require('../shared/constants.js');
3+
const { WeakMap } = require('../shared/poorlyfills.js');
4+
25
// hyperHTML.Component is a very basic class
36
// able to create Custom Elements like components
47
// including the ability to listen to connect/disconnect
58
// events via onconnect/ondisconnect attributes
9+
// Components can be created imperatively or declaratively.
10+
// The main difference is that declared components
11+
// will not automatically render on setState(...)
12+
// to simplify state handling on render.
613
function Component() {}
714
Object.defineProperty(exports, '__esModule', {value: true}).default = Component
815

916
// components will lazily define html or svg properties
1017
// as soon as these are invoked within the .render() method
1118
// Such render() method is not provided by the base class
1219
// but it must be available through the Component extend.
20+
// Declared components could implement a
21+
// render(props) method too and use props as needed.
1322
function setup(content) {
23+
const children = new WeakMap;
24+
const create = Object.create;
25+
const createEntry = (wm, id, component) => {
26+
wm.set(id, component);
27+
return component;
28+
};
29+
const get = (Class, info, id) => {
30+
switch (typeof id) {
31+
case 'object':
32+
case 'function':
33+
const wm = info.w || (info.w = new WeakMap);
34+
return wm.get(id) || createEntry(wm, id, new Class);
35+
default:
36+
const sm = info.p || (info.p = create(null));
37+
return sm[id] || (sm[id] = new Class);
38+
}
39+
};
40+
const set = context => {
41+
const info = {w: null, p: null};
42+
children.set(context, info);
43+
return info;
44+
};
45+
Object.defineProperties(
46+
Component,
47+
{
48+
for: {
49+
configurable: true,
50+
value(context, id) {
51+
const info = children.get(context) || set(context);
52+
return get(this, info, id == null ? 'default' : id);
53+
}
54+
}
55+
}
56+
);
1457
Object.defineProperties(
1558
Component.prototype,
1659
{
@@ -25,11 +68,12 @@ function setup(content) {
2568
svg: lazyGetter('svg', content),
2669
state: lazyGetter('state', function () { return this.defaultState; }),
2770
defaultState: {get() { return {}; }},
28-
setState: {value(state) {
71+
setState: {value(state, render) {
2972
const target = this.state;
3073
const source = typeof state === 'function' ? state.call(this, target) : state;
3174
for (const key in source) target[key] = source[key];
32-
this.render();
75+
if (render !== false) this.render();
76+
return this;
3377
}}
3478
}
3579
);

coverage/coverage.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy