|
| 1 | +import { html, signal, batch, forEach, mount } from "@hellajs/core"; |
| 2 | + |
| 3 | +const { div, table, tbody, tr, td, button, span, a, h1 } = html; |
| 4 | + |
| 5 | +const adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"]; |
| 6 | +const colors = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"]; |
| 7 | +const nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"]; |
| 8 | + |
| 9 | +const random = (max) => Math.round(Math.random() * 1000) % max; |
| 10 | + |
| 11 | +let nextId = 1; |
| 12 | + |
| 13 | +const buildData = (count) => { |
| 14 | + let d = new Array(count); |
| 15 | + for (let i = 0; i < count; i++) { |
| 16 | + const label = signal( |
| 17 | + `${adjectives[random(adjectives.length)]} ${colors[random(colors.length)]} ${nouns[random(nouns.length)]}` |
| 18 | + ); |
| 19 | + d[i] = { id: nextId++, label }; |
| 20 | + } |
| 21 | + return d; |
| 22 | +}; |
| 23 | + |
| 24 | +const ActionButton = ( |
| 25 | + id, |
| 26 | + label, |
| 27 | + onclick |
| 28 | +) => |
| 29 | + div({ class: "col-sm-6" }, |
| 30 | + button({ id, onclick, class: 'btn btn-primary btn-block col-md-6', type: "button" }, |
| 31 | + label |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | +function Bench() { |
| 36 | + const rows = signal([]); |
| 37 | + const selected = signal(undefined); |
| 38 | + |
| 39 | + const create = (count) => { |
| 40 | + rows.set(buildData(count)); |
| 41 | + } |
| 42 | + |
| 43 | + const append = (count) => { |
| 44 | + rows.set([...rows(), ...buildData(count)]); |
| 45 | + } |
| 46 | + |
| 47 | + const update = () => { |
| 48 | + batch(() => { |
| 49 | + for (let i = 0, d = rows(), len = d.length; i < len; i += 10) { |
| 50 | + const label = d[i].label; |
| 51 | + label.set(`${label()} !!!`); |
| 52 | + } |
| 53 | + }) |
| 54 | + }; |
| 55 | + |
| 56 | + const swapRows = () => { |
| 57 | + const list = rows().slice(); |
| 58 | + if (list.length > 998) { |
| 59 | + let item = list[1]; |
| 60 | + list[1] = list[998]; |
| 61 | + list[998] = item; |
| 62 | + rows.set(list); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const remove = (id) => { |
| 67 | + rows.set(rows().filter(row => row.id !== id)); |
| 68 | + } |
| 69 | + |
| 70 | + const clear = () => { |
| 71 | + rows.set([]); |
| 72 | + } |
| 73 | + |
| 74 | + return div({ id: 'main' }, |
| 75 | + div({ class: 'container' }, |
| 76 | + div({ class: 'jumbotron' }, |
| 77 | + div({ class: 'row' }, |
| 78 | + div({ class: 'col-md-6' }, h1('HellaJS Keyed')), |
| 79 | + div({ class: 'col-md-6' }, |
| 80 | + div({ class: 'row' }, |
| 81 | + ActionButton('run', 'Create 1,000 rows', () => create(1000)), |
| 82 | + ActionButton('runlots', 'Create 10,000 rows', () => create(10000)), |
| 83 | + ActionButton('add', 'Append 1,000 rows', () => append(1000)), |
| 84 | + ActionButton('update', 'Update every 10th row', () => update()), |
| 85 | + ActionButton('clear', 'Clear', () => clear()), |
| 86 | + ActionButton('swaprows', 'Swap Rows', () => swapRows()), |
| 87 | + ) |
| 88 | + ), |
| 89 | + ), |
| 90 | + ), |
| 91 | + table({ class: 'table table-hover table-striped test-rows' }, |
| 92 | + tbody( |
| 93 | + forEach(rows, (row) => |
| 94 | + tr({ class: () => (selected() === row.id ? 'danger' : '') }, |
| 95 | + td({ class: 'col-md-1' }, row.id), |
| 96 | + td({ class: 'col-md-4' }, |
| 97 | + a({ class: 'lbl', onclick: () => selected.set(row.id) }, |
| 98 | + row.label |
| 99 | + ), |
| 100 | + ), |
| 101 | + td({ class: 'col-md-1' }, |
| 102 | + a({ class: 'remove', onclick: () => remove(row.id) }, |
| 103 | + span({ class: 'glyphicon glyphicon-remove', ariaHidden: 'true' }) |
| 104 | + ), |
| 105 | + ), |
| 106 | + ) |
| 107 | + ) |
| 108 | + ), |
| 109 | + ), |
| 110 | + span({ class: 'preloadicon glyphicon glyphicon-remove' }, ''), |
| 111 | + ), |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +mount(Bench, "#app"); |
0 commit comments