Skip to content

Commit 9697f42

Browse files
author
lishiwen
committed
feat: getCurrentInstance bundle
1 parent fc315e1 commit 9697f42

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/m-vue.cjs.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ var instanceRuntimeExtendApis = /*#__PURE__*/Object.freeze({
235235
promiseEmit: promiseEmit
236236
});
237237

238+
let currentInstance = null;
238239
function createComponentInstance(vnode) {
239240
// instance
240241
const component = {
@@ -262,10 +263,14 @@ function setupStatefulComponent(instance) {
262263
const context = { _instance: instance, _component: Component };
263264
instance.proxy = new Proxy(context, ComponentPublicInstanceHandlers);
264265
if (Component.setup) {
266+
// NOTE: getCurrentInstance() API 只能在 setup() 中调用
267+
// 并且针对于每个组件都会拥有自己的组件实例
268+
setCurrentInstance(instance);
265269
// props is readonly because it's a Reflect instance
266270
const readonlyProps = shallowReadonly(instance.props);
267271
// TODO: context
268272
const setupResult = Component.setup(readonlyProps, Object.assign({ emit: instance.emit }, instanceRuntimeExtendApis));
273+
clearCurrentInstance();
269274
handleSetupResult(instance, setupResult);
270275
}
271276
}
@@ -281,6 +286,15 @@ function finishComponentSetup(instance) {
281286
instance.render = Component.render;
282287
// }
283288
}
289+
function getCurrentInstance() {
290+
return currentInstance;
291+
}
292+
function setCurrentInstance(instance) {
293+
currentInstance = instance;
294+
}
295+
function clearCurrentInstance() {
296+
currentInstance = null;
297+
}
284298

285299
function render(vnode, container) {
286300
patch(vnode, container);
@@ -397,12 +411,15 @@ function renderSlots(slots, renderName, props = {}) {
397411
}
398412
}
399413

414+
exports.clearCurrentInstance = clearCurrentInstance;
400415
exports.createApp = createApp;
401416
exports.createComponentInstance = createComponentInstance;
402417
exports.createTextVNode = createTextVNode;
403418
exports.createVNode = createVNode;
419+
exports.getCurrentInstance = getCurrentInstance;
404420
exports.h = h;
405421
exports.patch = patch;
406422
exports.render = render;
407423
exports.renderSlots = renderSlots;
424+
exports.setCurrentInstance = setCurrentInstance;
408425
exports.setupComponent = setupComponent;

lib/m-vue.esm.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ var instanceRuntimeExtendApis = /*#__PURE__*/Object.freeze({
231231
promiseEmit: promiseEmit
232232
});
233233

234+
let currentInstance = null;
234235
function createComponentInstance(vnode) {
235236
// instance
236237
const component = {
@@ -258,10 +259,14 @@ function setupStatefulComponent(instance) {
258259
const context = { _instance: instance, _component: Component };
259260
instance.proxy = new Proxy(context, ComponentPublicInstanceHandlers);
260261
if (Component.setup) {
262+
// NOTE: getCurrentInstance() API 只能在 setup() 中调用
263+
// 并且针对于每个组件都会拥有自己的组件实例
264+
setCurrentInstance(instance);
261265
// props is readonly because it's a Reflect instance
262266
const readonlyProps = shallowReadonly(instance.props);
263267
// TODO: context
264268
const setupResult = Component.setup(readonlyProps, Object.assign({ emit: instance.emit }, instanceRuntimeExtendApis));
269+
clearCurrentInstance();
265270
handleSetupResult(instance, setupResult);
266271
}
267272
}
@@ -277,6 +282,15 @@ function finishComponentSetup(instance) {
277282
instance.render = Component.render;
278283
// }
279284
}
285+
function getCurrentInstance() {
286+
return currentInstance;
287+
}
288+
function setCurrentInstance(instance) {
289+
currentInstance = instance;
290+
}
291+
function clearCurrentInstance() {
292+
currentInstance = null;
293+
}
280294

281295
function render(vnode, container) {
282296
patch(vnode, container);
@@ -393,4 +407,4 @@ function renderSlots(slots, renderName, props = {}) {
393407
}
394408
}
395409

396-
export { createApp, createComponentInstance, createTextVNode, createVNode, h, patch, render, renderSlots, setupComponent };
410+
export { clearCurrentInstance, createApp, createComponentInstance, createTextVNode, createVNode, getCurrentInstance, h, patch, render, renderSlots, setCurrentInstance, setupComponent };

0 commit comments

Comments
 (0)