|
2 | 2 |
|
3 | 3 | Object.defineProperty(exports, '__esModule', { value: true }); |
4 | 4 |
|
| 5 | +function isObject(value) { |
| 6 | + return value !== null && typeof value === "object"; |
| 7 | +} |
5 | 8 | function isStructObject(value) { |
6 | 9 | return Object.prototype.toString.call(value) === "[object Object]"; |
7 | 10 | } |
@@ -37,43 +40,194 @@ function childrenShapeFlag(vNode) { |
37 | 40 | } |
38 | 41 | } |
39 | 42 |
|
| 43 | +function hasOwnProperty(originObject, property) { |
| 44 | + return Reflect.has(originObject, property); |
| 45 | +} |
| 46 | +function capitalize(s) { |
| 47 | + return s.charAt(0).toUpperCase() + s.slice(1); |
| 48 | +} |
| 49 | +function underlineIn2hump(string) { |
| 50 | + return string.replace(/-(\w)/g, (all, letter) => letter.toUpperCase()); |
| 51 | +} |
| 52 | + |
| 53 | +const targetMap = new Map; |
| 54 | +// 触发依赖 |
| 55 | +function trigger(target, property) { |
| 56 | + const deps = targetMap.get(target).get(property); |
| 57 | + if (deps) { |
| 58 | + triggerEffects(deps); |
| 59 | + } |
| 60 | +} |
| 61 | +function triggerEffects(deps) { |
| 62 | + for (const effect of deps) { |
| 63 | + if (effect.scheduler) { |
| 64 | + effect.scheduler(); |
| 65 | + } |
| 66 | + else { |
| 67 | + effect.run(); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const mutableGet = createGetter(); |
| 73 | +const readonlyGet = createGetter(true); |
| 74 | +const mutableSet = createSetter(); |
| 75 | +const shallowReadonlyGet = createGetter(true, true); |
| 76 | +const mutableHandlers = { |
| 77 | + get: mutableGet, |
| 78 | + set: mutableSet |
| 79 | +}; |
| 80 | +const readonlyHandlers = { |
| 81 | + get: readonlyGet, |
| 82 | + set(target, property, value) { |
| 83 | + console.warn(`Cannot set readonly property "${property}"!`); |
| 84 | + return true; |
| 85 | + } |
| 86 | +}; |
| 87 | +const shallowReadonlyHandlers = { |
| 88 | + get: shallowReadonlyGet, |
| 89 | + set(target, property, value) { |
| 90 | + console.warn(`Cannot set readonly property "${property}"!`); |
| 91 | + return true; |
| 92 | + } |
| 93 | +}; |
| 94 | +function createGetter(isReadonly = false, isShallow = false) { |
| 95 | + return function get(target, property) { |
| 96 | + // TODO: 优化这里的if逻辑 |
| 97 | + // isReactive |
| 98 | + if (property === "_v_isReactive" /* ReactiveFlags.IS_REACTIVE */) { |
| 99 | + return !isReadonly; |
| 100 | + } |
| 101 | + // isReadonly |
| 102 | + if (property === "_v_isReadonly" /* ReactiveFlags.IS_READONLY */) { |
| 103 | + return isReadonly; |
| 104 | + } |
| 105 | + // isShallowReadonly |
| 106 | + if (property === "_v_isShallowReadonly" /* ReactiveFlags.IS_SHALLOW_READONLY */) { |
| 107 | + return isShallow && isReadonly; |
| 108 | + } |
| 109 | + // isShallowReactive |
| 110 | + if (property === "_v_isShallowReactive" /* ReactiveFlags.IS_SHALLOW_REACTIVE */) { |
| 111 | + return !isReadonly && isShallow; |
| 112 | + } |
| 113 | + // 只要出现shallow证明是浅层的处理,可以同时处理shallowReactive 和 shallowReadonly |
| 114 | + const value = Reflect.get(target, property); |
| 115 | + if (isShallow) { |
| 116 | + return value; |
| 117 | + } |
| 118 | + if (isObject(value)) { |
| 119 | + return isReadonly |
| 120 | + ? readonly(value) |
| 121 | + : reactive(value); |
| 122 | + } |
| 123 | + return value; |
| 124 | + }; |
| 125 | +} |
| 126 | +function createSetter() { |
| 127 | + return function set(target, property, value) { |
| 128 | + const nextValue = Reflect.set(target, property, value); |
| 129 | + trigger(target, property); |
| 130 | + return nextValue; |
| 131 | + }; |
| 132 | +} |
| 133 | + |
| 134 | +function createReactiveObject(raw, baseHandlers) { |
| 135 | + if (!isStructObject(raw)) { |
| 136 | + console.warn("the value to be proxies is not an object: " + raw); |
| 137 | + return; |
| 138 | + } |
| 139 | + return new Proxy(raw, baseHandlers); |
| 140 | +} |
| 141 | +function reactive(raw) { |
| 142 | + return createReactiveObject(raw, mutableHandlers); |
| 143 | +} |
| 144 | +function readonly(raw) { |
| 145 | + return createReactiveObject(raw, readonlyHandlers); |
| 146 | +} |
| 147 | +function shallowReadonly(value) { |
| 148 | + return createReactiveObject(value, shallowReadonlyHandlers); |
| 149 | +} |
| 150 | + |
| 151 | +function toHandlerKeys(prefix = "on", eventProperty) { |
| 152 | + return prefix + eventProperty; |
| 153 | +} |
| 154 | +function emit(instance, eventProperty, ...args) { |
| 155 | + const handlerKey = toHandlerKeys("on", capitalize(underlineIn2hump(eventProperty))); |
| 156 | + if (hasOwnProperty(instance.props, handlerKey) && typeof instance.props[handlerKey] === "function") { |
| 157 | + // call the event handler |
| 158 | + instance.props[handlerKey](...args); |
| 159 | + } |
| 160 | +} |
| 161 | +// TODO: 运行时扩展实例 |
| 162 | +function extendRuntimeInstance(instance, extendApis = {}) { |
| 163 | + console.log("extendApis", extendApis); |
| 164 | + // XXX: 临时解决方案 |
| 165 | + // TODO: 待完善扩展实例 |
| 166 | + Object.keys(extendApis).forEach(key => { |
| 167 | + instance[key] = extendApis[key]; |
| 168 | + console.log(key, extendApis[key]); |
| 169 | + }); |
| 170 | +} |
| 171 | + |
| 172 | +function initProps(instance, rawProps = {}) { |
| 173 | + instance.props = rawProps; |
| 174 | +} |
| 175 | + |
40 | 176 | const PublicPropertiesMaps = { |
41 | 177 | "$el": (instance) => instance.vnode.el, |
42 | 178 | }; |
43 | 179 | const ComponentPublicInstanceHandlers = { |
44 | 180 | get({ _instance }, key) { |
45 | | - if (key in _instance.setupState) { |
| 181 | + // XXX: 是否会因为优先级导致props不可用 |
| 182 | + if (hasOwnProperty(_instance.setupState, key)) { |
46 | 183 | return _instance.setupState[key]; |
47 | 184 | } |
| 185 | + if (hasOwnProperty(_instance.props, key)) { |
| 186 | + return _instance.props[key]; |
| 187 | + } |
48 | 188 | if (key in PublicPropertiesMaps) { |
49 | 189 | return PublicPropertiesMaps[key](_instance); |
50 | 190 | } |
51 | 191 | } |
52 | 192 | }; |
53 | 193 |
|
| 194 | +function promiseEmit() { } |
| 195 | + |
| 196 | +var instanceRuntimeExtendApis = /*#__PURE__*/Object.freeze({ |
| 197 | + __proto__: null, |
| 198 | + promiseEmit: promiseEmit |
| 199 | +}); |
| 200 | + |
54 | 201 | function createComponentInstance(vnode) { |
| 202 | + // instance |
55 | 203 | const component = { |
56 | 204 | vnode, |
57 | 205 | type: vnode.type, |
58 | | - setupState: {} |
| 206 | + setupState: {}, |
| 207 | + props: {}, |
| 208 | + emit: (instance, event) => { }, |
59 | 209 | }; |
| 210 | + // 官方Emit |
| 211 | + component.emit = emit.bind(null, component); |
| 212 | + // TODO: runtime instance extend |
| 213 | + // NOTE: 这是自己扩展的 |
| 214 | + extendRuntimeInstance(component, instanceRuntimeExtendApis); |
60 | 215 | return component; |
61 | 216 | } |
62 | 217 | function setupComponent(instance) { |
63 | | - initProps(instance); |
| 218 | + initProps(instance, instance.vnode.props); |
64 | 219 | // initSlots(); |
65 | 220 | setupStatefulComponent(instance); |
66 | 221 | } |
67 | | -// props |
68 | | -function initProps(instance) { |
69 | | - console.log(instance); |
70 | | -} |
71 | 222 | function setupStatefulComponent(instance) { |
72 | 223 | const Component = instance.type; |
73 | 224 | const context = { _instance: instance, _component: Component }; |
74 | 225 | instance.proxy = new Proxy(context, ComponentPublicInstanceHandlers); |
75 | 226 | if (Component.setup) { |
76 | | - const setupResult = Component.setup(); |
| 227 | + // props is readonly because it's a Reflect instance |
| 228 | + const readonlyProps = shallowReadonly(instance.props); |
| 229 | + // TODO: context |
| 230 | + const setupResult = Component.setup(readonlyProps, Object.assign({ emit: instance.emit }, instanceRuntimeExtendApis)); |
77 | 231 | handleSetupResult(instance, setupResult); |
78 | 232 | } |
79 | 233 | } |
|
0 commit comments