From f7e0ce08c34d4bfd36af427d449d1d31857e8367 Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 24 Mar 2023 11:29:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81,=E5=87=8F?= =?UTF-8?q?=E5=B0=91=E4=B8=80=E4=BA=9B=E4=B8=8D=E5=BF=85=E9=A1=BB=E7=9A=84?= =?UTF-8?q?symbol?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/constants.js | 7 ------- src/index.js | 43 +++++++++++++++++-------------------------- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 3311b0c..9bcb8e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/core", - "version": "1.5.13", + "version": "1.5.14", "type": "module", "description": "百搭UI组件库的核心", "main": "dist/index.js", diff --git a/src/constants.js b/src/constants.js index 116ac58..78e6d21 100644 --- a/src/constants.js +++ b/src/constants.js @@ -39,18 +39,11 @@ export const WC_PART = Symbol('wc_path') export const NO_CHANGE = Symbol('wc-noChange') export const NOTHING = Symbol('wc-nothing') export const __finalized__ = Symbol('finalized') -export const __render__ = Symbol('render') -export const __init__ = Symbol('init') export const __props__ = Symbol('props') export const __changed_props__ = Symbol('changed_props') export const __mounted__ = Symbol('mounted') -export const __feedback__ = Symbol('feedback') export const __pending__ = Symbol('pending') -export const __prop2attr__ = Symbol('prop2attr') -export const __attr2prop__ = Symbol('attr2prop') -export const __clear_update__ = Symbol('clearupdate') export const __children__ = Symbol('children') -export const __updated__ = Symbol('updated') export const RESET_CSS_STYLE = `* {box-sizing: border-box;margin: 0;padding: 0;}::before,::after {box-sizing: border-box;}` diff --git a/src/index.js b/src/index.js index eb3ed7d..f977940 100644 --- a/src/index.js +++ b/src/index.js @@ -8,18 +8,11 @@ import { parsePropsDeclaration, boolMap, __finalized__, - __render__, - __init__, __props__, __changed_props__, __mounted__, - __feedback__, __pending__, - __prop2attr__, - __attr2prop__, - __clear_update__, - __children__, - __updated__ + __children__ } from './constants.js' import { css, adoptStyles } from './css.js' import { render, html, svg } from './html.js' @@ -114,7 +107,7 @@ export class Component extends HTMLElement { this.created() } - [__init__]() { + #init() { // 这里重新赋值一次, 用于清除掉因为 observer 修正产生的变化 this[__changed_props__] = new Map() // 初始化 props @@ -125,7 +118,7 @@ export class Component extends HTMLElement { // 若无定义props时, 手动执行一次渲染 if (this[__pending__] === false) { this[__pending__] = true - nextTick(_ => this[__updated__]()) + nextTick(_ => this.#confirmUpdate()) } } @@ -134,7 +127,7 @@ export class Component extends HTMLElement { } connectedCallback() { - this[__init__]() + this.#init() adoptStyles(this.root, this.constructor.styles) this[__children__]?.setConnected(true) @@ -149,7 +142,7 @@ export class Component extends HTMLElement { if (old === val) { return } - this[__attr2prop__](name, val, old) + this.#attr2prop(name, val, old) } /** @@ -158,7 +151,7 @@ export class Component extends HTMLElement { * @param name * @param value */ - [__prop2attr__](name, value) { + #prop2attr(name, value) { let options = this.#getPropOptions(name) if (options.attribute === false) { @@ -191,7 +184,7 @@ export class Component extends HTMLElement { * @param name * @param value */ - [__attr2prop__](name, value, old) { + #attr2prop(name, value, old) { let _name = boolMap[name], options if (_name) { @@ -206,27 +199,25 @@ export class Component extends HTMLElement { $requestUpdate(name, oldValue) { if (name !== void 0) { this[__changed_props__].set(name, this[name]) - this[__prop2attr__](name, this[name]) + this.#prop2attr(name, this[name]) } if (this[__pending__] === false) { this[__pending__] = true - nextTick(_ => this[__updated__]()) + nextTick(_ => this.#confirmUpdate()) } } // 确认更新到视图 - [__updated__]() { - if (this[__pending__]) { - let props = this[__changed_props__] - this[__render__]() - this[__feedback__](props) - this[__clear_update__]() - } + #confirmUpdate() { + let props = this[__changed_props__] + this.#render() + this.#feedback(props) + this.#clearUpdate() } // 更新回调反馈 - [__feedback__](props) { + #feedback(props) { // 初始化时不触发updated回调 if (this[__mounted__] === false) { this[__mounted__] = true @@ -236,13 +227,13 @@ export class Component extends HTMLElement { } } - [__clear_update__]() { + #clearUpdate() { this[__changed_props__] = new Map() this[__pending__] = false } // 渲染视图 - [__render__]() { + #render() { let ast = this.render() this[__children__] = render(ast, this.root, {