Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // require() some stuff from npm (like you were using browserify)
- // and then hit Run Code to run it on the right
- // Require a default instance of immstruct (singleton instance)
- var immstruct = require('immstruct');
- // Get the structure we previously defined under the ID `myKey`
- var structure = immstruct('myKey', { a: { b: { c: 1 } } });
- var cursor = structure.cursor(['a', 'b', 'c']);
- var updatedCursor = cursor.update(function (x) { // triggers `swap` in somefile.js
- return x + 1;
- });
- var h = require('virtual-dom/h');
- var diff = require('virtual-dom/diff');
- var patch = require('virtual-dom/patch');
- var createElement = require('virtual-dom/create-element');
- // 1: Create a function that declares what the DOM should look like
- function render(count) {
- return h('div', {
- style: {
- textAlign: 'center',
- lineHeight: (100 + count) + 'px',
- border: '1px solid red',
- width: (100 + count) + 'px',
- height: (100 + count) + 'px'
- }
- }, [String(updatedCursor.deref())]);
- }
- // 2: Initialise the document
- var count = 0; // We need some app data. Here we just store a count.
- var tree = render(count); // We need an initial tree
- var rootNode = createElement(tree); // Create an initial root DOM node ...
- document.body.appendChild(rootNode); // ... and it should be in the document
- // 3: Wire up the update logic
- setInterval(function () {
- count++;
- var newTree = render(count);
- var patches = diff(tree, newTree);
- rootNode = patch(rootNode, patches);
- tree = newTree;
- }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement