Advertisement
Guest User

koine_neo diff

a guest
May 17th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.64 KB | None | 0 0
  1. diff --git a/src/client-entry.ts b/src/client-entry.ts
  2. index a2143df..bcac00f 100644
  3. --- a/src/client-entry.ts
  4. +++ b/src/client-entry.ts
  5. @@ -1,12 +1,53 @@
  6.  import { createApp } from "./main";
  7. +import Vue from "vue";
  8. +import {NavigationGuard, Route} from "vue-router";
  9.  
  10.  const { app, router, store } = createApp();
  11.  
  12. +Vue.mixin({
  13. +    beforeRouteUpdate: <NavigationGuard> ((to, from, next) => {
  14. +        // @ts-ignore
  15. +        const { asyncData } = this.$options;
  16. +        if (asyncData) {
  17. +            asyncData({
  18. +                // @ts-ignore
  19. +                store: this.$store,
  20. +                route: to
  21. +            }).then(next).catch(next)
  22. +        } else {
  23. +            next()
  24. +        }
  25. +    })
  26. +});
  27. +
  28. +
  29.  // @ts-ignore
  30.  if (window.__INITIAL_STATE__) {
  31.      // @ts-ignore
  32.      store.replaceState(window.__INITIAL_STATE__);
  33.  }
  34.  
  35. -app.$mount("#app");
  36. -console.log("App mounted.");
  37. \ No newline at end of file
  38. +router.onReady(() => {
  39. +    router.beforeResolve(<NavigationGuard>((to, from, next) => {
  40. +        const matched = router.getMatchedComponents(to);
  41. +        const prevMatched = router.getMatchedComponents(from);
  42. +        let diffed = false;
  43. +        const activated = matched.filter((c: any, i: number) => {
  44. +            return diffed || (diffed = (prevMatched[i] !== c))
  45. +        });
  46. +
  47. +        const asyncDataHooks = activated.map((c: any) => c.asyncData).filter((_: any) => _);
  48. +        if (!asyncDataHooks.length) {
  49. +            return next();
  50. +        }
  51. +
  52. +        Promise.all(asyncDataHooks.map((hook: any) => hook({store, route: to})))
  53. +            .then(() => {
  54. +                next()
  55. +            })
  56. +            .catch(next);
  57. +    }));
  58. +
  59. +    app.$mount("#app");
  60. +    console.log("App mounted.");
  61. +});
  62. \ No newline at end of file
  63. diff --git a/src/components/Home.vue b/src/components/Home.vue
  64. index 6e41c8d..39f564f 100644
  65. --- a/src/components/Home.vue
  66. +++ b/src/components/Home.vue
  67. @@ -60,6 +60,8 @@
  68.      import {GET_TOP_3} from "../store";
  69.      import Vue from "vue";
  70.      import Component from "vue-class-component";
  71. +    import {Store} from "vuex";
  72. +    import {Route} from "vue-router";
  73.  
  74.      @Component({
  75.          // @ts-ignore
  76. @@ -76,16 +78,20 @@
  77.              return this.$store.state.ranks;
  78.          }
  79.  
  80. -        fetchRanks(): Promise<void> {
  81. -            return this.$store.dispatch(GET_TOP_3);
  82. +        static fetchRanks(store: Store<any>) {
  83. +            return store.dispatch(GET_TOP_3);
  84.          }
  85.  
  86. -        serverPrefetch(): Promise<void> {
  87. -            return this.fetchRanks();
  88. +        static asyncData({ store, route }: { store: Store<any>, route: Route}) {
  89. +            return Home.fetchRanks(store);
  90.          }
  91.  
  92. -        mounted(): void {
  93. -            this.fetchRanks();
  94. +        beforeMount() {
  95. +            // @ts-ignore
  96. +            if (this.ranks.length === 0) {
  97. +                console.error("server side ranks is empty");
  98. +                Home.fetchRanks(this.$store);
  99. +            }
  100.          }
  101.      }
  102.  </script>
  103. diff --git a/src/components/Topics.vue b/src/components/Topics.vue
  104. index c74b249..a9a3c7f 100644
  105. --- a/src/components/Topics.vue
  106. +++ b/src/components/Topics.vue
  107. @@ -31,42 +31,47 @@
  108.  <script lang="ts">
  109.      import {GET_TOPICS} from "../store";
  110.      import PinkPlanetButton from "./PinkPlanetButton.vue";
  111. +    import Component from "vue-class-component";
  112. +    import Vue from "vue";
  113. +    import {AsyncParams} from "../AsyncParams";
  114. +    import {Store} from "vuex";
  115.  
  116. -    export default {
  117. -        name: "Topics",
  118. +    @Component({
  119. +        // @ts-ignore
  120. +        components: {PinkPlanetButton}
  121. +    })
  122. +    export default class Topics extends Vue {
  123. +        name: string = "Topics";
  124.  
  125. -        components: {PinkPlanetButton},
  126. -
  127. -        data: function () {
  128. +        static data() {
  129.              return {
  130.                  planetDefaultImage: "/assets/planet.png",
  131.                  planetHoverImage: "/assets/planet_full.png"
  132.              }
  133. -        },
  134. +        }
  135.  
  136. -        computed: {
  137. -            topics(this: any): [Topic] {
  138. -                return this.$store.state.topics;
  139. -            }
  140. -        },
  141. +        get topics(this: any): [Topic] {
  142. +            return this.$store.state.topics;
  143. +        }
  144.  
  145. -        methods: {
  146. -            fetchTopics(this: any): Promise<void> {
  147. -                return this.$store.dispatch(GET_TOPICS);
  148. -            },
  149. +        convertDate(date: Date): string {
  150. +            const month = date.toLocaleString('it-it', { month: 'long' });
  151. +            return month + " " + date.getFullYear();
  152. +        }
  153.  
  154. -            convertDate(date: Date): string {
  155. -                const month = date.toLocaleString('it-it', { month: 'long' });
  156. -                return month + " " + date.getFullYear();
  157. -            }
  158. -        },
  159. +        static fetchTopics(store: Store<any>): Promise<void> {
  160. +            return store.dispatch(GET_TOPICS);
  161. +        }
  162.  
  163. -        serverPrefetch(this: any): Promise<any> {
  164. -            return this.fetchTopics();
  165. -        },
  166. +        static asyncData({ store, route }: AsyncParams) {
  167. +            Topics.fetchTopics(store);
  168. +        }
  169.  
  170. -        mounted(this: any): void {
  171. -            this.fetchTopics();
  172. +        beforeMount() {
  173. +            // @ts-ignore
  174. +            if (this.topics.length === 0) {
  175. +                Topics.fetchTopics(this.$store);
  176. +            }
  177.          }
  178.      }
  179.  </script>
  180. diff --git a/src/server_entry.ts b/src/server_entry.ts
  181. index 5756121..9f2f02f 100644
  182. --- a/src/server_entry.ts
  183. +++ b/src/server_entry.ts
  184. @@ -5,7 +5,8 @@ export default (context: any) => {
  185.      // we will be returning a Promise so that the server can wait until
  186.      // everything is ready before rendering.
  187.      return new Promise((resolve, reject) => {
  188. -        const { app, router, store } = createApp();
  189. +        const {app, router, store} = createApp();
  190. +        const s = Date.now();
  191.  
  192.          // set server-side router's location
  193.          router.push(context.url);
  194. @@ -15,14 +16,17 @@ export default (context: any) => {
  195.              const matchedComponents = router.getMatchedComponents();
  196.              // no matched routes, reject with 404
  197.              if (!matchedComponents.length) {
  198. -                return reject({ code: 404, url: context.url });
  199. +                return reject({code: 404, url: context.url});
  200.              }
  201.  
  202. -            context.rendered = () => {
  203. +            Promise.all(matchedComponents.map(({ asyncData }: any) => asyncData && asyncData({
  204. +                store,
  205. +                route: router.currentRoute
  206. +            }))).then(() => {
  207. +                console.log(`data pre-fetch: ${Date.now() - s}ms`);
  208.                  context.state = store.state;
  209. -            };
  210. -
  211. -            resolve(app);
  212. -        }, reject);
  213. +                resolve(app)
  214. +            }).catch(reject);
  215. +        });
  216.      });
  217.  }
  218. \ No newline at end of file
  219. diff --git a/src/store.ts b/src/store.ts
  220. index 05735dc..c478c5e 100644
  221. --- a/src/store.ts
  222. +++ b/src/store.ts
  223. @@ -11,11 +11,9 @@ export const SET_TOPICS = "setTopics";
  224.  export const GET_TOPIC_INFO = "getTopicInfo";
  225.  export const SET_TOPIC_INFO = "setTopicInfo";
  226.  
  227. -const DEFAULT_RANK: RankedComment = {"id":"","upload_date":new Date(),"audio_format":"","author":{"user_type":"podcaster","id":"","department":"","username":""},"stars":0,"parent_topic":{"id":"","title":"","creation_date": new Date(),"description":null},"parent_podcast_author":{"user_type":"podcaster","id":"","department":"","username":""}};
  228. -
  229.  // FIXME: Something's wrong with Vuex initial state injection.
  230.  const state = {
  231. -    ranks: [DEFAULT_RANK, DEFAULT_RANK, DEFAULT_RANK],
  232. +    ranks: [],
  233.      topics: [],
  234.      topic_infos: {}
  235.  };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement