perf(@vant/lazyload): reuse some utils (#9591)

This commit is contained in:
neverland 2021-09-29 18:11:04 +08:00 committed by GitHub
parent ec5067ce7d
commit 95ffe9bc19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 103 deletions

View File

@ -25,6 +25,9 @@
"@babel/core": "^7.12.9", "@babel/core": "^7.12.9",
"release-it": "^14.2.2" "release-it": "^14.2.2"
}, },
"dependencies": {
"@vant/use": "^1.3.1"
},
"release-it": { "release-it": {
"git": { "git": {
"tag": false, "tag": false,

View File

@ -1,5 +1,5 @@
import { h } from 'vue'; import { h } from 'vue';
import { inBrowser } from './util'; import { inBrowser } from '@vant/use';
export default (lazy) => ({ export default (lazy) => ({
props: { props: {

View File

@ -1,5 +1,6 @@
/* eslint-disable max-classes-per-file */ /* eslint-disable max-classes-per-file */
import { find, remove } from './util'; /* eslint-disable prefer-object-spread */
import { remove } from './util';
const defaultOptions = { const defaultOptions = {
selector: 'img', selector: 'img',
@ -74,13 +75,13 @@ export default class LazyContainerManager {
} }
update(el, binding, vnode) { update(el, binding, vnode) {
const container = find(this._queue, (item) => item.el === el); const container = this._queue.find((item) => item.el === el);
if (!container) return; if (!container) return;
container.update({ el, binding, vnode }); container.update({ el, binding, vnode });
} }
unbind(el) { unbind(el) {
const container = find(this._queue, (item) => item.el === el); const container = this._queue.find((item) => item.el === el);
if (!container) return; if (!container) return;
container.clear(); container.clear();
remove(this._queue, container); remove(this._queue, container);

View File

@ -1,4 +1,5 @@
import { inBrowser, loadImageAsync, noop } from './util'; import { inBrowser } from '@vant/use';
import { loadImageAsync, noop } from './util';
export default (lazyManager) => ({ export default (lazyManager) => ({
props: { props: {

View File

@ -1,13 +1,12 @@
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import { inBrowser, getScrollParent } from '@vant/use';
import { import {
inBrowser,
remove, remove,
find, on,
_, off,
throttle, throttle,
supportWebp, supportWebp,
getDPR, getDPR,
scrollParent,
getBestSelectionFromSrcset, getBestSelectionFromSrcset,
isObject, isObject,
hasIntersectionObserver, hasIntersectionObserver,
@ -147,7 +146,7 @@ export default function () {
} }
if (!$parent) { if (!$parent) {
$parent = scrollParent(el); $parent = getScrollParent(el);
} }
const newListener = new ReactiveListener({ const newListener = new ReactiveListener({
@ -186,7 +185,7 @@ export default function () {
let { src } = value; let { src } = value;
src = getBestSelectionFromSrcset(el, this.options.scale) || src; src = getBestSelectionFromSrcset(el, this.options.scale) || src;
const exist = find(this.ListenerQueue, (item) => item.el === el); const exist = this.ListenerQueue.find((item) => item.el === el);
if (!exist) { if (!exist) {
this.add(el, binding, vnode); this.add(el, binding, vnode);
} else { } else {
@ -212,7 +211,7 @@ export default function () {
remove(el) { remove(el) {
if (!el) return; if (!el) return;
this._observer && this._observer.unobserve(el); this._observer && this._observer.unobserve(el);
const existItem = find(this.ListenerQueue, (item) => item.el === el); const existItem = this.ListenerQueue.find((item) => item.el === el);
if (existItem) { if (existItem) {
this._removeListenerTarget(existItem.$parent); this._removeListenerTarget(existItem.$parent);
this._removeListenerTarget(window); this._removeListenerTarget(window);
@ -273,7 +272,7 @@ export default function () {
*/ */
_addListenerTarget(el) { _addListenerTarget(el) {
if (!el) return; if (!el) return;
let target = find(this.TargetQueue, (target) => target.el === el); let target = this.TargetQueue.find((target) => target.el === el);
if (!target) { if (!target) {
target = { target = {
el, el,
@ -315,7 +314,7 @@ export default function () {
*/ */
_initListen(el, start) { _initListen(el, start) {
this.options.ListenEvents.forEach((evt) => this.options.ListenEvents.forEach((evt) =>
_[start ? 'on' : 'off'](el, evt, this.lazyLoadHandler) (start ? on : off)(el, evt, this.lazyLoadHandler)
); );
} }

View File

@ -32,7 +32,6 @@ export default class ReactiveListener {
this.elRenderer = elRenderer; this.elRenderer = elRenderer;
this._imageCache = imageCache; this._imageCache = imageCache;
this.performanceData = { this.performanceData = {
init: Date.now(),
loadStart: 0, loadStart: 0,
loadEnd: 0, loadEnd: 0,
}; };

View File

@ -1,4 +1,4 @@
const inBrowser = typeof window !== 'undefined'; import { inBrowser } from '@vant/use';
export const hasIntersectionObserver = export const hasIntersectionObserver =
inBrowser && inBrowser &&
@ -11,13 +11,13 @@ export const modeType = {
observer: 'observer', observer: 'observer',
}; };
function remove(arr, item) { export function remove(arr, item) {
if (!arr.length) return; if (!arr.length) return;
const index = arr.indexOf(item); const index = arr.indexOf(item);
if (index > -1) return arr.splice(index, 1); if (index > -1) return arr.splice(index, 1);
} }
function getBestSelectionFromSrcset(el, scale) { export function getBestSelectionFromSrcset(el, scale) {
if (el.tagName !== 'IMG' || !el.getAttribute('data-srcset')) return; if (el.tagName !== 'IMG' || !el.getAttribute('data-srcset')) return;
let options = el.getAttribute('data-srcset'); let options = el.getAttribute('data-srcset');
@ -82,21 +82,10 @@ function getBestSelectionFromSrcset(el, scale) {
return bestSelectedSrc; return bestSelectedSrc;
} }
function find(arr, fn) { export const getDPR = (scale = 1) =>
let item;
for (let i = 0, len = arr.length; i < len; i++) {
if (fn(arr[i])) {
item = arr[i];
break;
}
}
return item;
}
const getDPR = (scale = 1) =>
inBrowser ? window.devicePixelRatio || scale : scale; inBrowser ? window.devicePixelRatio || scale : scale;
function supportWebp() { export function supportWebp() {
if (!inBrowser) return false; if (!inBrowser) return false;
let support = true; let support = true;
@ -114,7 +103,7 @@ function supportWebp() {
return support; return support;
} }
function throttle(action, delay) { export function throttle(action, delay) {
let timeout = null; let timeout = null;
let lastRun = 0; let lastRun = 0;
return function (...args) { return function (...args) {
@ -135,19 +124,18 @@ function throttle(action, delay) {
}; };
} }
const _ = { export function on(el, type, func) {
on(el, type, func, capture = false) {
el.addEventListener(type, func, { el.addEventListener(type, func, {
capture, capture: false,
passive: true, passive: true,
}); });
}, }
off(el, type, func, capture = false) {
el.removeEventListener(type, func, capture);
},
};
const loadImageAsync = (item, resolve, reject) => { export function off(el, type, func) {
el.removeEventListener(type, func, false);
}
export const loadImageAsync = (item, resolve, reject) => {
const image = new Image(); const image = new Image();
if (!item || !item.src) { if (!item || !item.src) {
@ -160,61 +148,23 @@ const loadImageAsync = (item, resolve, reject) => {
image.crossOrigin = item.cors; image.crossOrigin = item.cors;
} }
image.onload = function () { image.onload = () =>
resolve({ resolve({
naturalHeight: image.naturalHeight, naturalHeight: image.naturalHeight,
naturalWidth: image.naturalWidth, naturalWidth: image.naturalWidth,
src: image.src, src: image.src,
}); });
image.onerror = (e) => reject(e);
}; };
image.onerror = function (e) { export function isObject(obj) {
reject(e);
};
};
const style = (el, prop) =>
typeof getComputedStyle !== 'undefined'
? getComputedStyle(el, null).getPropertyValue(prop)
: el.style[prop];
const overflow = (el) =>
style(el, 'overflow') + style(el, 'overflow-y') + style(el, 'overflow-x');
const scrollParent = (el) => {
if (!inBrowser) return;
if (!(el instanceof HTMLElement)) {
return window;
}
let parent = el;
while (parent) {
if (parent === document.body || parent === document.documentElement) {
break;
}
if (!parent.parentNode) {
break;
}
if (/(scroll|auto)/.test(overflow(parent))) {
return parent;
}
parent = parent.parentNode;
}
return window;
};
function isObject(obj) {
return obj !== null && typeof obj === 'object'; return obj !== null && typeof obj === 'object';
} }
function noop() {} export function noop() {}
class ImageCache { export class ImageCache {
constructor({ max }) { constructor({ max }) {
this.options = { this.options = {
max: max || 100, max: max || 100,
@ -238,19 +188,3 @@ class ImageCache {
this._caches.shift(); this._caches.shift();
} }
} }
export {
ImageCache,
inBrowser,
remove,
find,
noop,
_,
isObject,
throttle,
supportWebp,
getDPR,
scrollParent,
loadImageAsync,
getBestSelectionFromSrcset,
};

View File

@ -397,6 +397,11 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@vant/use@^1.3.1":
version "1.3.1"
resolved "https://registry.nlark.com/@vant/use/download/@vant/use-1.3.1.tgz?cache=0&sync_timestamp=1632302722581&other_urls=https%3A%2F%2Fregistry.nlark.com%2F%40vant%2Fuse%2Fdownload%2F%40vant%2Fuse-1.3.1.tgz#8609fc7f86713df0c4ace7109eee6df2f3bd9a0f"
integrity sha1-hgn8f4ZxPfDErOcQnu5t8vO9mg8=
ansi-align@^3.0.0: ansi-align@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.npm.taobao.org/ansi-align/download/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb" resolved "https://registry.npm.taobao.org/ansi-align/download/ansi-align-3.0.0.tgz#b536b371cf687caaef236c18d3e21fe3797467cb"