Skip to content

Commit a971793

Browse files
committed
refactor: move infinite loop check logic into utils
1 parent d2653cd commit a971793

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

src/components/InfiniteLoading.vue

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
<script>
1717
/* eslint-disable no-console */
1818
import Spinner from './Spinner.vue';
19-
import config, { evt3rdArg, WARNINGS, ERRORS } from '../config';
20-
import { throttleer } from '../utils';
19+
import config, { evt3rdArg, WARNINGS } from '../config';
20+
import { throttleer, loopTracker } from '../utils';
2121
2222
/**
2323
* determine slot is or not a empty element
@@ -42,9 +42,6 @@ export default {
4242
isLoading: false,
4343
isComplete: false,
4444
isFirstLoad: true, // save the current loading whether it is the first loading
45-
infiniteLoopChecked: false, // save the status of infinite loop check
46-
infiniteLoopTimer: null,
47-
continuousCallTimes: 0,
4845
slots: config.slots,
4946
};
5047
},
@@ -213,21 +210,10 @@ export default {
213210
this.$emit('infinite', this.stateChanger);
214211
}
215212
216-
if (isContinuousCall && !this.forceUseInfiniteWrapper && !this.infiniteLoopChecked) {
213+
if (isContinuousCall && !this.forceUseInfiniteWrapper && !loopTracker.isChecked) {
217214
// check this component whether be in an infinite loop if it is not checked
218215
// more details: https://github.com/PeachScript/vue-infinite-loading/issues/55#issuecomment-316934169
219-
this.continuousCallTimes += 1; // save the times of calls
220-
221-
clearTimeout(this.infiniteLoopTimer);
222-
this.infiniteLoopTimer = setTimeout(() => {
223-
this.infiniteLoopChecked = true;
224-
}, config.system.loopCheckTimeout);
225-
226-
// throw warning if the times of continuous calls large than the maximum times
227-
if (this.continuousCallTimes > config.system.loopCheckMaxCalls) {
228-
console.error(ERRORS.INFINITE_LOOP);
229-
this.infiniteLoopChecked = true;
230-
}
216+
loopTracker.track();
231217
}
232218
} else {
233219
this.isLoading = false;

src/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
22

3-
import config from './config';
3+
import config, { ERRORS } from './config';
44

55
export const throttleer = {
66
caches: [],
@@ -18,6 +18,29 @@ export const throttleer = {
1818
},
1919
};
2020

21+
export const loopTracker = {
22+
isChecked: false,
23+
timer: null,
24+
times: 0,
25+
track() {
26+
// record track times
27+
this.times += 1;
28+
29+
// try to mark check status
30+
clearTimeout(this.timer);
31+
this.timer = setTimeout(() => {
32+
this.isChecked = true;
33+
}, config.system.loopCheckTimeout);
34+
35+
// throw warning if the times of continuous calls large than the maximum times
36+
if (this.times > config.system.loopCheckMaxCalls) {
37+
console.error(ERRORS.INFINITE_LOOP);
38+
this.isChecked = true;
39+
}
40+
},
41+
};
42+
2143
export default {
2244
throttleer,
45+
loopTracker,
2346
};

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import Vue from 'vue/dist/vue.common';
44
import { isShow, continuesCall, fakeBox } from '../utils';
55
import config from '../../../src/config';
6+
import { loopTracker } from '../../../src/utils';
67
import InfiniteLoading from '../../../src/components/InfiniteLoading.vue';
78

89
describe('vue-infinite-loading:component', () => {
@@ -497,9 +498,9 @@ describe('vue-infinite-loading:component', () => {
497498
console.error = fakeBox();
498499
// wait for the loop check flag be marked as true
499500
setTimeout(() => {
500-
expect(this.$refs.infiniteLoading.infiniteLoopChecked).to.be.true;
501+
expect(loopTracker.isChecked).to.be.true;
501502
done();
502-
}, 1501);
503+
}, config.system.loopCheckTimeout);
503504
}
504505
},
505506
},

0 commit comments

Comments
 (0)