Skip to content

Commit 15e8bdf

Browse files
authored
refactor(client): use set to store content updated callbacks (#1636)
1 parent 4161600 commit 15e8bdf

File tree

6 files changed

+15
-16
lines changed

6 files changed

+15
-16
lines changed

e2e/docs/.vuepress/client.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defineClientConfig } from 'vuepress/client'
22
import ComponentForMarkdownGlobal from './components/ComponentForMarkdownGlobal.vue'
3+
import OnContentUpdated from './components/OnContentUpdated.vue'
34
import RootComponentFromUserConfig from './components/RootComponentFromUserConfig.vue'
45

56
// static imported styles file
@@ -13,5 +14,5 @@ export default defineClientConfig({
1314
// dynamic imported styles file
1415
await import('@vuepress-e2e/style-exports')
1516
},
16-
rootComponents: [RootComponentFromUserConfig],
17+
rootComponents: [OnContentUpdated, RootComponentFromUserConfig],
1718
})

e2e/docs/.vuepress/theme/client/components/MarkdownContentHooks.vue e2e/docs/.vuepress/components/OnContentUpdated.vue

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts" setup>
22
import { ref, watch } from 'vue'
3+
import type { ContentUpdatedCallback } from 'vuepress/client'
34
import { onContentUpdated, useRoutePath } from 'vuepress/client'
45
56
const mounted = ref('')
@@ -14,7 +15,7 @@ watch(routePath, () => {
1415
updatedCount.value = 0
1516
})
1617
17-
onContentUpdated((reason) => {
18+
const callback: ContentUpdatedCallback = (reason) => {
1819
switch (reason) {
1920
case 'mounted':
2021
mounted.value = routePath.value
@@ -28,7 +29,12 @@ onContentUpdated((reason) => {
2829
break
2930
default:
3031
}
31-
})
32+
}
33+
34+
onContentUpdated(callback)
35+
36+
// should not be triggered twice
37+
onContentUpdated(callback)
3238
</script>
3339

3440
<template>

e2e/docs/.vuepress/theme/client/layouts/Layout.vue

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script setup lang="ts">
22
import { Content, useSiteData } from 'vuepress/client'
3-
import MarkdownContentHooks from '../components/MarkdownContentHooks.vue'
43
54
const siteData = useSiteData()
65
</script>
@@ -19,8 +18,6 @@ const siteData = useSiteData()
1918
<main class="e2e-theme-content">
2019
<Content />
2120
</main>
22-
23-
<MarkdownContentHooks />
2421
</div>
2522
</template>
2623

packages/client/src/components/Content.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { ContentUpdatedReason } from '../types/index.js'
1010
* @internal
1111
*/
1212
const runContentUpdatedCallbacks = (reason: ContentUpdatedReason): void => {
13-
contentUpdatedCallbacks.value.forEach((fn) => fn(reason))
13+
contentUpdatedCallbacks.forEach((fn) => fn(reason))
1414
}
1515

1616
/**

packages/client/src/composables/onContentUpdated.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import type { ContentUpdatedCallback } from '../types/index.js'
77
* in the DOM.
88
*/
99
export const onContentUpdated = (fn: ContentUpdatedCallback): void => {
10-
contentUpdatedCallbacks.value.push(fn)
10+
contentUpdatedCallbacks.add(fn)
1111
onUnmounted(() => {
12-
contentUpdatedCallbacks.value = contentUpdatedCallbacks.value.filter(
13-
(f) => f !== fn,
14-
)
12+
contentUpdatedCallbacks.delete(fn)
1513
})
1614
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import type { Ref } from 'vue'
2-
import { shallowRef } from 'vue'
31
import type { ContentUpdatedCallback } from '../types/index.js'
42

53
/**
6-
* Global content updated callbacks ref
4+
* Global content updated callbacks
75
*/
8-
export const contentUpdatedCallbacks: Ref<ContentUpdatedCallback[]> =
9-
shallowRef([])
6+
export const contentUpdatedCallbacks = new Set<ContentUpdatedCallback>()

0 commit comments

Comments
 (0)