-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
124 lines (118 loc) · 3.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright (c) 2017 chenyinkai
* License: MIT
* https://github.com/chenyinkai/resloader
**/
/**
* Checks the value is an object or not
*
* @param {*} value value The value to check
* @return {boolean} Returns `true` if `value` is an object, else `false`
*/
const isObject = value => {
return typeof value === 'object' && !!value
}
/**
* Checks the value is an Array or not
*
* @param {*} value value The value to check
* @return {boolean} Returns `true` if `value` is an Array, else `false`
*/
const isArray = value => {
return toString.call(value) === '[object Array]'
}
/**
* Checks the value is a Function or not
*
* @param {*} value value The value to check
* @return {boolean} Returns `true` if `value` is an Function, else `false`
*/
const isFunction = value => {
return toString.call(value) === '[object Function]'
}
let resLoaderObj = {
total: 0,
start (options) {
let imageArr = options.resources
this.total = options.resources.length
if (isFunction(options.onStart)) {
options.onStart(this.total)
}
Promise.all(this._PromiseArray(imageArr, options.onProgress))
.then(result => {
if (isFunction(options.onComplete)) {
options.onComplete(this.total, result)
}
})
.catch(err => console.log(err))
},
_PromiseArray (imageArr, callback) {
let promiseArr = []
let currentIndex = 0
for (let i = 0; i < imageArr.length; i++) {
let reg = /\.(jpg|jpeg|png|gif|svg|webp)(\?\S*)?$/i
let isImg =
(isObject(imageArr[i]) && reg.test(imageArr[i].url).toString()) || reg.test(imageArr.url)
let promise =
(isImg == 'true' && this._loadImage(imageArr[i])) || this._loadAudio(imageArr[i])
promiseArr.push(promise)
promise
.then(data => {
currentIndex++
if (isFunction(callback)) {
callback(currentIndex, this.total)
}
})
.catch(err => console.log(err))
}
return promiseArr
},
_loadImage (imageData) {
return new Promise((resolve, reject) => {
let image = new Image()
image.onload = () => {
resolve(imageData)
}
if (isObject(imageData)) {
image.src = imageData.url
} else {
image.src = imageData
}
image.onerror = () => {
reject(imageData)
}
})
},
_loadAudio (audioData) {
return new Promise((resolve, reject) => {
let audio = new Audio()
audio.src = (isObject(audioData) && audioData.url) || audioData
audio.addEventListener(
'canplaythrough',
function () {
resolve(audioData)
},
false
)
audio.addEventListener(
'error',
function () {
reject(audioData)
},
false
)
})
}
}
const resloader = options => {
if (!isObject(options)) {
throw new TypeError('Expected an object')
} else {
if (!isArray(options.resources)) {
throw new TypeError('options.resources Expected an Array or JSON')
} else {
resLoaderObj.start(options)
}
}
}
module.exports = resloader