Skip to content

Commit

Permalink
chore: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Dec 12, 2024
1 parent 36a2670 commit ee7a5ca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
12 changes: 9 additions & 3 deletions packages/client/src/components/Content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ export const Content = defineComponent({

return () =>
h(ContentComponent.value, {
onVnodeMounted: runCallbacks,
onVnodeUpdated: runCallbacks,
onVnodeBeforeUnmount: runCallbacks,
onVnodeMounted: () => {
runCallbacks('mounted')
},
onVnodeUpdated: () => {
runCallbacks('change')
},
onVnodeBeforeUnmount: () => {
runCallbacks('beforeUnmount')
},
})
},
})
31 changes: 31 additions & 0 deletions packages/client/src/composables/contentHooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { onUnmounted } from 'vue'

type LifeCycle = 'beforeUnmount' | 'change' | 'mounted'

const hooks: Record<LifeCycle, (() => unknown)[]> = {
mounted: [],
beforeUnmount: [],
change: [],
}

const createHook =
(lifeCycle: LifeCycle) =>
(fn: () => unknown): void => {
hooks[lifeCycle].push(fn)
onUnmounted(() => {
hooks[lifeCycle] = hooks[lifeCycle].filter((f) => f !== fn)
})
}

export const onContentChange = createHook('change')

export const onContentMounted = createHook('mounted')

export const onContentBeforeUnmount = createHook('beforeUnmount')

/**
* Call all registered callbacks
*/
export const runCallbacks = (lifeCycle: LifeCycle): void => {
hooks[lifeCycle].forEach((fn) => fn())
}
21 changes: 0 additions & 21 deletions packages/client/src/composables/contentUpdated.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/client/src/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './clientData.js'
export * from './clientDataUtils.js'
export * from './updateHead.js'
export * from './contentUpdated.js'
export * from './contentHooks.js'

0 comments on commit ee7a5ca

Please sign in to comment.