Skip to content

Commit 936828f

Browse files
committedDec 5, 2018
Rename App -> Component
1 parent c33cfb8 commit 936828f

10 files changed

+173
-173
lines changed
 

‎index.d.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ declare namespace hyperdom {
77
children: any[]
88
}
99

10-
abstract class HyperdomApp {
10+
abstract class HyperdomComponent {
1111
public refreshImmediately (): void
1212
public refreshComponent (): void
1313
public refresh (): void
1414
}
1515

16-
export abstract class RenderApp extends HyperdomApp {
17-
public abstract render (): VdomFragment | App
16+
export abstract class RenderComponent extends HyperdomComponent {
17+
public abstract render (): VdomFragment | Component
1818
}
1919

20-
export abstract class RoutesApp extends HyperdomApp {
20+
export abstract class RoutesComponent extends HyperdomComponent {
2121
public abstract routes (): router.Route[]
2222

23-
protected renderLayout (content: any): VdomFragment | App
23+
protected renderLayout (content: any): VdomFragment | Component
2424
}
2525

26-
export type App = RoutesApp | RenderApp
26+
export type Component = RoutesComponent | RenderComponent
2727

2828
interface DomAttachement {
2929
remove (): void
@@ -48,11 +48,11 @@ declare namespace hyperdom {
4848

4949
export type Binding = ObjectBinding | SimpleBinding
5050

51-
export type AppFn = (model?: any) => VdomFragment
51+
export type FnComponent = (model?: any) => VdomFragment
5252

53-
export function append (root: HTMLElement, app: App | AppFn, opts?: MountOpts): DomAttachement
53+
export function append (root: HTMLElement, component: Component | FnComponent, opts?: MountOpts): DomAttachement
5454

55-
export function replace (root: HTMLElement, app: App | AppFn, opts?: MountOpts): DomAttachement
55+
export function replace (root: HTMLElement, component: Component | FnComponent, opts?: MountOpts): DomAttachement
5656

5757
interface ModelMeta {
5858
error: {
@@ -75,7 +75,7 @@ declare namespace hyperdom {
7575
export type NodeProps = HtmlNodeProps & HyperdomNodeProps
7676

7777
// TODO Date?
78-
export type Renderable = string | number | boolean | undefined | null | VdomFragment | App
78+
export type Renderable = string | number | boolean | undefined | null | VdomFragment | Component
7979

8080
const html: {
8181
(tag: string, nodeProps: NodeProps, ...children: Renderable[]): VdomFragment,
@@ -91,12 +91,12 @@ declare namespace hyperdom {
9191
export function rawHtml (tag: string, ...children: Renderable[]): VdomFragment
9292
export function rawHtml (tag: string, nodeProps: NodeProps, ...children: Renderable[]): VdomFragment
9393

94-
export function viewComponent (app: App): VdomFragment
94+
export function viewComponent (component: Component): VdomFragment
9595

96-
export function appendVDom (root: VdomFragment, app: App | AppFn): DomAttachement
96+
export function appendVDom (root: VdomFragment, component: Component | FnComponent): DomAttachement
9797

9898
const jsx: (
99-
tag: string | { new<T extends object>(props: T, children: Renderable[]): App },
99+
tag: string | { new<T extends object>(props: T, children: Renderable[]): Component },
100100
nodeProps: NodeProps | undefined,
101101
children?: Renderable | Renderable[],
102102
) => VdomFragment

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ exports.component = function (model) {
2323
}
2424

2525
exports.currentRender = render.currentRender
26-
exports.RenderApp = function () {} // placeholder for typescript
27-
exports.RoutesApp = function () {} // placeholder for typescript
26+
exports.RenderComponent = function () {} // placeholder for typescript
27+
exports.RoutesComponent = function () {} // placeholder for typescript

‎readme.md

+76-76
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ Sponsored by:
6767
* [Using with Typescript](#using-with-typescript)
6868
* [Router based components](#router-based-components)
6969
* [Render based components](#render-based-components)
70-
* [Performance](#performance)
71-
* [Debugging](#debugging)
72-
* [Chrome Plugin](#chrome-plugin)
73-
* [File Names and Line Numbers](#file-names-and-line-numbers)
74-
* [Production Build](#production-build)
75-
* [Common Errors](#common-errors)
76-
* [Outside Render Cycle](#outside-render-cycle)
77-
* [Refresh Outside Render Cycle](#refresh-outside-render-cycle)
7870
* [API](#api)
7971
* [Rendering the Virtual DOM](#rendering-the-virtual-dom)
8072
* [The binding Attribute](#the-binding-attribute)
@@ -84,6 +76,14 @@ Sponsored by:
8476
* [Attaching to the DOM](#attaching-to-the-dom)
8577
* [Detach](#detach)
8678
* [Remove](#remove)
79+
* [Performance](#performance)
80+
* [Debugging](#debugging)
81+
* [Chrome Plugin](#chrome-plugin)
82+
* [File Names and Line Numbers](#file-names-and-line-numbers)
83+
* [Production Build](#production-build)
84+
* [Common Errors](#common-errors)
85+
* [Outside Render Cycle](#outside-render-cycle)
86+
* [Refresh Outside Render Cycle](#refresh-outside-render-cycle)
8787
* [Development](#development)
8888
* [Building](#building)
8989
* [Automated Testing](#automated-testing)
@@ -116,7 +116,7 @@ Same example in Typescript:
116116
```tsx
117117
import * as hyperdom from 'hyperdom'
118118
119-
class App extends hyperdom.RenderApp {
119+
class App extends hyperdom.RenderComponent {
120120
private name: string
121121
122122
public render() {
@@ -1243,7 +1243,7 @@ import * as router from 'hyperdom/router'
12431243

12441244
const home = router.route('**')
12451245

1246-
class Thing extends hyperdom.RoutesApp {
1246+
class Thing extends hyperdom.RoutesComponent {
12471247
public title = 'hello'
12481248

12491249
public routes () {
@@ -1263,7 +1263,7 @@ class Thing extends hyperdom.RoutesApp {
12631263
```typescript
12641264
import * as hyperdom from 'hyperdom'
12651265

1266-
class Thing extends hyperdom.RenderApp {
1266+
class Thing extends hyperdom.RenderComponent {
12671267
public title = 'hello'
12681268

12691269
public render () {
@@ -1272,71 +1272,6 @@ class Thing extends hyperdom.RenderApp {
12721272
}
12731273
```
12741274
1275-
## Performance
1276-
1277-
Hyperdom is usually very fast. It's based on [virtual-dom](https://github.com/Matt-Esch/virtual-dom) which has excellent performance, several times faster than React. See [these benchmarks](http://vdom-benchmark.github.io/vdom-benchmark/). However, if you have very large and interactive pages there are several strategies you can employ to speed things up.
1278-
1279-
* Consider only rendering a part of the page on certain events. For this, you can use a [component](#components) for the portion of the page you want to refresh, then return a component or an array of components from the event handler.
1280-
* Consider using [key](#keys) attributes for large dynamic lists of elements. Key attributes allow the diffing engine to spot differences inside lists of elements in some cases massively reducing the amount of DOM changes between renders.
1281-
* For form inputs with bindings, especially text inputs that can refresh the page on each keypress, consider using `hyperdom.html.binding()` to not refresh, or only refresh a component.
1282-
* Consider using a component with a `renderCacheKey()` method, to have finer control over when the component re-renders. You can reduce the total render time by not rendering portions of the page that don't change very often. When the `renderCacheKey()` result changes from one render to the next, the component will be re-rendered. When it doesn't change, the component won't be re-rendered.
1283-
* For parts of the page that don't ever change, you can pre-render the VDOM statically once and return the same VDOM on each render.
1284-
1285-
## Debugging
1286-
1287-
### Chrome Plugin
1288-
1289-
[https://chrome.google.com/webstore/detail/hyperdom-inpector/pggnlghflkefenflladfgkbcmfnjkcle](https://chrome.google.com/webstore/detail/hyperdom-inpector/pggnlghflkefenflladfgkbcmfnjkcle)
1290-
1291-
### File Names and Line Numbers
1292-
1293-
By using [transform-react-jsx-source](http://babeljs.io/docs/plugins/transform-react-jsx-source/) hyperdom will generate `data-file-name` and `data-line-number` attributes pointing to the file that generated the DOM.
1294-
1295-
```jsx
1296-
render() {
1297-
return <h1>{this.title}</h1>
1298-
}
1299-
```
1300-
1301-
Will generate
1302-
1303-
```html
1304-
<h1 data-file-name="/full/path/to/file.jsx" data-line-number="40">Title</h1>
1305-
```
1306-
1307-
## Production Build
1308-
1309-
Debugging features and deprecation warnings can be turned off for production builds. Hyperdom source code checks the `NODE_ENV` environment constiable, and when set to `production` will turn these features off.
1310-
1311-
To make a production build with webpack, use `webpack -p`.
1312-
1313-
To make a production build with browserify, use [envify](https://github.com/hughsk/envify) and ensure `NODE_ENV=production`, for e.g. `browserify -t [ envify --NODE_ENV production ] ...` and then use a minifier like [uglify](https://github.com/mishoo/UglifyJS2) to strip the disabled code.
1314-
1315-
## Common Errors
1316-
1317-
### Outside Render Cycle
1318-
1319-
> You cannot create virtual-dom event handlers outside a render function
1320-
1321-
This usually happens when you try to create virtual dom outside of a render function, which is ok, but if you try to add event handlers (`onclick` etc, or otherwise have attributes set to functions) then you'll see this error. This is because outside of the render cycle, there's no way for the event handlers to know which attachment to refresh - you could have several on a page at once.
1322-
1323-
Another cause of this error is if you have more than one instance of the hyperdom module loaded. This can occur if you have an NPM listing like this:
1324-
1325-
```
1326-
my-app@1.0.0 /Users/bob/dev/my-app
1327-
├── hyperdom@1.19.1
1328-
├── my-hyperdom-component@1.0.0
1329-
│ ├── hyperdom@1.19.1
1330-
```
1331-
1332-
With `my-hyperdom-component` depending on another `hyperdom`. Better to have `my-hyperdom-component` have a `peerDependency` on hyperdom, allowing it to use the `hyperdom` under `my-app`.
1333-
1334-
### Refresh Outside Render Cycle
1335-
1336-
> Please assign hyperdom.html.refresh during a render cycle if you want to use it in event handlers
1337-
1338-
This can occur if you use `hyperdom.html.refresh`, or `h.refresh` outside of a render cycle, for example, in an event handler or after a `setTimeout`. This is easily fixed, take a look at [Refresh Function](#refresh-function).
1339-
13401275
## API
13411276
13421277
### Rendering the Virtual DOM
@@ -1461,6 +1396,71 @@ attachment.remove();
14611396
14621397
Destroys the DOM, running any `onremove` handlers found in components. This will remove the DOM element.
14631398
1399+
## Performance
1400+
1401+
Hyperdom is usually very fast. It's based on [virtual-dom](https://github.com/Matt-Esch/virtual-dom) which has excellent performance, several times faster than React. See [these benchmarks](http://vdom-benchmark.github.io/vdom-benchmark/). However, if you have very large and interactive pages there are several strategies you can employ to speed things up.
1402+
1403+
* Consider only rendering a part of the page on certain events. For this, you can use a [component](#components) for the portion of the page you want to refresh, then return a component or an array of components from the event handler.
1404+
* Consider using [key](#keys) attributes for large dynamic lists of elements. Key attributes allow the diffing engine to spot differences inside lists of elements in some cases massively reducing the amount of DOM changes between renders.
1405+
* For form inputs with bindings, especially text inputs that can refresh the page on each keypress, consider using `hyperdom.html.binding()` to not refresh, or only refresh a component.
1406+
* Consider using a component with a `renderCacheKey()` method, to have finer control over when the component re-renders. You can reduce the total render time by not rendering portions of the page that don't change very often. When the `renderCacheKey()` result changes from one render to the next, the component will be re-rendered. When it doesn't change, the component won't be re-rendered.
1407+
* For parts of the page that don't ever change, you can pre-render the VDOM statically once and return the same VDOM on each render.
1408+
1409+
## Debugging
1410+
1411+
### Chrome Plugin
1412+
1413+
[https://chrome.google.com/webstore/detail/hyperdom-inpector/pggnlghflkefenflladfgkbcmfnjkcle](https://chrome.google.com/webstore/detail/hyperdom-inpector/pggnlghflkefenflladfgkbcmfnjkcle)
1414+
1415+
### File Names and Line Numbers
1416+
1417+
By using [transform-react-jsx-source](http://babeljs.io/docs/plugins/transform-react-jsx-source/) hyperdom will generate `data-file-name` and `data-line-number` attributes pointing to the file that generated the DOM.
1418+
1419+
```jsx
1420+
render() {
1421+
return <h1>{this.title}</h1>
1422+
}
1423+
```
1424+
1425+
Will generate
1426+
1427+
```html
1428+
<h1 data-file-name="/full/path/to/file.jsx" data-line-number="40">Title</h1>
1429+
```
1430+
1431+
## Production Build
1432+
1433+
Debugging features and deprecation warnings can be turned off for production builds. Hyperdom source code checks the `NODE_ENV` environment constiable, and when set to `production` will turn these features off.
1434+
1435+
To make a production build with webpack, use `webpack -p`.
1436+
1437+
To make a production build with browserify, use [envify](https://github.com/hughsk/envify) and ensure `NODE_ENV=production`, for e.g. `browserify -t [ envify --NODE_ENV production ] ...` and then use a minifier like [uglify](https://github.com/mishoo/UglifyJS2) to strip the disabled code.
1438+
1439+
## Common Errors
1440+
1441+
### Outside Render Cycle
1442+
1443+
> You cannot create virtual-dom event handlers outside a render function
1444+
1445+
This usually happens when you try to create virtual dom outside of a render function, which is ok, but if you try to add event handlers (`onclick` etc, or otherwise have attributes set to functions) then you'll see this error. This is because outside of the render cycle, there's no way for the event handlers to know which attachment to refresh - you could have several on a page at once.
1446+
1447+
Another cause of this error is if you have more than one instance of the hyperdom module loaded. This can occur if you have an NPM listing like this:
1448+
1449+
```
1450+
my-app@1.0.0 /Users/bob/dev/my-app
1451+
├── hyperdom@1.19.1
1452+
├── my-hyperdom-component@1.0.0
1453+
│ ├── hyperdom@1.19.1
1454+
```
1455+
1456+
With `my-hyperdom-component` depending on another `hyperdom`. Better to have `my-hyperdom-component` have a `peerDependency` on hyperdom, allowing it to use the `hyperdom` under `my-app`.
1457+
1458+
### Refresh Outside Render Cycle
1459+
1460+
> Please assign hyperdom.html.refresh during a render cycle if you want to use it in event handlers
1461+
1462+
This can occur if you use `hyperdom.html.refresh`, or `h.refresh` outside of a render cycle, for example, in an event handler or after a `setTimeout`. This is easily fixed, take a look at [Refresh Function](#refresh-function).
1463+
14641464
## Development
14651465
14661466
To get started:

‎router.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {VdomFragment, Binding, App} from "index"
1+
import {VdomFragment, Binding, Component} from "index"
22

33
// TODO: what else is there?
44
export interface RouteDefinition {
@@ -44,7 +44,7 @@ export interface RoutableComponent {
4444

4545
redirect? (params: object): string | undefined
4646

47-
render? (): VdomFragment | App | string // TODO add `render() => string` test to hyperdomSpec
47+
render? (): VdomFragment | Component | string // TODO add `render() => string` test to hyperdomSpec
4848
// TODO: Promise<void> ?
4949
onload? (params: object): void
5050
}

‎test/browser/hyperdomSpec.ts

+55-55
Large diffs are not rendered by default.

‎test/browser/routerSpec.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai'
22
import 'lie/polyfill'
3-
import {html as h, App, VdomFragment, RoutesApp, Binding, RenderApp} from '../..'
3+
import {html as h, Component, VdomFragment, RoutesComponent, Binding, RenderComponent} from '../..'
44
import * as hyperdomRouter from '../../router'
55
import * as detect from './detect'
66

@@ -24,7 +24,7 @@ function describeRouter (historyApiType: string) {
2424
let router: hyperdomRouter.Router
2525
let historyApi: hyperdomRouter.RouteHistory
2626

27-
function mount (app: App, url?: string) {
27+
function mount (app: Component, url?: string) {
2828
const options: any = {router}
2929

3030
if (historyApiType === 'hash') {
@@ -53,7 +53,7 @@ function describeRouter (historyApiType: string) {
5353
})
5454

5555
context('app with two routes', function () {
56-
let app: RoutesApp
56+
let app: RoutesComponent
5757
let routes: {
5858
[key: string]: hyperdomRouter.RouteHandler,
5959
}
@@ -64,7 +64,7 @@ function describeRouter (historyApiType: string) {
6464
b: router.route('/b'),
6565
}
6666

67-
app = new class extends RoutesApp {
67+
app = new class extends RoutesComponent {
6868
public routes () {
6969
return [
7070
routes.a({
@@ -122,7 +122,7 @@ function describeRouter (historyApiType: string) {
122122
})
123123

124124
function articleComponent () {
125-
return new class extends RenderApp {
125+
return new class extends RenderComponent {
126126
public resolve: () => any
127127
public id: string | number
128128
private article: string | undefined
@@ -155,7 +155,7 @@ function describeRouter (historyApiType: string) {
155155
}
156156

157157
context('routes with bindings', function () {
158-
class MyApp extends RoutesApp {
158+
class MyApp extends RoutesComponent {
159159
public article = articleComponent()
160160

161161
public routes () {
@@ -216,7 +216,7 @@ function describeRouter (historyApiType: string) {
216216
if (historyApiType === 'pushState') {
217217
describe('push replace', function () {
218218
context('app with bindings', function () {
219-
class MyApp extends RoutesApp {
219+
class MyApp extends RoutesComponent {
220220
public a: string
221221

222222
public routes () {
@@ -351,7 +351,7 @@ function describeRouter (historyApiType: string) {
351351

352352
describe('onload', function () {
353353
context('app with onload', function () {
354-
class MyApp extends RoutesApp {
354+
class MyApp extends RoutesComponent {
355355
public article = articleComponent()
356356

357357
public routes () {
@@ -399,7 +399,7 @@ function describeRouter (historyApiType: string) {
399399
let a: hyperdomRouter.RouteHandler
400400
let b: hyperdomRouter.RouteHandler
401401

402-
class MyApp extends RoutesApp {
402+
class MyApp extends RoutesComponent {
403403
public b: string
404404

405405
constructor (readonly home: hyperdomRouter.RouteHandler) {
@@ -489,7 +489,7 @@ function describeRouter (historyApiType: string) {
489489

490490
describe('sub routes', function () {
491491
context('app with sub routes', function () {
492-
class MyApp extends RoutesApp {
492+
class MyApp extends RoutesComponent {
493493
public a = aComponent()
494494

495495
constructor (readonly routeTable: hyperdomRouter.Routes) {
@@ -521,7 +521,7 @@ function describeRouter (historyApiType: string) {
521521
let routes: hyperdomRouter.Routes
522522

523523
function aComponent () {
524-
return new class extends RoutesApp {
524+
return new class extends RoutesComponent {
525525
private a: string
526526
private b: string
527527

@@ -599,7 +599,7 @@ function describeRouter (historyApiType: string) {
599599
})
600600

601601
describe('base url', function () {
602-
class MyApp extends RoutesApp {
602+
class MyApp extends RoutesComponent {
603603
constructor (readonly routeTable: hyperdomRouter.Routes) {
604604
super()
605605
}
@@ -703,7 +703,7 @@ function describeRouter (historyApiType: string) {
703703
b: router.route('/b'),
704704
}
705705

706-
const app = new class extends RoutesApp {
706+
const app = new class extends RoutesComponent {
707707
public routes () {
708708
return [
709709
routes.a({render () { return 'a' }}),
@@ -729,7 +729,7 @@ function describeRouter (historyApiType: string) {
729729
b: router.route('/b'),
730730
}
731731

732-
const app = new class extends RoutesApp {
732+
const app = new class extends RoutesComponent {
733733
public routes () {
734734
return [
735735
routes.a({render () { return 'a' }}),

‎test/browser/tsxSpec.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const browser = browserMonkey.find('.test')
77
describe('tsx integration', function () {
88
let $div: HTMLElement
99

10-
function mount (app: hyperdom.App | hyperdom.AppFn) {
10+
function mount (app: hyperdom.Component | hyperdom.FnComponent) {
1111
hyperdom.append($div, app)
1212
}
1313

@@ -26,7 +26,7 @@ describe('tsx integration', function () {
2626

2727
// just checking compilation errors here - hence xit
2828
xit('supports hyperdom specific node attributes', function () {
29-
function render (this: hyperdom.AppFn) {
29+
function render (this: hyperdom.FnComponent) {
3030
return (
3131
<div>
3232
<button binding={[this, 'name']}></button>
@@ -41,7 +41,7 @@ describe('tsx integration', function () {
4141
})
4242

4343
it('renders hyperdom viewComponent', async function () {
44-
class Blue extends hyperdom.RenderApp {
44+
class Blue extends hyperdom.RenderComponent {
4545
private readonly title: string
4646

4747
constructor (properties: { title: string }, readonly children: hyperdom.Renderable[]) {

‎test/server/jsdomSpec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as hyperdom from '../..'
22
import {JSDOM} from 'jsdom'
33
import {expect} from 'chai'
4-
import {RenderApp} from "../../index"
4+
import {RenderComponent} from "../../index"
55

66
describe('hyperdom', function () {
77
describe('.append()', function () {
88
it('renders elements in jsdom', function () {
99
const {window} = new JSDOM(``)
10-
const app = new class extends RenderApp {
10+
const app = new class extends RenderComponent {
1111
public render () {
1212
return hyperdom.html('p', 'hello')
1313
}

‎test/server/toHtmlSpec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as hyperdom from '../..'
22
import toHtml = require('../../toHtml')
33
const h = hyperdom.html
44
import {expect} from 'chai'
5-
import {RenderApp} from "../../index"
5+
import {RenderComponent} from "../../index"
66

77
describe('to html', function () {
88
it('can render regular virtual dom to HTML', function () {
@@ -29,7 +29,7 @@ describe('to html', function () {
2929
})
3030

3131
it('can render top-level model components to HTML', function () {
32-
const vdom = new class extends RenderApp {
32+
const vdom = new class extends RenderComponent {
3333
public render () {
3434
return h('div', 'component')
3535
}
@@ -42,7 +42,7 @@ describe('to html', function () {
4242

4343
it('can render view components to HTML', function () {
4444
const vdom = h('div',
45-
hyperdom.viewComponent(new class extends RenderApp {
45+
hyperdom.viewComponent(new class extends RenderComponent {
4646
public render () {
4747
return h('div', 'component')
4848
}
@@ -54,7 +54,7 @@ describe('to html', function () {
5454
})
5555

5656
it('can render top-level view components to HTML', function () {
57-
const vdom = hyperdom.viewComponent(new class extends RenderApp {
57+
const vdom = hyperdom.viewComponent(new class extends RenderComponent {
5858
public render () {
5959
return h('div', 'component')
6060
}

‎toHtml.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {App, VdomFragment} from "./index"
1+
import {Component, VdomFragment} from "./index"
22

3-
declare function toHtml (vdom: App | VdomFragment): string
3+
declare function toHtml (vdom: Component | VdomFragment): string
44
export = toHtml

0 commit comments

Comments
 (0)
Please sign in to comment.