Skip to content

Commit 1eff53e

Browse files
committed
isWaiting -> is
1 parent dfc3425 commit 1eff53e

File tree

11 files changed

+83
-79
lines changed

11 files changed

+83
-79
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# vue-wait changelog
22

3+
## v1.2.0
4+
- Rename `isWaiting` to `is` to make the code less crowded.
5+
- Better array matching
6+
37
## v1.1.0
48
- Rename package to `vue-wait`
59
- `anyLoading` to `any`

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ You can use this options for customize VueWait behavior.
110110

111111
| Option Name | Type | Default | Description |
112112
| ----------- | ---- | ------- | ----------- |
113-
| `accessorName` | `String` | `"$wait"` | You can change this value to rename the accessor. E.g. if you rename this to `$w`, your `VueWait` methods will be accessible by `$w.isWaiting(..)` etc. |
113+
| `accessorName` | `String` | `"$wait"` | You can change this value to rename the accessor. E.g. if you rename this to `$w`, your `VueWait` methods will be accessible by `$w.waits(..)` etc. |
114114
| `useVuex` | `Boolean` | `false` | Use this value for enabling integration with `Vuex` store. When this value is true `VueWait` will store data in `Vuex` store and all changes to this data will be made by dispatching actions to store |
115115
| `vuexModuleName` | `String` | `"wait"` | Name for `Vuex` store if `useVuex` set to true, otherwise not used. |
116116
| `registerComponent` | `Boolean` | `true` | Registers `v-wait` component. |
@@ -133,13 +133,13 @@ Returns boolean value if any loader exists in page.
133133
</template>
134134
```
135135

136-
#### `.isWaiting(loader String | Matcher)`
136+
#### `.is(loader String | Matcher)`
137137

138138
Returns boolean value if given loader exists in page.
139139

140140
```html
141141
<template>
142-
<progress-bar v-if="$wait.isWaiting('creating user')">Creating User...</progress-bar>
142+
<progress-bar v-if="$wait.is('creating user')">Creating User...</progress-bar>
143143
</template>
144144
```
145145

@@ -149,17 +149,17 @@ Please see [matcher](https://github.com/sindresorhus/matcher/) library to see ho
149149

150150
```html
151151
<template>
152-
<progress-bar v-if="$wait.isWaiting('creating.*')">Creating something...</progress-bar>
152+
<progress-bar v-if="$wait.is('creating.*')">Creating something...</progress-bar>
153153
</template>
154154
```
155155

156-
#### `.isWaiting(loaders Array<String>)`
156+
#### `.is(loaders Array<String>)`
157157

158158
Returns boolean value if some of given loaders exists in page.
159159

160160
```html
161161
<template>
162-
<progress-bar v-if="$wait.isWaiting(['creating user', 'page loading'])">Creating User...</progress-bar>
162+
<progress-bar v-if="$wait.is(['creating user', 'page loading'])">Creating User...</progress-bar>
163163
</template>
164164
```
165165

@@ -362,7 +362,7 @@ In template, you should wrap your content with `v-wait` component to show loadin
362362
Better example for a `button` with loading state:
363363

364364
```html
365-
<button :disabled='$wait.isWaiting("creating user")'>
365+
<button :disabled='$wait.is("creating user")'>
366366
<v-wait for='creating user'>
367367
<template slot='waiting'>Creating User...</template>
368368
Create User

