Skip to content

Commit

Permalink
feat(useHydration): add useHydration composable
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed May 31, 2020
1 parent 80e16b5 commit 5af0bc7
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 10 deletions.
7 changes: 7 additions & 0 deletions docs/api/vue-composable.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
```ts
import { ComputedRef } from "@vue/runtime-core";
import { Plugin as Plugin_2 } from "@vue/runtime-core";
import { provide } from "@vue/runtime-core";
import { Ref } from "@vue/runtime-core";
import { UnwrapRef } from "@vue/runtime-core";
Expand Down Expand Up @@ -351,6 +352,9 @@ export function getCssVariableFor(
name: string
): CssVariable;

// @public (undocumented)
export const hydrationPlugin: Plugin_2;

// @public
export interface i18n extends Record<string, i18nMessageValue> {}

Expand Down Expand Up @@ -1214,6 +1218,9 @@ export function useGeolocation(
highAccuracy: Ref<boolean | null>;
};

// @public (undocumented)
export function useHydration(): Readonly<Ref<boolean>>;

// @public
export function useI18n<
T extends i18nDefinition<TMessage>,
Expand Down
3 changes: 3 additions & 0 deletions examples/vue-composable-example/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from "vue";
import VueRouter from "vue-router";
import VueCompositionApi from "@vue/composition-api";
import { hydrationPlugin } from "vue-composable";
import App from "./App.vue";

import HelloWorld from "./components/HelloWorld";
Expand All @@ -13,6 +14,8 @@ import SharedRef from "./components/SharedRef";

Vue.use(VueRouter);
Vue.use(VueCompositionApi);
Vue.use(hydrationPlugin);

window.SuperVue = Vue;

Vue.config.productionTip = false;
Expand Down
14 changes: 13 additions & 1 deletion examples/vue-next-webpack-preview-master/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<img src="./logo.png" />
<h1>Hello Vue 3!</h1>

<p>hydrating: {{ hydrating }}</p>

<ul>
<li
v-for="({ name, use }, i) in components"
Expand Down Expand Up @@ -34,6 +36,8 @@ import HelloWorld from "./components/HelloWorld.vue";
import Swapi from "./components/SWAPI.vue";
import TodoList from "./components/TodoList.vue";
import { ref } from "vue";
import { useHydration } from "vue-composable";
import { onMounted } from "vue";
export default {
components: {
Expand All @@ -48,6 +52,13 @@ export default {
},
setup(props, ctx) {
const hydrating = useHydration();
console.log("setup hydrating", hydrating.value);
onMounted(() => {
console.log("mounted hydrating", hydrating.value);
});
const selected = ref(0);
const components = require
.context("./components", true, /\.(vue)$/)
Expand All @@ -62,7 +73,8 @@ export default {
return {
components,
selected
selected,
hydrating
};
}
};
Expand Down
11 changes: 8 additions & 3 deletions examples/vue-next-webpack-preview-master/src/main.js
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");
2 changes: 1 addition & 1 deletion packages/axios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
"@vue/runtime-core": "^3.0.0-beta.14",
"typescript": "^3.8.3"
}
}
}
2 changes: 1 addition & 1 deletion packages/vue-composable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"@vue/composition-api": "^0.5.0",
"vue": "^2.6.10"
}
}
}
13 changes: 11 additions & 2 deletions packages/vue-composable/src/api.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ export {
computed,
getCurrentInstance
} from "@vue/composition-api";
export { VueConstructor as App } from "vue";

import {
Ref,
watch as vueWatch,
isRef,
set,
getCurrentInstance,
onUnmounted
onUnmounted,
computed
} from "@vue/composition-api";
import Vue from "vue";
import Vue, { PluginFunction } from "vue";

export type Plugin = PluginFunction<any>;

interface WatcherOption {
immediate: boolean;
Expand Down Expand Up @@ -95,3 +99,8 @@ export function watch(source: any, cb: any, options?: any): any {
}
return w;
}

// FAKE readonly
export function readonly<T extends object>(target: T): Readonly<UnwrapRef<T>> {
return computed(() => target) as any;
}
5 changes: 4 additions & 1 deletion packages/vue-composable/src/api.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export {
computed,
getCurrentInstance,
ComputedRef,
UnwrapRef
UnwrapRef,
Plugin,
App,
readonly
} from "@vue/runtime-core";

// istanbul ignore next
Expand Down
5 changes: 4 additions & 1 deletion packages/vue-composable/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export {
computed,
getCurrentInstance,
ComputedRef,
UnwrapRef
UnwrapRef,
Plugin,
App,
readonly
} from "@vue/runtime-core";

// istanbul ignore next
Expand Down
1 change: 1 addition & 0 deletions packages/vue-composable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from "./format";
export * from "./validation";
export * from "./i18n";
export * from "./meta";
export * from "./ssr";

export const VERSION = __VERSION__;
export const VUE_VERSION: "2" | "3" = __VUE_2__ ? "2" : "3";
Expand Down
57 changes: 57 additions & 0 deletions packages/vue-composable/src/ssr/hydration.ts
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)
);
}
1 change: 1 addition & 0 deletions packages/vue-composable/src/ssr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./hydration";

0 comments on commit 5af0bc7

Please sign in to comment.