UNPKG

6.52 kBJavaScriptView Raw
1/*!
2 * vue-identify-network v2.0.0
3 * Identify the network your users are using!
4 * (c) 2021 Vinayak Kulkarni<inbox.vinayak@gmail.com>
5 * Released under the MIT License
6 */
7
8import VueCompositionApi, { defineComponent, ref, onMounted, onBeforeUnmount } from '@vue/composition-api';
9
10var script = defineComponent({
11 name: 'VueIdentifyNetwork',
12 props: {
13 unknownClass: {
14 type: String ,
15 required: false,
16 default: null,
17 },
18 slowClass: {
19 type: String ,
20 required: false,
21 default: null,
22 },
23 fastClass: {
24 type: String ,
25 required: false,
26 default: null,
27 },
28 },
29 emits: ['network-type', 'network-speed'],
30 setup(_, { emit }) {
31 const type = ref(null);
32 const downLink = ref('Unknown');
33 const vendor = ref(
34 typeof window === 'undefined' ? 'Unknown' : navigator.vendor,
35 );
36
37 onMounted(() => {
38 if (vendor.value.includes('Google') && type.value !== 'Unknown') {
39 type.value = navigator.connection.effectiveType;
40 downLink.value = navigator.connection.downlink;
41 } else {
42 type.value = 'Unknown';
43 downLink.value = 'Unknown';
44 }
45 emit('network-type', type.value);
46 emit('network-speed', downLink.value);
47 navigator.connection.addEventListener('change', updateConnection);
48 });
49
50 onBeforeUnmount(() => {
51 navigator.connection.removeEventListener('change', updateConnection);
52 });
53
54 /**
55 * Updates the type & downLink info
56 *
57 * @param {Evented} e - The type & downlink info
58 */
59 function updateConnection(e) {
60 type.value = e.currentTarget && e.currentTarget.effectiveType;
61 downLink.value = e.currentTarget && e.currentTarget.downlink;
62 emit('network-type', type.value);
63 emit('network-speed', downLink.value);
64 }
65
66 return {
67 type,
68 };
69 },
70});
71
72function normalizeComponent(template, style, script, scopeId, isFunctionalTemplate, moduleIdentifier /* server only */, shadowMode, createInjector, createInjectorSSR, createInjectorShadow) {
73 if (typeof shadowMode !== 'boolean') {
74 createInjectorSSR = createInjector;
75 createInjector = shadowMode;
76 shadowMode = false;
77 }
78 // Vue.extend constructor export interop.
79 const options = typeof script === 'function' ? script.options : script;
80 // render functions
81 if (template && template.render) {
82 options.render = template.render;
83 options.staticRenderFns = template.staticRenderFns;
84 options._compiled = true;
85 // functional template
86 if (isFunctionalTemplate) {
87 options.functional = true;
88 }
89 }
90 // scopedId
91 if (scopeId) {
92 options._scopeId = scopeId;
93 }
94 let hook;
95 if (moduleIdentifier) {
96 // server build
97 hook = function (context) {
98 // 2.3 injection
99 context =
100 context || // cached call
101 (this.$vnode && this.$vnode.ssrContext) || // stateful
102 (this.parent && this.parent.$vnode && this.parent.$vnode.ssrContext); // functional
103 // 2.2 with runInNewContext: true
104 if (!context && typeof __VUE_SSR_CONTEXT__ !== 'undefined') {
105 context = __VUE_SSR_CONTEXT__;
106 }
107 // inject component styles
108 if (style) {
109 style.call(this, createInjectorSSR(context));
110 }
111 // register component module identifier for async chunk inference
112 if (context && context._registeredComponents) {
113 context._registeredComponents.add(moduleIdentifier);
114 }
115 };
116 // used by ssr in case component is cached and beforeCreate
117 // never gets called
118 options._ssrRegister = hook;
119 }
120 else if (style) {
121 hook = shadowMode
122 ? function (context) {
123 style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot));
124 }
125 : function (context) {
126 style.call(this, createInjector(context));
127 };
128 }
129 if (hook) {
130 if (options.functional) {
131 // register for functional component in vue file
132 const originalRender = options.render;
133 options.render = function renderWithStyleInjection(h, context) {
134 hook.call(context);
135 return originalRender(h, context);
136 };
137 }
138 else {
139 // inject component registration as beforeCreate hook
140 const existing = options.beforeCreate;
141 options.beforeCreate = existing ? [].concat(existing, hook) : [hook];
142 }
143 }
144 return script;
145}
146
147/* script */
148const __vue_script__ = script;
149
150/* template */
151var __vue_render__ = function () {
152 var _vm = this;
153 var _h = _vm.$createElement;
154 var _c = _vm._self._c || _h;
155 return _c("div", [
156 _vm.type === "Unknown"
157 ? _c("div", { class: _vm.unknownClass }, [_vm._t("unknown")], 2)
158 : _vm._e(),
159 _vm._v(" "),
160 _vm.type === "2g"
161 ? _c("div", { class: _vm.slowClass }, [_vm._t("slow")], 2)
162 : _vm._e(),
163 _vm._v(" "),
164 _vm.type !== "2g" && _vm.type !== "Unknown"
165 ? _c("div", { class: _vm.fastClass }, [_vm._t("fast")], 2)
166 : _vm._e(),
167 ])
168};
169var __vue_staticRenderFns__ = [];
170__vue_render__._withStripped = true;
171
172 /* style */
173 const __vue_inject_styles__ = undefined;
174 /* scoped */
175 const __vue_scope_id__ = undefined;
176 /* module identifier */
177 const __vue_module_identifier__ = undefined;
178 /* functional template */
179 const __vue_is_functional_template__ = false;
180 /* style inject */
181
182 /* style inject SSR */
183
184 /* style inject shadow dom */
185
186
187
188 const __vue_component__ = /*#__PURE__*/normalizeComponent(
189 { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
190 __vue_inject_styles__,
191 __vue_script__,
192 __vue_scope_id__,
193 __vue_is_functional_template__,
194 __vue_module_identifier__,
195 false,
196 undefined,
197 undefined,
198 undefined
199 );
200
201 var VueIdentifyNetwork = __vue_component__;
202
203let installed = false;
204
205const install = {
206 install(Vue) {
207 if (installed) return;
208 Vue.use(VueCompositionApi);
209 Vue.component('VueIdentifyNetwork', VueIdentifyNetwork);
210 installed = true;
211 },
212};
213
214var install$1 = install;
215
216export { VueIdentifyNetwork, install$1 as default };
217//# sourceMappingURL=vue-identify-network.esm.js.map