Skip to content

Commit 63c1dc2

Browse files
authored
Merge pull request hilongjw#489 from caozhong1996/next
chore: remove some useless function
2 parents d0e3c03 + e90861f commit 63c1dc2

File tree

7 files changed

+64
-110
lines changed

7 files changed

+64
-110
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
"rollup-plugin-node-resolve": "^3.4.0",
6666
"rollup-plugin-replace": "^2.2.0",
6767
"rollup-plugin-terser": "^7.0.2",
68-
"rollup-plugin-uglify": "^6.0.4",
6968
"rollup-plugin-typescript": "^1.0.1",
7069
"tslib": "^2.1.0",
7170
"typescript": "^4.1.3",

src/lazy-component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
onMounted,
55
onUnmounted,
66
ref,
7-
Ref,
87
reactive,
98
computed,
109
createVNode
@@ -21,7 +20,7 @@ export default (lazy: Lazy) => {
2120
},
2221
emits: ['show'],
2322
setup(props, { emit, slots }) {
24-
const el: Ref = ref(null)
23+
const el = ref<HTMLElement>()
2524
const state = reactive({
2625
loaded: false,
2726
error: false,

src/lazy-image.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
loadImageAsync,
33
noop
44
} from './util'
5-
import Lazy from './lazy'
5+
import Lazy, { TvalueFormatterParam } from './lazy'
66
import { useCheckInView } from './useCheckInView'
77
import { loadImageAsyncOption } from '../types'
88

@@ -13,22 +13,17 @@ import {
1313
watch,
1414
onMounted,
1515
onUnmounted,
16-
Ref,
1716
createVNode,
1817
computed
1918
} from 'vue'
2019

2120
export default (lazy: Lazy) => {
22-
return defineComponent({
23-
props: {
24-
src: [String, Object],
25-
tag: {
26-
type: String,
27-
default: 'img'
28-
}
29-
},
21+
return defineComponent<{
22+
src: TvalueFormatterParam,
23+
tag: string
24+
}>({
3025
setup(props,{ slots }) {
31-
const el: Ref = ref(null)
26+
const el = ref<HTMLElement>()
3227
const options = reactive({
3328
src: '',
3429
error: '',
@@ -40,13 +35,12 @@ export default (lazy: Lazy) => {
4035
error: false,
4136
attempt: 0
4237
})
43-
const {rect, checkInView } = useCheckInView(el, lazy.options.preLoad!)
44-
const renderSrc: Ref = ref('')
38+
const { rect, checkInView } = useCheckInView(el, lazy.options.preLoad!)
39+
const renderSrc = ref<string>('')
4540
const load = (onFinish = noop) => {
4641
if ((state.attempt > options.attempt! - 1) && state.error) {
4742
if (!lazy.options.silent) console.log(`VueLazyload log: ${options.src} tried too more than ${options.attempt} times`)
48-
onFinish()
49-
return
43+
return onFinish()
5044
}
5145
const src = options.src
5246
loadImageAsync({ src }, ({ src }: loadImageAsyncOption) => {
@@ -91,13 +85,14 @@ export default (lazy: Lazy) => {
9185
init()
9286
lazy.addLazyBox(vm.value)
9387
lazy.lazyLoadHandler()
88+
},
89+
{
90+
immediate: true
9491
}
9592
)
9693

97-
init()
98-
9994
return () => createVNode(
100-
props.tag,
95+
props.tag || 'img',
10196
{
10297
src: renderSrc.value,
10398
ref: el

src/lazy.ts

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type Tlistener = {
4747
load: () => void;
4848
}
4949

50+
export type TvalueFormatterParam = string | Pick<VueLazyloadOptions, 'loading' | 'error'> & { src: string }
51+
5052

5153
class Lazy {
5254
version: string;
@@ -86,7 +88,7 @@ class Lazy {
8688
observer,
8789
observerOptions
8890
}:VueLazyloadOptions) {
89-
this.version = '__VUE_LAZYLOAD_NEXT_VERSION__'
91+
this.version = '__VUE_LAZYLOAD_VERSION__'
9092
this.lazyContainerMananger = null;
9193
this.mode = modeType.event
9294
this.ListenerQueue = []
@@ -116,15 +118,6 @@ class Lazy {
116118
this.setMode(this.options.observer ? modeType.observer : modeType.event)
117119
}
118120

119-
/**
120-
* update config
121-
* @param {Object} config params
122-
* @return
123-
*/
124-
config (options = {}) {
125-
assign(this.options, options)
126-
}
127-
128121
/**
129122
* output listener's load performance
130123
* @return {Array}
@@ -205,8 +198,8 @@ class Lazy {
205198
this._addListenerTarget(window)
206199
this._addListenerTarget($parent)
207200
}
208-
// this.lazyLoadHandler()
209-
nextTick(() => this.lazyLoadHandler())
201+
202+
nextTick(this.lazyLoadHandler)
210203
})
211204
}
212205

@@ -234,8 +227,8 @@ class Lazy {
234227
this._observer.unobserve(el)
235228
this._observer.observe(el)
236229
}
237-
// this.lazyLoadHandler()
238-
nextTick(() => this.lazyLoadHandler())
230+
231+
nextTick(this.lazyLoadHandler)
239232
}
240233

241234
/**
@@ -486,29 +479,24 @@ class Lazy {
486479
}
487480
}
488481

489-
/**
490-
* generate loading loaded error image url
491-
* @param {string} image's src
492-
* @return {object} image's loading, loaded, error url
493-
*/
494-
_valueFormatter (value: any) {
495-
let src = value
496-
let loading = this.options.loading
497-
let error = this.options.error
498-
let cors = this.options.cors
499-
500-
// value is object
501-
if (isObject(value)) {
502-
if (!value.src && !this.options.silent) console.error('Vue Lazyload Next warning: miss src with ' + value)
503-
src = value.src
504-
loading = value.loading || this.options.loading
505-
error = value.error || this.options.error
482+
_valueFormatter (
483+
value: TvalueFormatterParam
484+
)
485+
{
486+
if (typeof value === 'object') {
487+
if (!value.src && !this.options.silent) console.error('Vue Lazyload warning: miss src with ' + value)
488+
return {
489+
src: value.src,
490+
loading: value.loading || this.options.loading,
491+
error: value.error || this.options.error,
492+
cors: this.options.cors
493+
}
506494
}
507495
return {
508-
src,
509-
loading,
510-
error,
511-
cors
496+
src: value,
497+
loading: this.options.loading,
498+
error: this.options.error,
499+
cors: this.options.cors
512500
}
513501
}
514502
}

test/test.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { createApp, inject } from 'vue'
77
describe('VueLazyload.js Test Suite', function () {
88
const App = {
99
template: '<div></div>',
10-
data() {
11-
return {
10+
data () {
11+
return {
1212
Lazyload: inject('Lazyload')
1313
}
1414
}

vue-lazyload.esm.js

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* Vue-Lazyload.js v3.0.0-alpha.0
3-
* (c) 2021 Awe <hilongjw@gmail.com>
3+
* (c) 2022 Awe <hilongjw@gmail.com>
44
* Released under the MIT License.
55
*/
66

@@ -306,9 +306,6 @@ const scrollParent = el => {
306306
}
307307
return window;
308308
};
309-
function isObject(obj) {
310-
return obj !== null && typeof obj === 'object';
311-
}
312309
function noop() {}
313310
class ImageCache {
314311
constructor(max) {
@@ -538,7 +535,7 @@ const DEFAULT_OBSERVER_OPTIONS = {
538535
};
539536
class Lazy {
540537
constructor({ preLoad, error, throttleWait, preLoadTop, dispatchEvent, loading, attempt, silent = true, scale, listenEvents, filter, adapter, observer, observerOptions }) {
541-
this.version = '__VUE_LAZYLOAD_NEXT_VERSION__';
538+
this.version = '"3.0.0-alpha.0"';
542539
this.lazyContainerMananger = null;
543540
this.mode = modeType.event;
544541
this.ListenerQueue = [];
@@ -566,14 +563,6 @@ class Lazy {
566563
this.lazyLoadHandler = throttle(this._lazyLoadHandler.bind(this), this.options.throttleWait);
567564
this.setMode(this.options.observer ? modeType.observer : modeType.event);
568565
}
569-
/**
570-
* update config
571-
* @param {Object} config params
572-
* @return
573-
*/
574-
config(options = {}) {
575-
assignDeep(this.options, options);
576-
}
577566
/**
578567
* output listener's load performance
579568
* @return {Array}
@@ -630,8 +619,7 @@ class Lazy {
630619
this._addListenerTarget(window);
631620
this._addListenerTarget($parent);
632621
}
633-
// this.lazyLoadHandler()
634-
nextTick(() => this.lazyLoadHandler());
622+
nextTick(this.lazyLoadHandler);
635623
});
636624
}
637625
/**
@@ -657,8 +645,7 @@ class Lazy {
657645
this._observer.unobserve(el);
658646
this._observer.observe(el);
659647
}
660-
// this.lazyLoadHandler()
661-
nextTick(() => this.lazyLoadHandler());
648+
nextTick(this.lazyLoadHandler);
662649
}
663650
/**
664651
* remove listener form list
@@ -884,28 +871,21 @@ class Lazy {
884871
el.dispatchEvent(event);
885872
}
886873
}
887-
/**
888-
* generate loading loaded error image url
889-
* @param {string} image's src
890-
* @return {object} image's loading, loaded, error url
891-
*/
892874
_valueFormatter(value) {
893-
let src = value;
894-
let loading = this.options.loading;
895-
let error = this.options.error;
896-
let cors = this.options.cors;
897-
// value is object
898-
if (isObject(value)) {
899-
if (!value.src && !this.options.silent) console.error('Vue Lazyload Next warning: miss src with ' + value);
900-
src = value.src;
901-
loading = value.loading || this.options.loading;
902-
error = value.error || this.options.error;
875+
if (typeof value === 'object') {
876+
if (!value.src && !this.options.silent) console.error('Vue Lazyload warning: miss src with ' + value);
877+
return {
878+
src: value.src,
879+
loading: value.loading || this.options.loading,
880+
error: value.error || this.options.error,
881+
cors: this.options.cors
882+
};
903883
}
904884
return {
905-
src,
906-
loading,
907-
error,
908-
cors
885+
src: value,
886+
loading: this.options.loading,
887+
error: this.options.error,
888+
cors: this.options.cors
909889
};
910890
}
911891
}
@@ -935,7 +915,7 @@ var LazyComponent = (lazy => {
935915
},
936916
emits: ['show'],
937917
setup(props, { emit, slots }) {
938-
const el = ref(null);
918+
const el = ref();
939919
const state = reactive({
940920
loaded: false,
941921
error: false,
@@ -1039,15 +1019,8 @@ class LazyContainer {
10391019

10401020
var LazyImage = (lazy => {
10411021
return defineComponent({
1042-
props: {
1043-
src: [String, Object],
1044-
tag: {
1045-
type: String,
1046-
default: 'img'
1047-
}
1048-
},
10491022
setup(props, { slots }) {
1050-
const el = ref(null);
1023+
const el = ref();
10511024
const options = reactive({
10521025
src: '',
10531026
error: '',
@@ -1064,8 +1037,7 @@ var LazyImage = (lazy => {
10641037
const load = (onFinish = noop) => {
10651038
if (state.attempt > options.attempt - 1 && state.error) {
10661039
if (!lazy.options.silent) console.log(`VueLazyload log: ${options.src} tried too more than ${options.attempt} times`);
1067-
onFinish();
1068-
return;
1040+
return onFinish();
10691041
}
10701042
const src = options.src;
10711043
loadImageAsync({ src }, ({ src }) => {
@@ -1105,11 +1077,12 @@ var LazyImage = (lazy => {
11051077
init();
11061078
lazy.addLazyBox(vm.value);
11071079
lazy.lazyLoadHandler();
1080+
}, {
1081+
immediate: true
11081082
});
1109-
init();
11101083
return () => {
11111084
var _a;
1112-
return createVNode(props.tag, {
1085+
return createVNode(props.tag || 'img', {
11131086
src: renderSrc.value,
11141087
ref: el
11151088
}, [(_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots)]);
@@ -1151,4 +1124,4 @@ var index = {
11511124
}
11521125
};
11531126

1154-
export default index;
1127+
export { index as default };

0 commit comments

Comments
 (0)