{"size": 1285, "ext": "js", "max_stars_repo_path": "src/components/Toast/toast.js", "max_stars_repo_name": "licenlc/demo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/Toast/toast.js", "max_issues_repo_name": "licenlc/demo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/Toast/toast.js", "max_forks_repo_name": "licenlc/demo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5438596491, "max_line_length": 70, "alphanum_fraction": 0.6653696498}
import Vue from 'Vue'
import ToastVue from './Toast.vue'

const ToastConstructor = Vue.extend(ToastVue)
let toolPool = []
let getInstance = () => {
  if (toolPool.length > 0) {
    let instance = toolPool[0]
    toolPool.splice(0, 1)
    return instance
  }
  return new ToastConstructor({
    el: document.createElement('div')
  })
}

let returnAnInstance = instance => {
  if (instance) {
    toolPool.push(instance)
  }
}

let removeDOM = event => {
  if (event.target.parentNode) {
    event.target.parentNode.removeChild(event.target)
  }
}

ToastConstructor.prototype.close = () => {
  this.visible = false
  this.closed = true
  this.$el.addEventListener('transitionend', removeDOM)
  returnAnInstance(this)
}

let Toast = (options = {}) => {
  let duration = options.duration || 5000
  let instance = getInstance()
  instance.closed = false
  clearTimeout(instance.timer)
  instance.desc = typeof options === 'string' ? options : options.desc
  instance.iconClass = options.iconClass || ''
  document.body.appendChild(instance.$el)
  Vue.nextTick(() => {
    instance.visible = true
    instance.timer = window.setTimeout(function () {
      if (instance.closed) {
        return
      }
      instance.close()
    }, duration)
    return instance
  })
}

export default Toast
