Upgrade to Pro — share decks privately, control downloads, hide ads and more …

off-the-main-thread with WorkerDOM

off-the-main-thread with WorkerDOM

Kyoto.js #15( https://kyotojs.connpass.com/event/113442/ )の発表資料です。
WebWorkerの基本やWorkerDOM( https://github.com/ampproject/worker-dom )の紹介をしました。
WorkerDOMを使うとWorkerでReactも動かせるので、ぜひ試してみてください。

Masashi Hirano

January 12, 2019
Tweet

More Decks by Masashi Hirano

Other Decks in Programming

Transcript

  1. ฏ໺ ণ࢜ / Masashi Hirano ɹɹɹɹ Kyoto office @shisama_ @shisama

    Node.js Core Collaborator ؔ੢NodeֶԂOrganizer
  2. // workerͰ࣮ߦ͢ΔϑΝΠϧΛࢦఆ͠workerੜ੒ const worker = new Worker(“./worker.js"); // workerʹσʔλΛ౉͢ worker.postMessage(time);

    // workerଆ͔ΒσʔλΛड͚औͬͨͱ͖ worker.onmessage = (e) => { const el = document.createElement('div'); el.textContent = e.data; document.body.appendChild(el); } εϨουؒͷσʔλڞ༗ // ϝΠϯεϨου͔ΒσʔλΛड͚औͬͨͱ͖ onmessage = (e) => { const n = e.data; for (let i = 0; i <= n; i++) { // Կ͔ॏ͍ॲཧ // ॲཧ݁ՌΛϝΠϯεϨουʹ౉͢ postMessage(result); } } ϝΠϯεϨου Worker༻ϑΝΠϧ
  3. <div src=“worker_thread.js” id=“upgrade-me“></div> <script type="module"> import {upgradeElement} from ‘/path/to/index.mjs’; upgradeElement(document.getElementById(‘upgrade-me'),

    ‘/path/to/worker.mjs’); </script> WorkerDOM࢖͍ํ for (let i = 0; i <= n; i++) { // Կ͔ॏ͍ॲཧ // ॲཧ݁ՌΛϝΠϯεϨουʹ౉͢ const el = document.createElement('div'); el.textContent = result; document.body.appendChild(el); } HTML (ϝΠϯεϨου) : Worker༻ϑΝΠϧ :
  4. <div src=“worker_thread.js” id=“upgrade-me“></div> <script type="module"> import {upgradeElement} from ‘/path/to/index.mjs’; upgradeElement(document.getElementById(‘upgrade-me'),

    ‘/path/to/worker.mjs’); </script> WorkerDOM࢖͍ํ 1. WorkerଆͰ࣮ߦ͢ΔϑΝΠϧΛࢦఆ 2. worker-domͷindex.mjs͔ΒupgradeElementؔ਺Λimport 3. 1ͷDOMͱworker-domͷworker.mjsΛҾ਺ʹઃఆ࣮͠ߦ
  5. <!DOCTYPE html> <div src="./dist/app.js" id="hello"> <div id="root"></div> </div> <script type="module">

    import {upgradeElement} from '/dist/index.mjs'; upgradeElement(document.getElementById(‘hello’), '/dist/worker.mjs'); </script> WorkerDOM + React const App = () => { return <h1>Hello, World!</h1>; }; ReactDOM.render(<App />, document.getElementById('root')); HTML (ϝΠϯεϨου) : Worker༻ϑΝΠϧ :
  6. <!DOCTYPE html> <div src="./dist/app.js" id="hello"> <div id="root"></div> </div> <script type="module">

    import {upgradeElement} from '/dist/index.mjs'; upgradeElement(document.getElementById(‘hello’), '/dist/worker.mjs'); </script> WorkerDOM + React const App = () => { return <h1>Hello, World!</h1>; }; ReactDOM.render(<App />, document.getElementById('root')); HTML (ϝΠϯεϨου) : Worker༻ϑΝΠϧ : Workerଆ͔ΒΞΫηε͍ͨ͠DOM͸ upgradeElementͰࢦఆ͢ΔDOMͷதʹॻ͘
  7. ࢀߟ • ΢Σϒϒϥ΢βͷ off-the-main-thread API ͷ࿩ (@nhiroki) • Off the

    main thread with workers ! - Speaker Deck (@kosamari) • off-the-main-thread ͷ࣌୅ (@mizchi) • Off the main threadͷ࿩ - Speaker Deck (@yosuke_furukawa) • Web WorkersΛ༻͍ͯJavaScriptΛϚϧνεϨουԽ͢Δ (@kfurumiya)