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

Vue.jsのReactiveの中をのぞいてみた件

 Vue.jsのReactiveの中をのぞいてみた件

v-kansai#5でLTを発表した際の資料です

morimorikochan

April 25, 2019
Tweet

More Decks by morimorikochan

Other Decks in Technology

Transcript

  1. • 名前 ◦ morifuji ◦ @marooon88 • 趣味 ◦ kotlin

    ◦ ゲーム(switch) • 会社 ◦ atma株式会社 ◦ サーバーサイドエンジニア ▪ 最近はvue.js成分多め 誰?
  2. addTodo: function () { var value = this.newTodo && this.newTodo.trim()

    if (!value) { return } this.todos.push({ id: todoStorage.uid++, title: value, completed: false }) this.newTodo = '' }, やってみた
  3. addTodo: function () { var value = this.newTodo && this.newTodo.trim()

    if (!value) { return } this.todos.push({ id: todoStorage.uid++, title: value, completed: false }) this.newTodo = '' }, やってみた Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: function reactiveGetter () { const value = getter ? getter.call(obj) : val if (Dep.target) { dep.depend() if (childOb) { childOb.dep.depend() if (Array.isArray(value)) { dependArray(value) } } } return value },
  4. addTodo: function () { var value = this.newTodo && this.newTodo.trim()

    if (!value) { return } this.todos.push({ id: todoStorage.uid++, title: value, completed: false }) this.newTodo = '' }, やってみた Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: function reactiveGetter () { const value = getter ? getter.call(obj) : val if (Dep.target) { dep.depend() if (childOb) { childOb.dep.depend() if (Array.isArray(value)) { dependArray(value) } } } return value }, depend () { if (Dep.target) { Dep.target.addDep(this) } }
  5. やってみた doneEdit: function (todo) { if (!this.editedTodo) { return }

    this.editedTodo = null todo.title = todo.title.trim() if (!todo.title) { this.removeTodo(todo) } },
  6. やってみた doneEdit: function (todo) { if (!this.editedTodo) { return }

    this.editedTodo = null todo.title = todo.title.trim() if (!todo.title) { this.removeTodo(todo) } }, Object.defineProperty (obj, key, { enumerable: true, configurable: true, get: …………, set: function reactiveSetter (newVal) { const value = getter ? getter.call(obj) : val /* eslint-disable no-self-compare */ if (newVal === value || (newVal !== newVal && value !== value)) { return } /* eslint-enable no-self-compare */ if (process.env.NODE_ENV !== 'production' && customSetter ) { customSetter () } // #7981: for accessor properties without setter if (getter && !setter) return if (setter) { setter.call(obj, newVal) } else { val = newVal } childOb = !shallow && observe(newVal) dep.notify() }
  7. やってみた doneEdit: function (todo) { if (!this.editedTodo) { return }

    this.editedTodo = null todo.title = todo.title.trim() if (!todo.title) { this.removeTodo(todo) } }, Object.defineProperty (obj, key, { enumerable: true, configurable: true, get: …………, set: function reactiveSetter (newVal) { const value = getter ? getter.call(obj) : val /* eslint-disable no-self-compare */ if (newVal === value || (newVal !== newVal && value !== value)) { return } /* eslint-enable no-self-compare */ if (process.env.NODE_ENV !== 'production' && customSetter ) { customSetter () } // #7981: for accessor properties without setter if (getter && !setter) return if (setter) { setter.call(obj, newVal) } else { val = newVal } childOb = !shallow && observe(newVal) dep.notify() } notify () { // stabilize the subscriber list first const subs = this.subs.slice() if (process.env.NODE_ENV !== 'production' && !config.async) { // subs aren't sorted in scheduler if not running async // we need to sort them now to make sure they fire in correct // order subs.sort((a, b) => a.id - b.id) } for (let i = 0, l = subs.length; i < l; i++) { subs[i].update() } }
  8. ご静聴ありがとうございました 参考資料 • リアクティブの探求 ◦ https://jp.vuejs.org/v2/guide/reactivity.html • Understanding Rendering Process

    with Virtual DOM In Vue.js ◦ https://medium.com/@koheimikami/understanding-rendering-process-with-virtual-dom-in-vue-js-a6e602811782 • Evan you 氏のサンプルアプリ ◦ https://github.com/vuejs/vue/tree/dev/examples/todomvc • madge(依存関係を可視化するnpmライブラリ) ◦ https://github.com/pahen/madge まとめ