Skip to content

Commit e55d75a

Browse files
author
lishiwen
committed
feat: basic provide/inject
1 parent f6857af commit e55d75a

File tree

7 files changed

+62
-19
lines changed

7 files changed

+62
-19
lines changed

example/provide-inject/Consumer.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import { h, inject } from "../../lib/m-vue.esm.js";
1+
import { createTextVNode, h, inject } from "../../lib/m-vue.esm.js";
22

33
export default {
44
name: "Consumer",
55

66
render() {
7-
return h("span", {}, `inject value: ${this.bar}`);
7+
return h("span", {}, [
8+
createTextVNode(`inject value: ${this.bar}`),
9+
h("div", {}, `当前的Count: ${this.count}`),
10+
createTextVNode("第二层提供的 inject value:" + this.two),
11+
]);
812
},
913

1014
setup() {
1115
const bar = inject("bar");
16+
const two = inject("two");
17+
18+
const count = inject("count");
1219

1320
return {
1421
bar,
22+
two,
23+
count,
1524
};
1625
},
1726
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createTextVNode, h, provide, inject } from "../../lib/m-vue.esm.js";
2+
import Consumer from "./Consumer.js";
3+
4+
export default {
5+
name: "ProviderTwo",
6+
7+
render() {
8+
return h("div", {}, [h("div", {}, "bar: " + this.bar), h(Consumer)]);
9+
},
10+
11+
setup() {
12+
provide("two", "twosss");
13+
provide("bar", "two bar");
14+
const bar = inject("bar");
15+
16+
return {
17+
bar,
18+
};
19+
},
20+
};

example/provide-inject/Provider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { h, provide } from "../../lib/m-vue.esm.js";
2-
import Consumer from "./Consumer.js";
2+
import ProvideTwo from "./ProvideTwo.js";
33

44
export default {
55
name: "Provider",
66

77
render() {
8-
return h("div", {}, [h(Consumer)]);
8+
return h("div", {}, [h(ProvideTwo)]);
99
},
1010

1111
setup() {

lib/m-vue.cjs.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function createComponentInstance(vnode, parent) {
245245
props: {},
246246
slots: {},
247247
parent,
248-
provides: {},
248+
provides: parent ? parent.provides : {},
249249
emit: (instance, event) => { },
250250
};
251251
// 官方Emit
@@ -420,16 +420,20 @@ function renderSlots(slots, renderName, props = {}) {
420420
function provide(key, value) {
421421
const currentInstance = getCurrentInstance();
422422
if (currentInstance) {
423-
mountProvide(currentInstance === null || currentInstance === void 0 ? void 0 : currentInstance.provides, key, value);
423+
let { provides } = currentInstance;
424+
const parentProvides = currentInstance.parent.provides;
425+
// init
426+
if (provides === parentProvides) {
427+
provides = currentInstance.provides = Object.create(parentProvides);
428+
}
429+
provides[key] = value;
424430
}
425431
}
426-
function mountProvide(target, key, value) {
427-
target[key] = value;
428-
}
429432
function inject(key) {
430433
const currentInstance = getCurrentInstance();
431434
if (currentInstance) {
432435
const parentProvides = currentInstance.parent.provides;
436+
console.log(key, parentProvides);
433437
return parentProvides[key];
434438
}
435439
}

lib/m-vue.esm.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ function createComponentInstance(vnode, parent) {
241241
props: {},
242242
slots: {},
243243
parent,
244-
provides: {},
244+
provides: parent ? parent.provides : {},
245245
emit: (instance, event) => { },
246246
};
247247
// 官方Emit
@@ -416,16 +416,20 @@ function renderSlots(slots, renderName, props = {}) {
416416
function provide(key, value) {
417417
const currentInstance = getCurrentInstance();
418418
if (currentInstance) {
419-
mountProvide(currentInstance === null || currentInstance === void 0 ? void 0 : currentInstance.provides, key, value);
419+
let { provides } = currentInstance;
420+
const parentProvides = currentInstance.parent.provides;
421+
// init
422+
if (provides === parentProvides) {
423+
provides = currentInstance.provides = Object.create(parentProvides);
424+
}
425+
provides[key] = value;
420426
}
421427
}
422-
function mountProvide(target, key, value) {
423-
target[key] = value;
424-
}
425428
function inject(key) {
426429
const currentInstance = getCurrentInstance();
427430
if (currentInstance) {
428431
const parentProvides = currentInstance.parent.provides;
432+
console.log(key, parentProvides);
429433
return parentProvides[key];
430434
}
431435
}

src/runtime-core/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function createComponentInstance(vnode, parent) {
2020
props: {},
2121
slots: {},
2222
parent,
23-
provides: {},
23+
provides: parent ? parent.provides : {},
2424
emit: (instance, event) => { },
2525
};
2626

src/runtime-core/inject.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ import { getCurrentInstance } from "./component";
77
export function provide(key, value) {
88
const currentInstance: any = getCurrentInstance();
99
if (currentInstance) {
10-
mountProvide(currentInstance?.provides, key, value)
10+
let { provides } = currentInstance;
11+
const parentProvides = currentInstance.parent.provides;
12+
13+
// init
14+
if (provides === parentProvides) {
15+
provides = currentInstance.provides = Object.create(parentProvides);
16+
}
17+
18+
provides[key] = value;
1119
}
1220
}
13-
function mountProvide(target, key, value) {
14-
target[key] = value;
15-
}
1621

1722
export function inject(key) {
1823
const currentInstance: any = getCurrentInstance();
1924
if (currentInstance) {
2025
const parentProvides = currentInstance.parent.provides;
26+
console.log(key, parentProvides)
2127
return parentProvides[key];
2228
}
2329
}

0 commit comments

Comments
 (0)