Skip to content

Commit

Permalink
refactor: onEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Oct 13, 2021
1 parent 2b2ee17 commit d76983b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example/componentUpdate/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 在 render 中使用 proxy 调用 emit 函数
// 也可以直接使用 this
// 验证 proxy 的实现逻辑
import { h, ref, reactive } from "../../lib/mini-vue.esm.js";
import { h, ref } from "../../lib/mini-vue.esm.js";
import Child from "./Child.js";

export default {
Expand Down
45 changes: 43 additions & 2 deletions lib/mini-vue.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/mini-vue.cjs.js.map

Large diffs are not rendered by default.

45 changes: 43 additions & 2 deletions lib/mini-vue.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/mini-vue.esm.js.map

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions src/runtime-dom/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// 源码里面这些接口是由 runtime-dom 来实现
// 这里先简单实现

import { isOn } from "../shared";

// 后面也修改成和源码一样的实现
export function hostCreateElement(type) {
console.log("hostCreateElement", type);
Expand Down Expand Up @@ -27,6 +30,41 @@ export function hostPatchProp(el, key, preValue, nextValue) {
console.log(`hostPatchProp 设置属性:${key} 值:${nextValue}`);
console.log(`key: ${key} 之前的值是:${preValue}`);

// TODO
// 需要参考 runtime-dom 的做法
// 这里需要额外的处理 nextValue 的值
// 因为 nextValue 是一个匿名函数的话,那么对比的时候,肯定2个匿名函数就不一样了
// 然后就会注册了2遍事件监听

if (isOn(key)) {
// 添加事件处理函数的时候需要注意一下
// 添加的和删除的必须是一个函数,不然的话 删除不掉
// 那么就需要把之前 add 的函数给存起来,后面删除的时候需要用到
// 存储所有的事件函数
const invokers = el._vei || (el._vei = {});
const existingInvoker = invokers[key];
if (nextValue && existingInvoker) {
// patch
// 直接修改函数的值即可
existingInvoker.value = nextValue;
} else {
const eventName = key.slice(2).toLowerCase();
if (nextValue) {
const invoker = (invokers[key] = nextValue);
el.addEventListener(eventName, invoker);
} else {
el.removeEventListener(eventName, existingInvoker);
invokers[key] = undefined;
}
}
} else {
if (nextValue === null || nextValue === "") {
el.removeAttribute(key);
} else {
el.setAttribute(key, nextValue);
}
}

switch (key) {
case "id":
case "tId":
Expand Down
3 changes: 3 additions & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export const camelize = (str: string): string => {

export const extend = Object.assign;

// 必须是 on+一个大写字母的格式开头
export const isOn = (key) => /^on[A-Z]/.test(key);

export function hasChanged(value, oldValue) {
return !Object.is(value, oldValue);
}
Expand Down

0 comments on commit d76983b

Please sign in to comment.