优化异常处理

master 1.10.8
yutent 2023-09-19 18:35:33 +08:00
parent f54a5a3653
commit 4f4e8970ca
3 changed files with 24 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "wkit", "name": "wkit",
"version": "1.10.7", "version": "1.10.8",
"type": "module", "type": "module",
"description": "A library for building fast, lightweight web components.", "description": "A library for building fast, lightweight web components.",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -259,7 +259,7 @@ export class Component extends HTMLElement {
disconnectedCallback() { disconnectedCallback() {
if (this.keepAlive) { if (this.keepAlive) {
this.deactivated() this.#asyncCallSafety(this.deactivated)
} else { } else {
this[__children__]?.setConnected(false) this[__children__]?.setConnected(false)
@ -276,7 +276,7 @@ export class Component extends HTMLElement {
window.wkitd.deassign(this) window.wkitd.deassign(this)
} }
} }
this.unmounted() this.#asyncCallSafety(this.unmounted)
} }
} }
// 监听属性变化 // 监听属性变化
@ -386,11 +386,11 @@ export class Component extends HTMLElement {
this.$animate() this.$animate()
} }
if (this.keepAlive) { if (this.keepAlive) {
nextTick(_ => this.activated()) this.#asyncCallSafety(this.activated)
} }
nextTick(_ => this.mounted()) this.#asyncCallSafety(this.mounted)
} else { } else {
nextTick(_ => this.updated(props)) this.#asyncCallSafety(this.updated, props)
} }
} }
@ -411,6 +411,17 @@ export class Component extends HTMLElement {
console.error(err) console.error(err)
} }
} }
#asyncCallSafety(callback, arg) {
nextTick(_ => {
try {
callback.call(this, arg)
} catch (err) {
console.error(err)
}
})
}
// 几个生命周期回调 // 几个生命周期回调
created() {} created() {}
mounted() {} mounted() {}

View File

@ -79,12 +79,16 @@ export function offset(node) {
/** /**
* 事件绑定 * 事件绑定
*/ */
export function bind(dom, type, selector, fn, phase = true) { export function bind(dom, type = '', selector, fn, phase = true) {
let events = type.split(',') let events = type.split(',')
let callback let callback
let isWc = dom.host === dom let isWc = dom && dom.host === dom
let host = isWc ? dom : null let host = isWc ? dom : null
if (!dom || !type) {
return console.error('call bind: argument error')
}
if (typeof selector === 'function') { if (typeof selector === 'function') {
phase = fn phase = fn
fn = selector fn = selector
@ -147,7 +151,7 @@ export function bind(dom, type, selector, fn, phase = true) {
/** /**
* 解除事件绑定 * 解除事件绑定
*/ */
export function unbind(dom, type, fn = noop, phase = false) { export function unbind(dom, type = '', fn = noop, phase = false) {
let events = type.split(',') let events = type.split(',')
events.forEach(function (t) { events.forEach(function (t) {
dom.removeEventListener(t.trim(), fn, phase) dom.removeEventListener(t.trim(), fn, phase)