1- import type { ShallowRef } from 'vue'
21import type { ModuleReplacement } from 'module-replacements'
32
43async function fetchReplacements (
5- deps : Record < string , string > | undefined ,
6- replacements : ShallowRef < Record < string , ModuleReplacement > > ,
7- ) {
8- if ( ! deps || Object . keys ( deps ) . length === 0 ) {
9- replacements . value = { }
10- return
11- }
12-
4+ deps : Record < string , string > ,
5+ ) : Promise < Record < string , ModuleReplacement > > {
136 const names = Object . keys ( deps )
147
158 const results = await Promise . all (
@@ -29,7 +22,7 @@ async function fetchReplacements(
2922 map [ name ] = replacement
3023 }
3124 }
32- replacements . value = map
25+ return map
3326}
3427
3528/**
@@ -40,12 +33,27 @@ export function useReplacementDependencies(
4033 dependencies : MaybeRefOrGetter < Record < string , string > | undefined > ,
4134) {
4235 const replacements = shallowRef < Record < string , ModuleReplacement > > ( { } )
36+ let generation = 0
4337
4438 if ( import . meta. client ) {
4539 watch (
4640 ( ) => toValue ( dependencies ) ,
47- deps => {
48- fetchReplacements ( deps , replacements ) . catch ( ( ) => { } )
41+ async deps => {
42+ const currentGeneration = ++ generation
43+
44+ if ( ! deps || Object . keys ( deps ) . length === 0 ) {
45+ replacements . value = { }
46+ return
47+ }
48+
49+ try {
50+ const result = await fetchReplacements ( deps )
51+ if ( currentGeneration === generation ) {
52+ replacements . value = result
53+ }
54+ } catch {
55+ // catastrophic failure, just keep whatever we have
56+ }
4957 } ,
5058 { immediate : true } ,
5159 )
0 commit comments