|
| 1 | +import { Component, connect, Events, $h, $c, selectorData, createStore, render, update, mut } from "ivi"; |
| 2 | + |
| 3 | +let startTime = 0; |
| 4 | +let lastMeasure = null; |
| 5 | + |
| 6 | +function startMeasure(name) { |
| 7 | + startTime = performance.now(); |
| 8 | + lastMeasure = name; |
| 9 | +} |
| 10 | + |
| 11 | +function stopMeasure() { |
| 12 | + const last = lastMeasure; |
| 13 | + if (lastMeasure) { |
| 14 | + window.setTimeout(function () { |
| 15 | + const now = performance.now(); |
| 16 | + lastMeasure = null; |
| 17 | + console.log(`${last} took ${now - startTime}`); |
| 18 | + }, 0); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function random(max) { |
| 23 | + return Math.round(Math.random() * 1000) % max; |
| 24 | +} |
| 25 | + |
| 26 | +const A = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", |
| 27 | + "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", |
| 28 | + "important", "inexpensive", "cheap", "expensive", "fancy"]; |
| 29 | +const C = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
| 30 | +const N = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", |
| 31 | + "mouse", "keyboard"]; |
| 32 | + |
| 33 | +let nextId = 1; |
| 34 | + |
| 35 | +function buildData(count = 1000) { |
| 36 | + const data = new Array(count); |
| 37 | + for (let i = 0; i < count; i++) { |
| 38 | + data[i] = { |
| 39 | + id: nextId++, |
| 40 | + label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}`, |
| 41 | + }; |
| 42 | + } |
| 43 | + return data; |
| 44 | +} |
| 45 | + |
| 46 | +const store = createStore( |
| 47 | + { data: mut([]), selected: null }, |
| 48 | + function (state, action) { |
| 49 | + const data = state.data.ref; |
| 50 | + switch (action.type) { |
| 51 | + case "delete": |
| 52 | + data.splice(data.indexOf(action.item), 1); |
| 53 | + return Object.assign({}, state, { data: mut(data) }); |
| 54 | + case "run": |
| 55 | + return { data: mut(buildData(1000)), selected: null }; |
| 56 | + case "add": |
| 57 | + return Object.assign({}, state, { data: mut(state.data.ref.concat(buildData(1000))) }); |
| 58 | + case "update": |
| 59 | + for (let i = 0; i < data.length; i += 10) { |
| 60 | + const r = data[i]; |
| 61 | + data[i] = { id: r.id, label: r.label + " !!!" }; |
| 62 | + } |
| 63 | + return Object.assign({}, state); |
| 64 | + case "select": |
| 65 | + return Object.assign({}, state, { selected: action.item }); |
| 66 | + case "runlots": |
| 67 | + return { data: mut(buildData(10000)), selected: null }; |
| 68 | + case "clear": |
| 69 | + return { data: mut([]), selected: null }; |
| 70 | + case "swaprows": |
| 71 | + if (data.length > 10) { |
| 72 | + const a = data[4]; |
| 73 | + data[4] = data[9]; |
| 74 | + data[9] = a; |
| 75 | + } |
| 76 | + return Object.assign({}, state, { data: mut(data) }); |
| 77 | + } |
| 78 | + return state; |
| 79 | + }, |
| 80 | + function () { |
| 81 | + update(); |
| 82 | + stopMeasure(); |
| 83 | + } |
| 84 | +); |
| 85 | + |
| 86 | +class Row extends Component { |
| 87 | + constructor(props) { |
| 88 | + super(props); |
| 89 | + this.click = Events.onClick(this.click.bind(this)); |
| 90 | + this.del = Events.onClick(this.del.bind(this)); |
| 91 | + } |
| 92 | + |
| 93 | + click() { |
| 94 | + startMeasure("select"); |
| 95 | + store.dispatch({ type: "select", item: this.props.item }); |
| 96 | + } |
| 97 | + |
| 98 | + del() { |
| 99 | + startMeasure("delete"); |
| 100 | + store.dispatch({ type: "delete", item: this.props.item }); |
| 101 | + } |
| 102 | + |
| 103 | + render() { |
| 104 | + const { selected, item } = this.props; |
| 105 | + return $h("tr", selected ? "danger" : null) |
| 106 | + .children([ |
| 107 | + $h("td", "col-md-1").children(item.id), |
| 108 | + $h("td", "col-md-4").children( |
| 109 | + $h("a").events(this.click).children(item.label) |
| 110 | + ), |
| 111 | + $h("td", "col-md-1").children( |
| 112 | + $h("a").events(this.del).children( |
| 113 | + $h("span", "glyphicon glyphicon-remove").props({ "aria-hidden": "true" }) |
| 114 | + ) |
| 115 | + ), |
| 116 | + $h("td", "col-md-6") |
| 117 | + ]); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +const $Row = connect( |
| 122 | + function (prev, id) { |
| 123 | + const item = store.getState().data.ref[id]; |
| 124 | + const selected = store.getState().selected === item; |
| 125 | + if (prev && prev.in.item === item && prev.in.selected === selected) { |
| 126 | + return prev; |
| 127 | + } |
| 128 | + return selectorData({ item, selected }); |
| 129 | + }, |
| 130 | + Row |
| 131 | +); |
| 132 | + |
| 133 | +function RowList(rows) { |
| 134 | + const children = new Array(rows.length); |
| 135 | + for (let i = 0; i < children.length; i++) { |
| 136 | + children[i] = $Row(i).key(rows[i].id); |
| 137 | + } |
| 138 | + return $h("tbody").children(children); |
| 139 | +} |
| 140 | + |
| 141 | +const $RowList = connect( |
| 142 | + function (prev) { |
| 143 | + const rows = store.getState().data; |
| 144 | + if (prev && prev.in === rows) { |
| 145 | + return prev; |
| 146 | + } |
| 147 | + return selectorData(rows, rows.ref); |
| 148 | + }, |
| 149 | + RowList |
| 150 | +); |
| 151 | + |
| 152 | +function $Button(text, id) { |
| 153 | + return $h("div", "col-sm-6 smallpad").children( |
| 154 | + $h("button", "btn btn-primary btn-block") |
| 155 | + .events(Events.onClick(() => { |
| 156 | + startMeasure(id); |
| 157 | + store.dispatch({ type: id }); |
| 158 | + })) |
| 159 | + .props({ "type": "button", "id": id }) |
| 160 | + .children(text) |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +function Controller() { |
| 165 | + return $h("div", "container") |
| 166 | + .children([ |
| 167 | + $h("div", "jumbotron").children( |
| 168 | + $h("div", "row").children([ |
| 169 | + $h("div", "col-md-6").children( |
| 170 | + $h("h1").children("ivi v0.5.0") |
| 171 | + ), |
| 172 | + $h("div", "col-md-6").children( |
| 173 | + $h("div", "row").children([ |
| 174 | + $Button("Create 1,000 rows", "run"), |
| 175 | + $Button("Create 10,000 rows", "runlots"), |
| 176 | + $Button("Append 1,000 rows", "add"), |
| 177 | + $Button("Update every 10th row", "update"), |
| 178 | + $Button("Clear", "clear"), |
| 179 | + $Button("Swap Rows", "swaprows") |
| 180 | + ]) |
| 181 | + ) |
| 182 | + ]) |
| 183 | + ), |
| 184 | + $h("table", "table table-hover table-striped test-data").children($RowList()), |
| 185 | + $h("span", "preloadicon glyphicon glyphicon-remove").props({ "aria-hidden": "true" }) |
| 186 | + ]); |
| 187 | +} |
| 188 | + |
| 189 | +render($c(Controller), document.getElementById("main")); |
0 commit comments