Skip to content

Commit

Permalink
refactor context
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhan committed Feb 23, 2022
1 parent 1619522 commit a75819f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
37 changes: 36 additions & 1 deletion index.js
Expand Up @@ -97,6 +97,41 @@ function getComputedWatcher(name, fn) {
};
}

function getComponentContext(instance) {
return {
el() {
return instance.el;
},
owner() {
return instance.owner;
},
parentComponent() {
return instance.parentComponent;
},
dispatch(...args) {
instance.dispatch.apply(instance, args);
},
fire(...args) {
instance.fire.apply(instance, args);
},
ref(name) {
return instance.ref(name);
},
nextTick(...args) {
instance.nextTick(args);
},
data: {
get(key) {
return instance.data.get(key);
},
set(key, val) {
instance.data.set(key, val);
}
// TODO: 其他方法
}
};
}


/**
* 通过组合式API定义San组件
Expand Down Expand Up @@ -134,7 +169,7 @@ export function defineComponent(creator, san) {
contexts.push(context);

let creatorAsInstance = defineContext.creator;
creatorAsInstance(this);
creatorAsInstance(getComponentContext(this));

contexts.pop();
context = contexts[contexts.length - 1];
Expand Down
16 changes: 8 additions & 8 deletions test/lifeCycle.spec.js
Expand Up @@ -234,10 +234,10 @@ describe('[life cycle]: ', () => {

onConstruct(function (options) {
expect(options.from).toBe('err');
expect(typeof context.template).toBe('string');
expect(context.data).toBeUndefined();
expect(context.scope).toBeUndefined();
expect(context.owner).toBeUndefined();
// expect(typeof context.template).toBe('string');
// expect(context.data).toBeUndefined();
// expect(context.scope).toBeUndefined();
expect(context.owner()).toBeUndefined();
});
});

Expand Down Expand Up @@ -342,12 +342,12 @@ describe('[life cycle]: ', () => {
});

onCreated(function () {
expect(context.el.tagName).toBe('U');
expect(context.el().tagName).toBe('U');
uState.created = 1;
});

onAttached(function () {
expect(context.el.tagName).toBe('U');
expect(context.el().tagName).toBe('U');
uState.attached = 1;
});

Expand Down Expand Up @@ -377,12 +377,12 @@ describe('[life cycle]: ', () => {
})

onCreated(function () {
expect(context.el.tagName).toBe('B');
expect(context.el().tagName).toBe('B');
mainState.created = 1;
})

onAttached(function () {
expect(context.el.tagName).toBe('B');
expect(context.el().tagName).toBe('B');
mainState.attached = 1;
});

Expand Down
6 changes: 3 additions & 3 deletions test/messages.spec.js
Expand Up @@ -48,7 +48,7 @@ describe('[messages]: ', () => {
});

onAttached(function () {
item = context.el;
item = context.el();
context.dispatch('UI:select-item-attached');
});

Expand Down Expand Up @@ -155,7 +155,7 @@ describe('[messages]: ', () => {
});

onAttached(function () {
item = context.el;
item = context.el();
context.dispatch('UI:select-item-attached');
});

Expand Down Expand Up @@ -276,7 +276,7 @@ describe('[messages]: ', () => {
});

onAttached(function () {
item = context.el;
item = context.el();
context.dispatch('UI:select-item-attached');
});

Expand Down

0 comments on commit a75819f

Please sign in to comment.