@@ -232,14 +232,16 @@ var instanceRuntimeExtendApis = /*#__PURE__*/Object.freeze({
232232} ) ;
233233
234234let currentInstance = null ;
235- function createComponentInstance ( vnode ) {
235+ function createComponentInstance ( vnode , parent ) {
236236 // instance
237237 const component = {
238238 vnode,
239239 type : vnode . type ,
240240 setupState : { } ,
241241 props : { } ,
242242 slots : { } ,
243+ parent,
244+ provides : { } ,
243245 emit : ( instance , event ) => { } ,
244246 } ;
245247 // 官方Emit
@@ -292,42 +294,42 @@ function clearCurrentInstance() {
292294 currentInstance = null ;
293295}
294296
295- function render ( vnode , container ) {
296- patch ( vnode , container ) ;
297+ function render ( vnode , container , parentComponent ) {
298+ patch ( vnode , container , parentComponent ) ;
297299}
298- function patch ( vnode , container ) {
300+ function patch ( vnode , container , parentComponent ) {
299301 switch ( vnode === null || vnode === void 0 ? void 0 : vnode . type ) {
300302 case Fragment :
301- processFragment ( vnode , container ) ;
303+ processFragment ( vnode , container , parentComponent ) ;
302304 break ;
303305 case Text :
304306 processText ( vnode , container ) ;
305307 break ;
306308 default :
307309 if ( vnode . shapeFlag & 1 /* ShapeFlags.ELEMENT */ ) {
308- processElement ( vnode , container ) ;
310+ processElement ( vnode , container , parentComponent ) ;
309311 }
310312 if ( vnode . shapeFlag & 2 /* ShapeFlags.STATEFUL_COMPONENT */ ) {
311- processComponent ( vnode , container ) ;
313+ processComponent ( vnode , container , parentComponent ) ;
312314 }
313315 break ;
314316 }
315317}
316- function processElement ( vnode , container ) {
317- mountElement ( vnode , container ) ;
318+ function processElement ( vnode , container , parentComponent ) {
319+ mountElement ( vnode , container , parentComponent ) ;
318320}
319- function processComponent ( vnode , container ) {
320- mountComponent ( vnode , container ) ;
321+ function processComponent ( vnode , container , parentComponent ) {
322+ mountComponent ( vnode , container , parentComponent ) ;
321323}
322- function processFragment ( vnode , container ) {
323- mountChildren ( vnode === null || vnode === void 0 ? void 0 : vnode . children , container ) ;
324+ function processFragment ( vnode , container , parentComponent ) {
325+ mountChildren ( vnode === null || vnode === void 0 ? void 0 : vnode . children , container , parentComponent ) ;
324326}
325- function mountComponent ( initialVNode , container ) {
326- const instance = createComponentInstance ( initialVNode ) ;
327+ function mountComponent ( initialVNode , container , parentComponent ) {
328+ const instance = createComponentInstance ( initialVNode , parentComponent ) ;
327329 setupComponent ( instance ) ;
328330 setupRenderEffect ( instance , container , initialVNode ) ;
329331}
330- function mountElement ( vnode , container ) {
332+ function mountElement ( vnode , container , parentComponent ) {
331333 // vnode type -> div/span
332334 // vnode.el -> element.el
333335 const el = ( vnode . el = document . createElement ( vnode . type ) ) ;
@@ -336,16 +338,16 @@ function mountElement(vnode, container) {
336338 el . textContent = vnode . children ;
337339 }
338340 else if ( vnode . shapeFlag & 8 /* ShapeFlags.ARRAY_CHILDREN */ ) {
339- mountChildren ( vnode . children , el ) ;
341+ mountChildren ( vnode . children , el , parentComponent ) ;
340342 }
341343 // props
342344 addAttrs ( vnode , el ) ;
343345 // append to container
344346 container . appendChild ( el ) ;
345347}
346- function mountChildren ( children = [ ] , container ) {
348+ function mountChildren ( children = [ ] , container , parentComponent ) {
347349 children . forEach ( child => {
348- patch ( child , container ) ;
350+ patch ( child , container , parentComponent ) ;
349351 } ) ;
350352}
351353function isOnEvent ( propertyName ) {
@@ -371,7 +373,7 @@ function addAttrs(vnode, container) {
371373function setupRenderEffect ( instance , container , initialVNode ) {
372374 var _a ;
373375 const subTree = ( _a = instance === null || instance === void 0 ? void 0 : instance . render ) === null || _a === void 0 ? void 0 : _a . call ( instance . proxy ) ;
374- patch ( subTree , container ) ;
376+ patch ( subTree , container , instance ) ;
375377 initialVNode . el = subTree . el ;
376378}
377379function processText ( vnode , container ) {
@@ -384,7 +386,7 @@ function createApp(rootComponent) {
384386 mount ( rootContainer ) {
385387 // 转vNode, 基于vNode工作
386388 const vnode = createVNode ( rootComponent ) ;
387- render ( vnode , rootContainer ) ;
389+ render ( vnode , rootContainer , undefined ) ;
388390 }
389391 } ;
390392}
@@ -407,4 +409,25 @@ function renderSlots(slots, renderName, props = {}) {
407409 }
408410}
409411
410- export { clearCurrentInstance , createApp , createComponentInstance , createTextVNode , createVNode , getCurrentInstance , h , patch , render , renderSlots , setCurrentInstance , setupComponent } ;
412+ /**
413+ * Notes:
414+ * 1. instance只能在setup中调用, 故使得provide工/inject作的作用域仅为setup
415+ */
416+ function provide ( key , value ) {
417+ const currentInstance = getCurrentInstance ( ) ;
418+ if ( currentInstance ) {
419+ mountProvide ( currentInstance === null || currentInstance === void 0 ? void 0 : currentInstance . provides , key , value ) ;
420+ }
421+ }
422+ function mountProvide ( target , key , value ) {
423+ target [ key ] = value ;
424+ }
425+ function inject ( key ) {
426+ const currentInstance = getCurrentInstance ( ) ;
427+ if ( currentInstance ) {
428+ const parentProvides = currentInstance . parent . provides ;
429+ return parentProvides [ key ] ;
430+ }
431+ }
432+
433+ export { clearCurrentInstance , createApp , createComponentInstance , createTextVNode , createVNode , getCurrentInstance , h , inject , patch , provide , render , renderSlots , setCurrentInstance , setupComponent } ;
0 commit comments