-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useHydration): add useHydration composable
- Loading branch information
Showing
12 changed files
with
111 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { createApp } from 'vue' | ||
import App from './App.vue' | ||
import { createApp } from "vue"; | ||
import App from "./App.vue"; | ||
import { hydrationPlugin } from "vue-composable"; | ||
|
||
createApp(App).mount('#app') | ||
const app = createApp(App); | ||
|
||
app.use(hydrationPlugin); | ||
|
||
app.mount("#app"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,4 +58,4 @@ | |
"@vue/runtime-core": "^3.0.0-beta.14", | ||
"typescript": "^3.8.3" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,4 +46,4 @@ | |
"@vue/composition-api": "^0.5.0", | ||
"vue": "^2.6.10" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
inject, | ||
ref, | ||
InjectionKey, | ||
Ref, | ||
readonly, | ||
computed, | ||
Plugin | ||
} from "../api"; | ||
|
||
// istanbul ignore next | ||
const HYDRATION_KEY: InjectionKey<Readonly< | ||
Ref<Readonly<boolean>> | ||
>> = /*#__PURE__*/ Symbol((__DEV__ && "VUE_COMPOSABLE_HYDRATION_KEY") || ``); | ||
|
||
export const hydrationPlugin: Plugin = { | ||
// @ts-ignore | ||
install(app: any) { | ||
const hydrating = ref(true); | ||
const h = readonly(hydrating); | ||
if (__VUE_2__) { | ||
// TODO add mixin? | ||
if (__DEV__) { | ||
console.warn( | ||
"[hydrationPlugin] HydrationPlugin is not supported in vue2" | ||
); | ||
hydrating.value = false; | ||
} | ||
} else { | ||
// @ts-ignore | ||
app._context.provides[HYDRATION_KEY] = h; | ||
|
||
const appMount = app.mount; | ||
app.mount = (...args: any[]) => { | ||
const component = appMount(...args); | ||
hydrating.value = false; | ||
return component; | ||
}; | ||
} | ||
} | ||
}; | ||
|
||
export function useHydration() { | ||
if (__DEV__) { | ||
const s = Symbol(); | ||
const r = inject(HYDRATION_KEY, s as any); | ||
if (r === s) { | ||
console.warn( | ||
"[useHydration] no hydration found, did you forget to `app.use(HydrationPlugin)`?" | ||
); | ||
} | ||
} | ||
return inject( | ||
HYDRATION_KEY, | ||
computed(() => false) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./hydration"; |