Skip to content

Commit 8116075

Browse files
committed
perf: optimize logger, disable warn in production
1 parent ea9e18e commit 8116075

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

src/components/InfiniteLoading.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
</div>
2626
</template>
2727
<script>
28-
/* eslint-disable no-console */
2928
import Spinner from './Spinner.vue';
3029
import config, { evt3rdArg, WARNINGS, STATUS } from '../config';
31-
import { throttleer, loopTracker, isBlankSlotElm } from '../utils';
30+
import {
31+
warn, throttleer, loopTracker, isBlankSlotElm,
32+
} from '../utils';
3233
3334
export default {
3435
name: 'InfiniteLoading',
@@ -125,7 +126,7 @@ export default {
125126
}
126127
127128
if (!ev || ev.target !== this) {
128-
console.warn(WARNINGS.STATE_CHANGER);
129+
warn(WARNINGS.STATE_CHANGER);
129130
}
130131
});
131132
@@ -140,7 +141,7 @@ export default {
140141
this.scrollParent.removeEventListener('scroll', this.scrollHandler, evt3rdArg);
141142
142143
if (!ev || ev.target !== this) {
143-
console.warn(WARNINGS.STATE_CHANGER);
144+
warn(WARNINGS.STATE_CHANGER);
144145
}
145146
});
146147
@@ -152,12 +153,12 @@ export default {
152153
setTimeout(this.scrollHandler, 1);
153154
154155
if (!ev || ev.target !== this) {
155-
console.warn(WARNINGS.IDENTIFIER);
156+
warn(WARNINGS.IDENTIFIER);
156157
}
157158
});
158159
159160
if (this.onInfinite) {
160-
console.warn(WARNINGS.INFINITE_EVENT);
161+
warn(WARNINGS.INFINITE_EVENT);
161162
}
162163
163164
/**

src/config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const evt3rdArg = (() => {
6767

6868
export const WARNINGS = {
6969
STATE_CHANGER: [
70-
'[Vue-infinite-loading warn]: emit `loaded` and `complete` event through component instance of `$refs` may cause error, so it will be deprecated soon, please use the `$state` argument instead (`$state` just the special `$event` variable):',
70+
'emit `loaded` and `complete` event through component instance of `$refs` may cause error, so it will be deprecated soon, please use the `$state` argument instead (`$state` just the special `$event` variable):',
7171
'\ntemplate:',
7272
'<infinite-loading @infinite="infiniteHandler"></infinite-loading>',
7373
`
@@ -87,8 +87,8 @@ infiniteHandler($state) {
8787
'',
8888
'more details: https://github.com/PeachScript/vue-infinite-loading/issues/57#issuecomment-324370549',
8989
].join('\n'),
90-
INFINITE_EVENT: '[Vue-infinite-loading warn]: `:on-infinite` property will be deprecated soon, please use `@infinite` event instead.',
91-
IDENTIFIER: '[Vue-infinite-loading warn]: the `reset` event will be deprecated soon, please reset this component by change the `identifier` property.',
90+
INFINITE_EVENT: '`:on-infinite` property will be deprecated soon, please use `@infinite` event instead.',
91+
IDENTIFIER: 'the `reset` event will be deprecated soon, please reset this component by change the `identifier` property.',
9292
};
9393

9494
/**
@@ -97,7 +97,7 @@ infiniteHandler($state) {
9797

9898
export const ERRORS = {
9999
INFINITE_LOOP: [
100-
`[Vue-infinite-loading error]: executed the callback function more than ${system.loopCheckMaxCalls} times for a short time, it looks like searched a wrong scroll wrapper that doest not has fixed height or maximum height, please check it. If you want to force to set a element as scroll wrapper ranther than automatic searching, you can do this:`,
100+
`executed the callback function more than ${system.loopCheckMaxCalls} times for a short time, it looks like searched a wrong scroll wrapper that doest not has fixed height or maximum height, please check it. If you want to force to set a element as scroll wrapper ranther than automatic searching, you can do this:`,
101101
`
102102
<!-- add a special attribute for the real scroll wrapper -->
103103
<div infinite-wrapper>

src/utils.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
import config, { ERRORS } from './config';
44

5+
/**
6+
* console warning in production
7+
* @param {String} msg console content
8+
*/
9+
export function warn(msg) {
10+
/* istanbul ignore else */
11+
if (config.mode !== 'production') {
12+
console.warn(`[Vue-infinite-loading warn]: ${msg}`);
13+
}
14+
}
15+
16+
/**
17+
* console error
18+
* @param {String} msg console content
19+
*/
20+
export function error(msg) {
21+
console.error(`[Vue-infinite-loading error]: ${msg}`);
22+
}
23+
524
export const throttleer = {
625
caches: [],
726
throttle(fn) {
@@ -34,7 +53,7 @@ export const loopTracker = {
3453

3554
// throw warning if the times of continuous calls large than the maximum times
3655
if (this.times > config.system.loopCheckMaxCalls) {
37-
console.error(ERRORS.INFINITE_LOOP);
56+
error(ERRORS.INFINITE_LOOP);
3857
this.isChecked = true;
3958
}
4059
},
@@ -55,6 +74,8 @@ export function isBlankSlotElm(slot) {
5574
}
5675

5776
export default {
77+
warn,
78+
error,
5879
throttleer,
5980
loopTracker,
6081
isBlankSlotElm,

test/unit/specs/InfiniteLoading.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,17 @@ describe('vue-infinite-loading:component', () => {
5858
`;
5959

6060
document.body.appendChild(styles);
61+
62+
// use development mode
63+
config.mode = 'development';
6164
});
6265

6366
after(() => {
6467
// remove global CSS
6568
document.getElementById('testing-style').remove();
69+
70+
// restore app mode
71+
config.mode = Vue.config.productionTip ? 'development' : 'production';
6672
});
6773

6874
beforeEach(() => {

0 commit comments

Comments
 (0)