Skip to content

Commit aa86488

Browse files
committed
refactor: use status system instead of multiple flags
1 parent 54e67c6 commit aa86488

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/components/InfiniteLoading.vue

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<template>
22
<div class="infinite-loading-container">
3-
<div v-show="isLoading">
3+
<div v-show="isShowSpinner">
44
<slot name="spinner">
55
<spinner :spinner="spinner" />
66
</slot>
77
</div>
8-
<div class="infinite-status-prompt" v-show="isNoResults">
8+
<div class="infinite-status-prompt" v-show="isShowNoResults">
99
<slot name="no-results">{{ slots.noResults }}</slot>
1010
</div>
11-
<div class="infinite-status-prompt" v-show="isNoMore">
11+
<div class="infinite-status-prompt" v-show="isShowNoMore">
1212
<slot name="no-more">{{ slots.noMore }}</slot>
1313
</div>
1414
</div>
1515
</template>
1616
<script>
1717
/* eslint-disable no-console */
1818
import Spinner from './Spinner.vue';
19-
import config, { evt3rdArg, WARNINGS } from '../config';
19+
import config, { evt3rdArg, WARNINGS, STATUS } from '../config';
2020
import { throttleer, loopTracker, isBlankSlotElm } from '../utils';
2121
2222
export default {
@@ -25,33 +25,33 @@ export default {
2525
return {
2626
scrollParent: null,
2727
scrollHandler: null,
28-
isLoading: false,
29-
isComplete: false,
3028
isFirstLoad: true, // save the current loading whether it is the first loading
29+
status: STATUS.READY,
3130
slots: config.slots,
3231
};
3332
},
3433
components: {
3534
Spinner,
3635
},
3736
computed: {
38-
isNoResults: {
37+
isShowSpinner() {
38+
return this.status === STATUS.LOADING;
39+
},
40+
isShowNoResults: {
3941
cache: false, // disable cache to fix the problem of get slot text delay
4042
get() {
4143
return (
42-
!this.isLoading
43-
&& this.isComplete
44+
this.status === STATUS.COMPLETE
4445
&& this.isFirstLoad
4546
&& !isBlankSlotElm(this.$slots['no-results'])
4647
);
4748
},
4849
},
49-
isNoMore: {
50+
isShowNoMore: {
5051
cache: false, // disable cache to fix the problem of get slot text delay
5152
get() {
5253
return (
53-
!this.isLoading
54-
&& this.isComplete
54+
this.status === STATUS.COMPLETE
5555
&& !this.isFirstLoad
5656
&& !isBlankSlotElm(this.$slots['no-more'])
5757
);
@@ -91,7 +91,7 @@ export default {
9191
}, { immediate: true });
9292
9393
this.scrollHandler = function scrollHandlerOriginal(ev) {
94-
if (!this.isLoading) {
94+
if (this.status === STATUS.READY) {
9595
if (ev && ev.constructor === Event) {
9696
throttleer.throttle(this.attemptLoad);
9797
} else {
@@ -106,7 +106,7 @@ export default {
106106
this.$on('$InfiniteLoading:loaded', (ev) => {
107107
this.isFirstLoad = false;
108108
109-
if (this.isLoading) {
109+
if (this.status === STATUS.LOADING) {
110110
this.$nextTick(this.attemptLoad.bind(null, true));
111111
}
112112
@@ -116,8 +116,7 @@ export default {
116116
});
117117
118118
this.$on('$InfiniteLoading:complete', (ev) => {
119-
this.isLoading = false;
120-
this.isComplete = true;
119+
this.status = STATUS.COMPLETE;
121120
122121
// force re-complation computed properties to fix the problem of get slot text delay
123122
this.$nextTick(() => {
@@ -132,8 +131,7 @@ export default {
132131
});
133132
134133
this.$on('$InfiniteLoading:reset', (ev) => {
135-
this.isLoading = false;
136-
this.isComplete = false;
134+
this.status = STATUS.READY;
137135
this.isFirstLoad = true;
138136
throttleer.reset();
139137
this.scrollParent.addEventListener('scroll', this.scrollHandler, evt3rdArg);
@@ -167,7 +165,10 @@ export default {
167165
* To adapt to keep-alive feature, but only work on Vue 2.2.0 and above, see: https://vuejs.org/v2/api/#keep-alive
168166
*/
169167
deactivated() {
170-
this.isLoading = false;
168+
/* istanbul ignore else */
169+
if (this.status === STATUS.LOADING) {
170+
this.status = STATUS.READY;
171+
}
171172
this.scrollParent.removeEventListener('scroll', this.scrollHandler, evt3rdArg);
172173
},
173174
activated() {
@@ -184,11 +185,11 @@ export default {
184185
const currentDistance = this.getCurrentDistance();
185186
186187
if (
187-
!this.isComplete
188+
this.status !== STATUS.COMPLETE
188189
&& currentDistance <= this.distance
189190
&& (this.$el.offsetWidth + this.$el.offsetHeight) > 0
190191
) {
191-
this.isLoading = true;
192+
this.status = STATUS.LOADING;
192193
193194
if (typeof this.onInfinite === 'function') {
194195
this.onInfinite.call(null, this.stateChanger);
@@ -202,7 +203,7 @@ export default {
202203
loopTracker.track();
203204
}
204205
} else {
205-
this.isLoading = false;
206+
this.status = STATUS.READY;
206207
}
207208
},
208209
/**
@@ -253,7 +254,8 @@ export default {
253254
},
254255
},
255256
destroyed() {
256-
if (!this.isComplete) {
257+
/* istanbul ignore else */
258+
if (!this.status !== STATUS.COMPLETE) {
257259
this.scrollParent.removeEventListener('scroll', this.scrollHandler, evt3rdArg);
258260
}
259261
},

src/config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,21 @@ or
114114
].join('\n'),
115115
};
116116

117+
/**
118+
* plugin status constants
119+
*/
120+
export const STATUS = {
121+
READY: 0,
122+
LOADING: 1,
123+
COMPLETE: 2,
124+
};
125+
117126
export default {
118127
mode: 'development',
119128
props,
120129
system,
121130
slots,
122131
WARNINGS,
123132
ERRORS,
133+
STATUS,
124134
};

0 commit comments

Comments
 (0)