Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复 Vue 写法的 MIP 组件的依赖项会重复执行两次的 bug #742

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 45 additions & 34 deletions packages/mip/src/services/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,37 +245,6 @@ export class Extensions {
this.currentExtensionId = extensionId
factory(...args)

/**
* This extension needs `mip-vue` service.
*/
if (
document.documentElement.hasAttribute('mip-vue') &&
!Services.getServiceOrNull('mip-vue')
) {
/**
* Inserts script of `mip-vue` service if needed.
*/
if (!document.querySelector('script[src*="mip-vue.js"]')) {
/* istanbul ignore if */
if (process.env.NODE_ENV === 'production') {
insertScript(`https://c.mipcdn.com/static/v2/mip-vue.js`)
} else {
const baseUrl = document.querySelector('script[src*="mip.js"]').src.replace(/\/[^/]+$/, '')

insertScript(`${baseUrl}/mip-vue.js`)
}
}

/**
* Interrupts current registration.
* Reregisters this extension while `mip-vue` service is loaded.
*/
Services.getServicePromise('mip-vue')
.then(() => this.registerExtension(extensionId, factory, ...args))

return
}

/**
* It still possible that all element instances in current extension call lifecycle `build` synchronously.
* Executes callback in microtask to make sure all these elements are built.
Expand All @@ -290,6 +259,29 @@ export class Extensions {
}
}

/**
* load mip-vue script
*/
loadVueScript () {
if (
document.documentElement.hasAttribute('mip-vue') &&
!Services.getServiceOrNull('mip-vue')
) {
/**
* Inserts script of `mip-vue` service if needed.
*/
if (!document.querySelector('script[src*="mip-vue.js"]')) {
/* istanbul ignore if */
if (process.env.NODE_ENV === 'production') {
insertScript(`https://c.mipcdn.com/static/v2/mip-vue.js`)
} else {
const baseUrl = document.querySelector('script[src*="mip.js"]').src.replace(/\/[^/]+$/, '')
insertScript(`${baseUrl}/mip-vue.js`)
}
}
}
}

/**
* To see if all elements registered in current extension are built.
*
Expand Down Expand Up @@ -380,12 +372,31 @@ export class Extensions {
holder.extension.elements[name] = element
}

const registrator = this.getElementRegistrator(element)

let registrator = this.getElementRegistrator(element)
// only vue registrator would return undefined
// so reload vue script and get registrator again
if (!registrator) {
return
this.loadVueScript()
Services.getServicePromise('mip-vue').then(() => {
registrator = this.getElementRegistrator(element)
this.execRegisterElement(registrator, name, implementation, css, holder)
})
} else {
this.execRegisterElement(registrator, name, implementation, css, holder)
}

}

/**
* using registrator to execute registration
*
* @param {Function} registrator MIP Component registrator
* @param {string} name Component name
* @param {!Object | !Object} implementation implementation
* @param {string=} css css string
* @param {Object} holder component holder
*/
execRegisterElement (registrator, name, implementation, css, holder) {
/** @type {?HTMLElement[]} */
let elementInstances = registrator(name, implementation, css)

Expand Down
11 changes: 7 additions & 4 deletions packages/mip/test/specs/mip1-polyfill/element.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,13 @@ describe('mip1 element', function () {
let node = createDomByTag(name)
node.setAttribute('name', 'MIP')

return extensions.waitForExtension(name).then(function () {
document.body.removeChild(node)
expect(queue).to.deep.equal(['createdCallback', 'attachedCallback', 'build', 'detachedCallback'])
})
return extensions.waitForExtension(name)
.then(function () {
document.body.removeChild(node)
})
.then(function () {
expect(queue).to.deep.equal(['createdCallback', 'attachedCallback', 'build', 'detachedCallback'])
})
})
})

Expand Down