提交 fad6292a 编写于 作者: fxy060608's avatar fxy060608

fix(mp): support multiple arguments

上级 c54d85e1
...@@ -27,15 +27,20 @@ export function handlePromise(promise: Promise<unknown>) { ...@@ -27,15 +27,20 @@ export function handlePromise(promise: Promise<unknown>) {
} }
export function promisify(name: string, fn: Function) { export function promisify(name: string, fn: Function) {
return (args = {}) => { return (args = {}, ...rest: unknown[]) => {
if (hasCallback(args)) { if (hasCallback(args)) {
return wrapperReturnValue(name, invokeApi(name, fn, args)) return wrapperReturnValue(name, invokeApi(name, fn, args, rest))
} }
return wrapperReturnValue( return wrapperReturnValue(
name, name,
handlePromise( handlePromise(
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
invokeApi(name, fn, extend(args, { success: resolve, fail: reject })) invokeApi(
name,
fn,
extend(args, { success: resolve, fail: reject }),
rest
)
}) })
) )
) )
......
...@@ -115,7 +115,7 @@ export function invokeApi( ...@@ -115,7 +115,7 @@ export function invokeApi(
method: string, method: string,
api: Function, api: Function,
options: object, options: object,
...params: [] params: unknown[]
) { ) {
const interceptor = getApiInterceptorHooks(method) const interceptor = getApiInterceptorHooks(method)
if (interceptor && Object.keys(interceptor).length) { if (interceptor && Object.keys(interceptor).length) {
......
...@@ -8,7 +8,7 @@ export interface MiniProgramCompilerOptions { ...@@ -8,7 +8,7 @@ export interface MiniProgramCompilerOptions {
* 需要延迟渲染的组件,通常是某个组件的某个事件会立刻触发,需要延迟到首次 render 之后,比如微信 editor 的 ready 事件,快手 switch 的 change * 需要延迟渲染的组件,通常是某个组件的某个事件会立刻触发,需要延迟到首次 render 之后,比如微信 editor 的 ready 事件,快手 switch 的 change
*/ */
lazyElement?: { lazyElement?: {
[name: string]: string[] [name: string]: { name: 'on' | 'bind'; arg: string[] }[]
} }
event?: { event?: {
format( format(
......
...@@ -213,16 +213,20 @@ function isLazyElement(node: ElementNode, context: TemplateCodegenContext) { ...@@ -213,16 +213,20 @@ function isLazyElement(node: ElementNode, context: TemplateCodegenContext) {
if (!context.lazyElement) { if (!context.lazyElement) {
return false return false
} }
const events = context.lazyElement[node.tag] const lazyProps = context.lazyElement[node.tag]
return ( if (!lazyProps) {
events && return
node.props.some( }
(prop) => return node.props.some(
prop.type === NodeTypes.DIRECTIVE && (prop) =>
prop.name === 'on' && prop.type === NodeTypes.DIRECTIVE &&
prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION && lazyProps.find((lazyProp) => {
events.includes(prop.arg.content) return (
) prop.name === lazyProp.name &&
prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION &&
lazyProp.arg.includes(prop.arg.content)
)
})
) )
} }
/** /**
......
...@@ -66,13 +66,16 @@ export function promisify(name: string, api: unknown) { ...@@ -66,13 +66,16 @@ export function promisify(name: string, api: unknown) {
if (!isFunction(api)) { if (!isFunction(api)) {
return api return api
} }
return function promiseApi(options: Record<string, any> = {}) { return function promiseApi(
options: Record<string, any> = {},
...rest: unknown[]
) {
if ( if (
isFunction(options.success) || isFunction(options.success) ||
isFunction(options.fail) || isFunction(options.fail) ||
isFunction(options.complete) isFunction(options.complete)
) { ) {
return wrapperReturnValue(name, invokeApi(name, api, options)) return wrapperReturnValue(name, invokeApi(name, api, options, rest))
} }
return wrapperReturnValue( return wrapperReturnValue(
name, name,
...@@ -84,7 +87,8 @@ export function promisify(name: string, api: unknown) { ...@@ -84,7 +87,8 @@ export function promisify(name: string, api: unknown) {
extend({}, options, { extend({}, options, {
success: resolve, success: resolve,
fail: reject, fail: reject,
}) }),
rest
) )
}) })
) )
......
...@@ -147,7 +147,7 @@ const miniProgram = { ...@@ -147,7 +147,7 @@ const miniProgram = {
}, },
directive: 'ks:', directive: 'ks:',
lazyElement: { lazyElement: {
switch: ['change'], switch: [{ name: 'on', arg: ['change'] }],
}, },
}; };
const projectConfigFilename = 'project.config.json'; const projectConfigFilename = 'project.config.json';
......
...@@ -32,7 +32,7 @@ export const miniProgram: MiniProgramCompilerOptions = { ...@@ -32,7 +32,7 @@ export const miniProgram: MiniProgramCompilerOptions = {
}, },
directive: 'ks:', directive: 'ks:',
lazyElement: { lazyElement: {
switch: ['change'], switch: [{ name: 'on', arg: ['change'] }],
}, },
} }
const projectConfigFilename = 'project.config.json' const projectConfigFilename = 'project.config.json'
......
...@@ -11,7 +11,7 @@ describe('mp-weixin: transform component', () => { ...@@ -11,7 +11,7 @@ describe('mp-weixin: transform component', () => {
}` }`
) )
}) })
test('lazy element', () => { test('lazy element: editor', () => {
assert( assert(
`<editor/>`, `<editor/>`,
`<editor/>`, `<editor/>`,
...@@ -24,6 +24,43 @@ describe('mp-weixin: transform component', () => { ...@@ -24,6 +24,43 @@ describe('mp-weixin: transform component', () => {
`<block wx:if="{{r0}}"><editor bindready="{{a}}"/></block>`, `<block wx:if="{{r0}}"><editor bindready="{{a}}"/></block>`,
`(_ctx, _cache) => { `(_ctx, _cache) => {
return { a: _o(_ctx.ready) } return { a: _o(_ctx.ready) }
}`
)
})
test('lazy element: canvas', () => {
assert(
`<canvas/>`,
`<canvas/>`,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`<canvas canvas-id="myCanvas" id="myCanvas"/>`,
`<canvas canvas-id="myCanvas" id="myCanvas"/>`,
`(_ctx, _cache) => {
return {}
}`
)
assert(
`<canvas :id="id"/>`,
`<block wx:if="{{r0}}"><canvas id="{{a}}"/></block>`,
`(_ctx, _cache) => {
return { a: _ctx.id }
}`
)
assert(
`<canvas :canvas-id="id"/>`,
`<block wx:if="{{r0}}"><canvas canvas-id="{{a}}"/></block>`,
`(_ctx, _cache) => {
return { a: _ctx.id }
}`
)
assert(
`<canvas :canvas-id="id" :id="id"/>`,
`<block wx:if="{{r0}}"><canvas canvas-id="{{a}}" id="{{b}}"/></block>`,
`(_ctx, _cache) => {
return { a: _ctx.id, b: _ctx.id }
}` }`
) )
}) })
......
...@@ -236,7 +236,7 @@ function getApiInterceptorHooks(method) { ...@@ -236,7 +236,7 @@ function getApiInterceptorHooks(method) {
} }
return interceptor; return interceptor;
} }
function invokeApi(method, api, options, ...params) { function invokeApi(method, api, options, params) {
const interceptor = getApiInterceptorHooks(method); const interceptor = getApiInterceptorHooks(method);
if (interceptor && Object.keys(interceptor).length) { if (interceptor && Object.keys(interceptor).length) {
if (isArray(interceptor.invoke)) { if (isArray(interceptor.invoke)) {
...@@ -586,17 +586,17 @@ function promisify(name, api) { ...@@ -586,17 +586,17 @@ function promisify(name, api) {
if (!isFunction(api)) { if (!isFunction(api)) {
return api; return api;
} }
return function promiseApi(options = {}) { return function promiseApi(options = {}, ...rest) {
if (isFunction(options.success) || if (isFunction(options.success) ||
isFunction(options.fail) || isFunction(options.fail) ||
isFunction(options.complete)) { isFunction(options.complete)) {
return wrapperReturnValue(name, invokeApi(name, api, options)); return wrapperReturnValue(name, invokeApi(name, api, options, rest));
} }
return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => { return wrapperReturnValue(name, handlePromise(new Promise((resolve, reject) => {
invokeApi(name, api, extend({}, options, { invokeApi(name, api, extend({}, options, {
success: resolve, success: resolve,
fail: reject, fail: reject,
})); }), rest);
}))); })));
}; };
} }
......
...@@ -72,7 +72,8 @@ const miniProgram = { ...@@ -72,7 +72,8 @@ const miniProgram = {
}, },
directive: 'wx:', directive: 'wx:',
lazyElement: { lazyElement: {
editor: ['ready'], canvas: [{ name: 'bind', arg: ['canvas-id', 'id'] }],
editor: [{ name: 'on', arg: ['ready'] }],
}, },
}; };
const projectConfigFilename = 'project.config.json'; const projectConfigFilename = 'project.config.json';
......
...@@ -25,7 +25,8 @@ export const miniProgram: MiniProgramCompilerOptions = { ...@@ -25,7 +25,8 @@ export const miniProgram: MiniProgramCompilerOptions = {
}, },
directive: 'wx:', directive: 'wx:',
lazyElement: { lazyElement: {
editor: ['ready'], canvas: [{ name: 'bind', arg: ['canvas-id', 'id'] }],
editor: [{ name: 'on', arg: ['ready'] }],
}, },
} }
const projectConfigFilename = 'project.config.json' const projectConfigFilename = 'project.config.json'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册