<commit_before>/* global HTMLElement */

import diff from 'virtual-dom/diff'
import patch from 'virtual-dom/patch'
import dom from './dom'
import {getScheduler} from './scheduler-assignment'
import refsStack from './refs-stack'

export default function createCustomElementPrototype (nativePrototype) {
  return Object.create(nativePrototype, {
    createdCallback: {
      value: function () {
        if (isElementUnregistered(this)) {
          return
        }

        this.refs = {}
        this._setImmediateHandle = null
        this._attached = false
        this._updateRequested = false

        if (typeof this._createdCallback === 'function') {
          this._createdCallback()
        }
      }
    },

    attachedCallback: {
      value: function () {
        if (isElementUnregistered(this)) {
          return
        }

        this._attached = true

        if (this._setImmediateHandle) {
          global.clearImmediate(this._setImmediateHandle)
          this._setImmediateHandle = null
        } else {
          this.vnode = dom(this.tagName.toLowerCase())
          this.updateSync()
        }

        if (typeof this._attachedCallback === 'function') {
          this._attachedCallback()
        }
      }
    },

    detachedCallback: {
      value: function () {
        if (isElementUnregistered(this)) {
          return
        }

        this._attached = false

        if (typeof this._detachedCallback === 'function') {
          this._detachedCallback()
        }

        this._setImmediateHandle = global.setImmediate(() => {
          patch(this, diff(this.vnode, dom(this.tagName.toLowerCase())))
          this.vnode = null
          this._setImmediateHandle = null
        })
      }
    },

    attributeChangedCallback: {
      value: function (attributeName, oldValue, newValue) {
        if (typeof this._attributeChangedCallback === 'function') {
          this.attributeChangedCallback(attributeName, oldValue, newValue)
        }
      }
    },

    isAttached: {
      value: function () {
        return this._attached
      }
    },

    update: {
      value: function () {
        if (!this._updateRequested) {
          this._updateRequested = true
          getScheduler().updateDocument(() => {
            this._updateRequested = false
            this.updateSync()
          })
        }
      }
    },

    updateSync: {
      value: function () {
        if (this.isAttached()) {
          refsStack.push(this.refs)
          let newVnode = this.render()
          patch(this, diff(this.vnode, newVnode))
          this.vnode = newVnode
          refsStack.pop()
          return true
        } else {
          return false
        }
      }
    }
  })

  function isElementUnregistered (element) {
    return Object.getPrototypeOf(Object.getPrototypeOf(element)) === nativePrototype
  }
}<commit_msg>Remove check for element being unregistered<commit_after>/* global HTMLElement */

import diff from 'virtual-dom/diff'
import patch from 'virtual-dom/patch'
import dom from './dom'
import {getScheduler} from './scheduler-assignment'
import refsStack from './refs-stack'

export default function createCustomElementPrototype (nativePrototype) {
  return Object.create(nativePrototype, {
    createdCallback: {
      value: function () {
        this.refs = {}
        this._setImmediateHandle = null
        this._attached = false
        this._updateRequested = false

        if (typeof this._createdCallback === 'function') {
          this._createdCallback()
        }
      }
    },

    attachedCallback: {
      value: function () {
        this._attached = true

        if (this._setImmediateHandle) {
          global.clearImmediate(this._setImmediateHandle)
          this._setImmediateHandle = null
        } else {
          this.vnode = dom(this.tagName.toLowerCase())
          this.updateSync()
        }

        if (typeof this._attachedCallback === 'function') {
          this._attachedCallback()
        }
      }
    },

    detachedCallback: {
      value: function () {
        this._attached = false

        if (typeof this._detachedCallback === 'function') {
          this._detachedCallback()
        }

        this._setImmediateHandle = global.setImmediate(() => {
          patch(this, diff(this.vnode, dom(this.tagName.toLowerCase())))
          this.vnode = null
          this._setImmediateHandle = null
        })
      }
    },

    attributeChangedCallback: {
      value: function (attributeName, oldValue, newValue) {
        if (typeof this._attributeChangedCallback === 'function') {
          this.attributeChangedCallback(attributeName, oldValue, newValue)
        }
      }
    },

    isAttached: {
      value: function () {
        return this._attached
      }
    },

    update: {
      value: function () {
        if (!this._updateRequested) {
          this._updateRequested = true
          getScheduler().updateDocument(() => {
            this._updateRequested = false
            this.updateSync()
          })
        }
      }
    },

    updateSync: {
      value: function () {
        if (this.isAttached()) {
          refsStack.push(this.refs)
          let newVnode = this.render()
          patch(this, diff(this.vnode, newVnode))
          this.vnode = newVnode
          refsStack.pop()
          return true
        } else {
          return false
        }
      }
    }
  })
}
{'fraction_non_alphanumeric': 0.1088724584103512, 'fraction_numerical': 0.0, 'mean_word_length': 1.9312026002166847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6256329', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1468, 'n_words': 284}