Skip to content

Commit e1205d5

Browse files
committed
Improve passive event support logic
1 parent 7c62fac commit e1205d5

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/components/InfiniteLoading.vue

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
</template>
1616
<script>
1717
/* eslint-disable no-console */
18-
/* eslint-disable import/extensions */
1918
import Spinner from './Spinner';
20-
import { supportsPassive } from '../utils/supportsPassive.js';
2119
2220
const LOOP_CHECK_TIMEOUT = 1000; // the timeout for check infinite loop
2321
const LOOP_CHECK_MAX_CALLS = 10; // the maximum number of continuous calls
@@ -59,6 +57,22 @@
5957
'more details: https://github.com/PeachScript/vue-infinite-loading/issues/55#issuecomment-316934169',
6058
].join('\n'),
6159
};
60+
const evt3rdArg = (() => {
61+
let result = false;
62+
63+
try {
64+
const arg = Object.defineProperty({}, 'passive', {
65+
get() {
66+
result = { passive: true };
67+
},
68+
});
69+
70+
window.addEventListener('testpassive', arg, arg);
71+
window.remove('testpassive', arg, arg);
72+
} catch (e) { /* */ }
73+
74+
return result;
75+
})();
6276
6377
export default {
6478
name: 'InfiniteLoading',
@@ -133,7 +147,7 @@
133147
}.bind(this);
134148
135149
setTimeout(this.scrollHandler, 1);
136-
this.scrollParent.addEventListener('scroll', this.scrollHandler, supportsPassive ? { passive: true } : false);
150+
this.scrollParent.addEventListener('scroll', this.scrollHandler, evt3rdArg);
137151
138152
this.$on('$InfiniteLoading:loaded', (ev) => {
139153
this.isFirstLoad = false;
@@ -156,7 +170,7 @@
156170
this.$forceUpdate();
157171
});
158172
159-
this.scrollParent.removeEventListener('scroll', this.scrollHandler, supportsPassive ? { passive: true } : false);
173+
this.scrollParent.removeEventListener('scroll', this.scrollHandler, evt3rdArg);
160174
161175
if (!ev || ev.target !== this) {
162176
console.warn(WARNINGS.STATE_CHANGER);
@@ -168,7 +182,7 @@
168182
this.isComplete = false;
169183
this.isFirstLoad = true;
170184
this.inThrottle = false;
171-
this.scrollParent.addEventListener('scroll', this.scrollHandler, supportsPassive ? { passive: true } : false);
185+
this.scrollParent.addEventListener('scroll', this.scrollHandler, evt3rdArg);
172186
setTimeout(this.scrollHandler, 1);
173187
});
174188
@@ -203,10 +217,10 @@
203217
*/
204218
deactivated() {
205219
this.isLoading = false;
206-
this.scrollParent.removeEventListener('scroll', this.scrollHandler, supportsPassive ? { passive: true } : false);
220+
this.scrollParent.removeEventListener('scroll', this.scrollHandler, evt3rdArg);
207221
},
208222
activated() {
209-
this.scrollParent.addEventListener('scroll', this.scrollHandler, supportsPassive ? { passive: true } : false);
223+
this.scrollParent.addEventListener('scroll', this.scrollHandler, evt3rdArg);
210224
},
211225
methods: {
212226
/**
@@ -291,7 +305,7 @@
291305
},
292306
destroyed() {
293307
if (!this.isComplete) {
294-
this.scrollParent.removeEventListener('scroll', this.scrollHandler, supportsPassive ? { passive: true } : false);
308+
this.scrollParent.removeEventListener('scroll', this.scrollHandler, evt3rdArg);
295309
}
296310
},
297311
};

src/utils/supportsPassive.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)