Advertisement
Guest User

Untitled

a guest
Aug 27th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // require() some stuff from npm (like you were using browserify)
  2. // and then hit Run Code to run it on the right
  3. // Require a default instance of immstruct (singleton instance)
  4. var immstruct = require('immstruct');
  5.  
  6. // Get the structure we previously defined under the ID `myKey`
  7. var structure = immstruct('myKey', { a: { b: { c: 1 } } });
  8.  
  9. var cursor = structure.cursor(['a', 'b', 'c']);
  10.  
  11. var updatedCursor = cursor.update(function (x) { // triggers `swap` in somefile.js
  12. return x + 1;
  13. });
  14.  
  15. var h = require('virtual-dom/h');
  16. var diff = require('virtual-dom/diff');
  17. var patch = require('virtual-dom/patch');
  18. var createElement = require('virtual-dom/create-element');
  19.  
  20. // 1: Create a function that declares what the DOM should look like
  21. function render(count) {
  22. return h('div', {
  23. style: {
  24. textAlign: 'center',
  25. lineHeight: (100 + count) + 'px',
  26. border: '1px solid red',
  27. width: (100 + count) + 'px',
  28. height: (100 + count) + 'px'
  29. }
  30. }, [String(updatedCursor.deref())]);
  31. }
  32.  
  33. // 2: Initialise the document
  34. var count = 0; // We need some app data. Here we just store a count.
  35.  
  36. var tree = render(count); // We need an initial tree
  37. var rootNode = createElement(tree); // Create an initial root DOM node ...
  38. document.body.appendChild(rootNode); // ... and it should be in the document
  39.  
  40. // 3: Wire up the update logic
  41. setInterval(function () {
  42. count++;
  43.  
  44. var newTree = render(count);
  45. var patches = diff(tree, newTree);
  46. rootNode = patch(rootNode, patches);
  47. tree = newTree;
  48. }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement