Skip to content

Commit d15e61d

Browse files
committed
Level 1: defer component initialization for script modules
1 parent b9bd32f commit d15e61d

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

‎js/features/supportJsModules.js‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { on } from '@/hooks'
22
import { getModuleUrl } from '@/utils'
3+
import Alpine from 'alpinejs'
34

45
let pendingComponentAssets = new WeakMap()
56

@@ -14,18 +15,30 @@ on('effect', ({ component, effects }) => {
1415
let encodedName = component.name.replace(/\./g, '--').replace(/::/g, '---').replace(/:/g, '----')
1516
let path = `${getModuleUrl()}/js/${encodedName}.js?v=${scriptModuleHash}`
1617

17-
pendingComponentAssets.set(component, Alpine.reactive({
18+
let pendingAsset = Alpine.reactive({
1819
loading: true,
1920
afterLoaded: [],
20-
}))
21+
})
2122

22-
import(/* @vite-ignore */ path).then(module => {
23-
module.run.call(component.$wire, component.$wire, component.$wire.js);
23+
pendingComponentAssets.set(component, pendingAsset)
2424

25-
pendingComponentAssets.get(component).loading = false
26-
pendingComponentAssets.get(component).afterLoaded.forEach(callback => callback())
27-
pendingComponentAssets.delete(component)
28-
});
25+
let promise = import(/* @vite-ignore */ path).then(module => {
26+
module.run.call(component.$wire, component.$wire, component.$wire.js)
27+
})
28+
29+
let finish = () => {
30+
pendingAsset.loading = false
31+
32+
if (pendingComponentAssets.get(component) === pendingAsset) {
33+
pendingComponentAssets.delete(component)
34+
}
35+
36+
let callbacks = pendingAsset.afterLoaded.splice(0)
37+
38+
callbacks.forEach(callback => callback())
39+
}
40+
41+
Alpine.deferInit(component.el, promise.finally(finish))
2942
}
3043
})
3144

@@ -39,4 +52,4 @@ export function runAfterAssetIsLoadedFor(component, callback) {
3952
} else {
4053
callback()
4154
}
42-
}
55+
}

0 commit comments

Comments
 (0)