main-loop

WebJar for main-loop

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

main-loop
Last Version

Last Version

3.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

main-loop
WebJar for main-loop
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/Raynos/main-loop

Download main-loop

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/main-loop/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>main-loop</artifactId>
    <version>3.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/main-loop/
implementation 'org.webjars.npm:main-loop:3.1.0'
// https://jarcasting.com/artifacts/org.webjars.npm/main-loop/
implementation ("org.webjars.npm:main-loop:3.1.0")
'org.webjars.npm:main-loop:jar:3.1.0'
<dependency org="org.webjars.npm" name="main-loop" rev="3.1.0">
  <artifact name="main-loop" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='main-loop', version='3.1.0')
)
libraryDependencies += "org.webjars.npm" % "main-loop" % "3.1.0"
[org.webjars.npm/main-loop "3.1.0"]

Dependencies

compile (2)

Group / Artifact Type Version
org.webjars.npm : error jar [4.1.1,5)
org.webjars.npm : raf jar [2.0.1,3)

Project Modules

There are no modules declared in this project.

main-loop

A rendering loop for diffable UIs

main-loop is an optimization module for a virtual DOM system. Normally you would re-create the virtual tree every time your state changes. This is not optimum, with main-loop you will only update your virtual tree at most once per request animation frame.

main-loop basically gives you batching of your virtual DOM changes, which means if you change your model multiple times it will be rendered once asynchronously on the next request animation frame.

Example

var mainLoop = require("main-loop")
var h = require("virtual-dom/h")

var initState = { fruits: ["apple", "banana"], name: "Steve" }

function render(state) {
    return h("div", [
        h("div", [
            h("span", "hello "),
            h("span.name", state.name)
        ]),
        h("ul", state.fruits.map(renderFruit))
    ])

    function renderFruit(fruitName) {
        return h("li", [
            h("span", fruitName)
        ])
    }
}

// set up a loop
var loop = mainLoop(initState, render, {
    create: require("virtual-dom/create-element"),
    diff: require("virtual-dom/diff"),
    patch: require("virtual-dom/patch")
})
document.body.appendChild(loop.target)

// update the loop with the new application state
loop.update({
    fruits: ["apple", "banana", "cherry"],
    name: "Steve"
})
loop.update({
    fruits: ["apple", "banana", "cherry"],
    name: "Stevie"
})

var loop = mainLoop(initState, render, opts)

Create a loop object with some initial state, a render function, and some options. Your function render (state) {} receives the current state as its argument and must return a virtual-dom object.

You must supply: opts.diff, opts.patch, and opts.create. These can be obtained directly from require("virtual-dom").

Optionally supply an opts.target and opts.initialTree.

loop.target

The main-loop root DOM element. Insert this element to the page.

loop.update(newState)

Update the page state, automatically re-rendering the page as necessary.

loop.state

Read the current main-loop state. To modify the loop state, use loop.update().

Installation

npm install main-loop

Contributors

  • Raynos

MIT Licenced

Versions

Version
3.1.0