dist/vue-wait.js

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/vuex-example/main.vue

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
This will be shown after load.
99
</v-wait>
1010
</div>
11-
<button @click='writeCode()' :disable='$l.isWaiting("writing code")'>
11+
<button @click='writeCode()' :disable='$l.is("writing code")'>
1212
<v-wait for='writing code' message='Writing Code...'>
1313
<template slot='waiting'>
1414
<span class="animated">️️♻️</span>
1515
</template>
1616
Write Code
1717
</v-wait>
1818
</button>
19-
<button @click='end()' :disabled='!$l.isWaiting("writing code")'>
20-
<span v-if='$l.isWaiting("writing code")'>Stop Coding</span>
19+
<button @click='end()' :disabled='!$l.is("writing code")'>
20+
<span v-if='$l.is("writing code")'>Stop Coding</span>
2121
<span v-else>Coding Stopped</span>
2222
</button>
2323
<br>
24-
<button @click='$l.start("c")' :disabled='$l.isWaiting("c")'>
24+
<button @click='$l.start("c")' :disabled='$l.is("c")'>
2525
Start <b>"c"</b> Loader
2626
</button>
27-
<button @click='$l.end("c")' :disabled='!$l.isWaiting("c")'>
27+
<button @click='$l.end("c")' :disabled='!$l.is("c")'>
2828
Stop <b>"c"</b> Loader
2929
</button>
3030
<p>
@@ -84,7 +84,7 @@
8484
this.$l.end('writing code');
8585
},
8686
toggleLoader(loader) {
87-
if (this.$l.isWaiting(loader)) {
87+
if (this.$l.is(loader)) {
8888
this.$l.end(loader);
8989
} else {
9090
this.$l.start(loader);

examples/wrap-example/main.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
This will be shown after load.
99
</my-waiter>
1010
</div>
11-
<button @click='fetchDataFromApi' :disable='$wait.isWaiting("fetch data")'>
11+
<button @click='fetchDataFromApi' :disable='$wait.is("fetch data")'>
1212
<my-waiter for='fetch data' message='Fetching data...'>
1313
<template slot='waiting'>
1414
<span class="animated">👾</span>
@@ -17,7 +17,7 @@
1717
</my-waiter>
1818
</button>
1919

20-
<button @click='syncCalculator' :disable='$wait.isWaiting("sync work")'>
20+
<button @click='syncCalculator' :disable='$wait.is("sync work")'>
2121
<my-waiter for='sync work' message='Calculating data...'>
2222
<template slot='waiting'>
2323
<span class="animated">❤️</span>

src/components/v-wait.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template lang="html">
22
<div>
3-
<span v-if='isWaiting'>
3+
<span v-if='waitsFor'>
44
<slot name='waiting'></slot>
55
<span v-if='message'>{{ message }}</span>
66
</span>
7-
<slot v-if='!isWaiting'></slot>
7+
<slot v-else></slot>
88
</div>
99
</template>
1010
<script>
@@ -15,12 +15,12 @@
1515
message: String,
1616
},
1717
computed: {
18-
isWaiting() {
18+
waitsFor() {
1919
if (this.visible) {
2020
return this.visible;
2121
}
2222
if (this.for) {
23-
return this.__$waitInstance.isWaiting(this.for);
23+
return this.__$waitInstance.is(this.for);
2424
}
2525
return this.__$waitInstance.any;
2626
}

src/directives/wait.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function bind(el, binding, vNode, oldVNode) {
1616
el.addEventListener(
1717
'click',
1818
() => {
19-
const isWaiting = instance.isWaiting(value);
19+
const isWaiting = instance.is(value);
2020
if (!isWaiting) {
2121
instance.start(value);
2222
} else {
@@ -35,7 +35,7 @@ function update(el, binding, vNode, oldVNode) {
3535
const { arg, modifiers, value } = binding;
3636
const instance = vNode.context.__$waitInstance;
3737

38-
let isWaiting = instance.isWaiting(value);
38+
let isWaiting = instance.is(value);
3939
if (modifiers.not || ['hidden', 'enabled'].includes(arg)) {
4040
isWaiting = !isWaiting;
4141
}

src/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function mapWaitingGetters(getters) {
2626
Object.keys(getters).forEach(getter => {
2727
const waiter = getters[getter];
2828
mappings[getter] = function() {
29-
return this.__$waitInstance.isWaiting(waiter);
29+
return this.__$waitInstance.is(waiter);
3030
};
3131
});
3232
return mappings;

src/utils.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import { isMatch } from 'matcher';
22

3-
export function isWaiting(waiters, waiter) {
3+
function uniqArray(array) {
4+
return array.filter((el, index, arr) => index == arr.indexOf(el));
5+
}
6+
7+
export function is(waitingFor, waiter) {
48
if (typeof waiter === 'string' && waiter.match(/[\*\!]/)) {
5-
return waiters.filter(l => isMatch(l, waiter)).length > 0;
9+
return waitingFor.filter(w => isMatch(w, waiter)).length > 0;
610
}
711
return Array.isArray(waiter)
8-
? waiters.some(v => waiter.includes(v))
9-
: waiters.indexOf(waiter) > -1;
12+
? waitingFor.some(w => is(waiter, w))
13+
: waitingFor.includes(waiter);
1014
}
1115

12-
export function any(waiters) {
13-
return waiters.length > 0;
16+
export function any(waitingFor) {
17+
return waitingFor.length > 0;
1418
}
1519

16-
export function start(waiters, waiter) {
17-
return uniqArray([...waiters, waiter]);
20+
export function start(waitingFor, waiter) {
21+
return uniqArray([...waitingFor, waiter]);
1822
}
1923

20-
export function end(waiters, waiter) {
21-
return uniqArray(waiters).filter(l => l !== waiter);
22-
}
23-
24-
export function uniqArray(array) {
25-
return array.filter((el, index, arr) => index == arr.indexOf(el));
24+
export function end(waitingFor, waiter) {
25+
return uniqArray(waitingFor).filter(l => l !== waiter);
2626
}
2727

2828
export function nodeIsDebug() {

src/vue-wait.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { isWaiting, any, start, end, nodeIsDebug } from './utils';
1+
import { is, any, start, end, nodeIsDebug } from './utils';
22

3+
// Import to export
34
import { mapWaitingActions, mapWaitingGetters, waitFor } from './helpers';
45

56
import vuexStore from './vuex/store';
@@ -59,32 +60,31 @@ export default class VueWait {
5960

6061
this.stateHandler = new Vue({
6162
computed: {
62-
isWaiting: () => loader =>
63-
store.getters[`${vuexModuleName}/isWaiting`](loader),
63+
is: () => waiter => store.getters[`${vuexModuleName}/is`](waiter),
6464
any: () => store.getters[`${vuexModuleName}/any`]
6565
}
6666
});
6767
} else {
6868
this.stateHandler = new Vue({
6969
data() {
7070
return {
71-
activeLoaders: []
71+
waitingFor: []
7272
};
7373
},
7474
computed: {
75-
isWaiting() {
76-
return loader => isWaiting(this.activeLoaders, loader);
75+
is() {
76+
return waiter => is(this.waitingFor, waiter);
7777
},
7878
any() {
79-
return any(this.activeLoaders);
79+
return any(this.waitingFor);
8080
}
8181
},
8282
methods: {
83-
start(loader) {
84-
this.activeLoaders = start(this.activeLoaders, loader);
83+
start(waiter) {
84+
this.waitingFor = start(this.waitingFor, waiter);
8585
},
86-
end(loader) {
87-
this.activeLoaders = end(this.activeLoaders, loader);
86+
end(waiter) {
87+
this.waitingFor = end(this.waitingFor, waiter);
8888
}
8989
}
9090
});
@@ -97,39 +97,39 @@ export default class VueWait {
9797
return this.stateHandler.any;
9898
}
9999

100-
isWaiting(loader) {
101-
return this.stateHandler.isWaiting(loader);
100+
is(waiter) {
101+
return this.stateHandler.is(waiter);
102102
}
103103

104-
dispatchLoaderAction(action, loader) {
104+
dispatchWaitingAction(action, waiter) {
105105
const { vuexModuleName } = this.options;
106-
this.store.dispatch(`${vuexModuleName}/${action}`, loader, {
106+
this.store.dispatch(`${vuexModuleName}/${action}`, waiter, {
107107
root: true
108108
});
109109
}
110110

111-
start(loader) {
111+
start(waiter) {
112112
if (this.options.useVuex) {
113-
this.dispatchLoaderAction('start', loader);
113+
this.dispatchWaitingAction('start', waiter);
114114
return;
115115
}
116-
this.stateHandler.start(loader);
116+
this.stateHandler.start(waiter);
117117
}
118118

119-
end(loader) {
119+
end(waiter) {
120120
if (this.options.useVuex) {
121-
this.dispatchLoaderAction('end', loader);
121+
this.dispatchWaitingAction('end', waiter);
122122
return;
123123
}
124-
this.stateHandler.end(loader);
124+
this.stateHandler.end(waiter);
125125
}
126126
}
127127

128128
export function install(Vue) {
129129
if (install.installed && Vue) {
130130
if (nodeIsDebug()) {
131131
console.warn(
132-
'[vue-wait] already installed. Vue.use(VuexLoading) should be called only once.'
132+
'[vue-wait] already installed. Vue.use(VueWait) should be called only once.'
133133
);
134134
}
135135
return;
@@ -163,7 +163,7 @@ export function install(Vue) {
163163
install.installed = true;
164164
}
165165

166-
// Bypass helpers
166+
// Export which are imported to export
167167
export { mapWaitingActions, mapWaitingGetters, waitFor };
168168

169169
VueWait.install = install;

src/vuex/store.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import { isWaiting, any, start, end } from '../utils';
1+
import { is, any, start, end } from '../utils';
22

33
const mutations = {
4-
START_WAITING: 'START_WAITING',
5-
END_WAITING: 'END_WAITING'
4+
START: 'START',
5+
END: 'END'
66
};
77

88
export default {
99
namespaced: true,
1010
state: {
11-
activeLoaders: []
11+
waitingFor: []
1212
},
1313
getters: {
14-
isWaiting: state => loader => isWaiting(state.activeLoaders, loader),
15-
any: state => any(state.activeLoaders)
14+
is: state => waiter => is(state.waitingFor, waiter),
15+
any: state => any(state.waitingFor)
1616
},
1717
actions: {
18-
start: ({ commit }, loader) => commit(mutations.START_WAITING, loader),
19-
end: ({ commit }, loader) => commit(mutations.END_WAITING, loader)
18+
start: ({ commit }, waiter) => commit(mutations.START, waiter),
19+
end: ({ commit }, waiter) => commit(mutations.END, waiter)
2020
},
2121
mutations: {
22-
[mutations.START_WAITING](state, loader) {
23-
state.activeLoaders = start(state.activeLoaders, loader);
22+
[mutations.START](state, waiter) {
23+
state.waitingFor = start(state.waitingFor, waiter);
2424
},
25-
[mutations.END_WAITING](state, loader) {
26-
state.activeLoaders = end(state.activeLoaders, loader);
25+
[mutations.END](state, waiter) {
26+
state.waitingFor = end(state.waitingFor, waiter);
2727
}
2828
}
2929
};

0 commit comments

Comments
 (0)