Skip to content

Commit

Permalink
fix: 修复了mounted生命周期在异步脚本中执行时机错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
bailicangdu committed Oct 18, 2021
1 parent 1f0e2b5 commit 5a02596
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
5 changes: 3 additions & 2 deletions docs/zh-cn/prefetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ app: {
```js
import microApp from '@micro-zoe/micro-app'

// 方式一
microApp.preFetch([
{ name: 'my-app', url: 'xxx' }
])

// 或者
// 方式二
microApp.preFetch(() => [
{ name: 'my-app', url: 'xxx' }
])

// 或者
// 方式三
microApp.start({
preFetchApps: [
{ name: 'my-app', url: 'xxx' }
Expand Down
12 changes: 6 additions & 6 deletions examples/main-react16/src/pages/react16/react16.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ export default class App extends React.Component {
}

handleCreated = () => {
console.log('生命周期:created')
console.log('生命周期:created -- react16')
}

beforemount = (e) => {
console.log('生命周期:beforemount', e)
console.log('生命周期:beforemount -- react16', e)
}

mounted = () => {
console.log('生命周期:mounted', document.querySelector('micro-app'))
console.log('生命周期:mounted -- react16', document.querySelector('micro-app'))
this.setState({
showLoading: false
})
}

unmount = () => {
console.log('生命周期:unmount', document.querySelector('#template-style'))
console.log('生命周期:unmount -- react16', document.querySelector('#template-style'))
}

error = (e) => {
console.log('生命周期:error', e)
console.log('生命周期:error -- react16', e)
}

changeData = () => {
Expand Down Expand Up @@ -147,8 +147,8 @@ export default class App extends React.Component {
onUnmount={this.unmount}
onError={this.error}
onDataChange={this.handleDataChange}
// destory
baseRoute='/micro-app/demo/react16'
// destory
// inline
// disableSandbox
// disableScopecss
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"node-fetch": "~2.6.1",
"npm-run-all": "~4.1.5",
"pretty-bytes": "~5.5.0",
"react": "~17.0.2",
"rollup": "~2.35.1",
"rollup-plugin-terser": "~7.0.2",
"ts-jest": "26.5.6",
Expand Down
11 changes: 10 additions & 1 deletion src/create_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default class CreateApp implements AppInterface {

this.sandBox?.start(this.baseroute)
if (!this.umdHookMount) {
execScripts(this.source.scripts, this, () => {
execScripts(this.source.scripts, this, (isFinished: boolean) => {
if (this.umdHookMount === null) {
const { mount, unmount } = this.getUmdLibraryHooks()
// if mount & unmount is function, the sub app is umd mode
Expand All @@ -151,12 +151,21 @@ export default class CreateApp implements AppInterface {
this.umdHookMount()
}
}
if (isFinished === true) {
this.dispatchMountedEvent()
}
})
} else {
this.sandBox?.rebuildUmdSnapshot()
this.umdHookMount()
this.dispatchMountedEvent()
}
}

/**
* dispatch mounted event when app run finished
*/
dispatchMountedEvent (): void {
if (appStatus.UNMOUNT !== this.status) {
this.status = appStatus.MOUNTED
defer(() => {
Expand Down
7 changes: 4 additions & 3 deletions src/source/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable node/no-callback-literal */
import type {
AppInterface,
sourceScriptInfo,
Expand Down Expand Up @@ -188,13 +189,13 @@ export function execScripts (
const [url, info] = deferScriptInfo[index]
runScript(url, info.code = info.code || code, app, info.module, false, callback)
})
callback()
callback(true)
}).catch((err) => {
logError(err)
callback()
callback(true)
})
} else {
callback()
callback(true)
}
}

Expand Down
1 change: 1 addition & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ declare module '@micro-app/types' {
inline?: boolean,
baseroute?: string,
): void // mount app
dispatchMountedEvent(): void // dispatch mounted event when app run finished
unmount(destory: boolean): void // unmount app
onerror(e: Error): void // app rendering error
getAppStatus(): string // get app status
Expand Down
24 changes: 17 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4773,7 +4773,7 @@ [email protected]:
import-local "^3.0.2"
jest-cli "^26.6.0"

js-tokens@^4.0.0:
"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
Expand Down Expand Up @@ -5142,6 +5142,13 @@ log-update@^4.0.0:
slice-ansi "^4.0.0"
wrap-ansi "^6.2.0"

loose-envify@^1.1.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

lowercase-keys@^1.0.0, lowercase-keys@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.1.tgz#6f9e30b47084d971a7c820ff15a6c5167b74c26f"
Expand Down Expand Up @@ -5547,7 +5554,7 @@ oauth-sign@~0.9.0:
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==

object-assign@^4, object-assign@latest:
object-assign@^4, object-assign@^4.1.1, object-assign@latest:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
Expand Down Expand Up @@ -6091,6 +6098,14 @@ react-is@^17.0.1:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==

react@~17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

read-pkg-up@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be"
Expand Down Expand Up @@ -7376,11 +7391,6 @@ url-parse-lax@^3.0.0:
dependencies:
prepend-http "^2.0.0"

url-polyfill@~1.1.12:
version "1.1.12"
resolved "https://registry.yarnpkg.com/url-polyfill/-/url-polyfill-1.1.12.tgz#6cdaa17f6b022841b3aec0bf8dbd87ac0cd33331"
integrity sha512-mYFmBHCapZjtcNHW0MDq9967t+z4Dmg5CJ0KqysK3+ZbyoNOWQHksGCTWwDhxGXllkWlOc10Xfko6v4a3ucM6A==

use@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"
Expand Down

0 comments on commit 5a02596

Please sign in to comment.