Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vue_apollo.js « vue3compat « utils « lib « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd08d34a80e6896de549af8e274e71bc2505e0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import Vue from 'vue';
import { createApolloProvider } from '@vue/apollo-option';
import { ApolloMutation } from '@vue/apollo-components';

export { ApolloMutation };

const installed = new WeakMap();

function callLifecycle(hookName, ...extraArgs) {
  const { GITLAB_INTERNAL_ADDED_MIXINS: addedMixins } = this.$;
  if (!addedMixins) {
    return [];
  }

  return addedMixins.map((m) => m[hookName]?.apply(this, extraArgs));
}

function createMixinForLateInit({ install, shouldInstall }) {
  return {
    created() {
      callLifecycle.call(this, 'created');
    },
    // @vue/compat normalizez lifecycle hook names so there is no error here
    destroyed() {
      callLifecycle.call(this, 'unmounted');
    },

    data(...args) {
      const extraData = callLifecycle.call(this, 'data', ...args);
      if (!extraData.length) {
        return {};
      }

      return Object.assign({}, ...extraData);
    },

    beforeCreate() {
      if (shouldInstall(this)) {
        const { mixins } = this.$.appContext;
        const globalMixinsBeforeInit = new Set(mixins);
        install(this);

        this.$.GITLAB_INTERNAL_ADDED_MIXINS = mixins.filter((m) => !globalMixinsBeforeInit.has(m));

        callLifecycle.call(this, 'beforeCreate');
      }
    },
  };
}

export default class VueCompatApollo {
  constructor(...args) {
    // eslint-disable-next-line no-constructor-return
    return createApolloProvider(...args);
  }

  static install() {
    Vue.mixin(
      createMixinForLateInit({
        shouldInstall: (vm) =>
          vm.$options.apolloProvider &&
          !installed.get(vm.$.appContext.app)?.has(vm.$options.apolloProvider),
        install: (vm) => {
          const { app } = vm.$.appContext;
          const { apolloProvider } = vm.$options;

          if (!installed.has(app)) {
            installed.set(app, new WeakSet());
          }

          installed.get(app).add(apolloProvider);

          vm.$.appContext.app.use(vm.$options.apolloProvider);
        },
      }),
    );
  }
}