async-preloader
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -164,10 +164,7 @@ (function (global, factory) { | ||
}() | ||
// Loaders | ||
}, { | ||
key: "loadDefault", | ||
key: "loadManifest", | ||
value: function () { | ||
var _ref3 = asyncToGenerator(regeneratorRuntime.mark(function _callee3(item) { | ||
var response, data; | ||
var _ref3 = asyncToGenerator(regeneratorRuntime.mark(function _callee3(src) { | ||
var loadedManifest, loadedItems; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -178,12 +175,12 @@ while (1) { | ||
_context3.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
return AsyncPreloader.loadJson({ src: src }); | ||
case 2: | ||
response = _context3.sent; | ||
loadedManifest = _context3.sent; | ||
_context3.next = 5; | ||
return response.text(); | ||
return AsyncPreloader.loadItems(loadedManifest.items); | ||
case 5: | ||
data = _context3.sent; | ||
return _context3.abrupt("return", data); | ||
loadedItems = _context3.sent; | ||
return _context3.abrupt("return", loadedItems); | ||
@@ -198,10 +195,13 @@ case 7: | ||
function loadDefault(_x3) { | ||
function loadManifest(_x3) { | ||
return _ref3.apply(this, arguments); | ||
} | ||
return loadDefault; | ||
return loadManifest; | ||
}() | ||
// Loaders | ||
}, { | ||
key: "loadJson", | ||
key: "loadDefault", | ||
value: function () { | ||
@@ -220,3 +220,3 @@ var _ref4 = asyncToGenerator(regeneratorRuntime.mark(function _callee4(item) { | ||
_context4.next = 5; | ||
return response.json(); | ||
return response.text(); | ||
@@ -235,13 +235,13 @@ case 5: | ||
function loadJson(_x4) { | ||
function loadDefault(_x4) { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return loadJson; | ||
return loadDefault; | ||
}() | ||
}, { | ||
key: "loadImage", | ||
key: "loadJson", | ||
value: function () { | ||
var _ref5 = asyncToGenerator(regeneratorRuntime.mark(function _callee5(item) { | ||
var response, data, image; | ||
var response, data; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
@@ -257,6 +257,41 @@ while (1) { | ||
_context5.next = 5; | ||
return response.json(); | ||
case 5: | ||
data = _context5.sent; | ||
return _context5.abrupt("return", data); | ||
case 7: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function loadJson(_x5) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return loadJson; | ||
}() | ||
}, { | ||
key: "loadImage", | ||
value: function () { | ||
var _ref6 = asyncToGenerator(regeneratorRuntime.mark(function _callee6(item) { | ||
var response, data, image; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context6.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context6.sent; | ||
_context6.next = 5; | ||
return response.blob(); | ||
case 5: | ||
data = _context5.sent; | ||
data = _context6.sent; | ||
image = new Image(); | ||
@@ -266,3 +301,3 @@ | ||
_context5.next = 10; | ||
_context6.next = 10; | ||
return new Promise(function (resolve, reject) { | ||
@@ -276,14 +311,14 @@ image.addEventListener("load", function () { | ||
case 10: | ||
return _context5.abrupt("return", _context5.sent); | ||
return _context6.abrupt("return", _context6.sent); | ||
case 11: | ||
case "end": | ||
return _context5.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee6, this); | ||
})); | ||
function loadImage(_x5) { | ||
return _ref5.apply(this, arguments); | ||
function loadImage(_x6) { | ||
return _ref6.apply(this, arguments); | ||
} | ||
@@ -296,18 +331,18 @@ | ||
value: function () { | ||
var _ref6 = asyncToGenerator(regeneratorRuntime.mark(function _callee6(item) { | ||
var _ref7 = asyncToGenerator(regeneratorRuntime.mark(function _callee7(item) { | ||
var response, data, video; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context6.next = 2; | ||
_context7.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context6.sent; | ||
_context6.next = 5; | ||
response = _context7.sent; | ||
_context7.next = 5; | ||
return response.blob(); | ||
case 5: | ||
data = _context6.sent; | ||
data = _context7.sent; | ||
video = document.createElement("video"); | ||
@@ -317,3 +352,3 @@ | ||
_context6.next = 10; | ||
_context7.next = 10; | ||
return new Promise(function (resolve, reject) { | ||
@@ -327,14 +362,14 @@ video.addEventListener("canplaythrough", function () { | ||
case 10: | ||
return _context6.abrupt("return", _context6.sent); | ||
return _context7.abrupt("return", _context7.sent); | ||
case 11: | ||
case "end": | ||
return _context6.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
}, _callee7, this); | ||
})); | ||
function loadVideo(_x6) { | ||
return _ref6.apply(this, arguments); | ||
function loadVideo(_x7) { | ||
return _ref7.apply(this, arguments); | ||
} | ||
@@ -347,18 +382,18 @@ | ||
value: function () { | ||
var _ref7 = asyncToGenerator(regeneratorRuntime.mark(function _callee7(item) { | ||
var _ref8 = asyncToGenerator(regeneratorRuntime.mark(function _callee8(item) { | ||
var response, data, audio; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context8.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context7.sent; | ||
_context7.next = 5; | ||
response = _context8.sent; | ||
_context8.next = 5; | ||
return response.blob(); | ||
case 5: | ||
data = _context7.sent; | ||
data = _context8.sent; | ||
audio = document.createElement("audio"); | ||
@@ -370,3 +405,3 @@ | ||
_context7.next = 12; | ||
_context8.next = 12; | ||
return new Promise(function (resolve, reject) { | ||
@@ -380,14 +415,14 @@ audio.addEventListener("canplaythrough", function () { | ||
case 12: | ||
return _context7.abrupt("return", _context7.sent); | ||
return _context8.abrupt("return", _context8.sent); | ||
case 13: | ||
case "end": | ||
return _context7.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
}, _callee8, this); | ||
})); | ||
function loadAudio(_x7) { | ||
return _ref7.apply(this, arguments); | ||
function loadAudio(_x8) { | ||
return _ref8.apply(this, arguments); | ||
} | ||
@@ -400,18 +435,18 @@ | ||
value: function () { | ||
var _ref8 = asyncToGenerator(regeneratorRuntime.mark(function _callee8(item, extension) { | ||
var _ref9 = asyncToGenerator(regeneratorRuntime.mark(function _callee9(item, extension) { | ||
var response, data, type, parser; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context8.next = 2; | ||
_context9.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context8.sent; | ||
_context8.next = 5; | ||
response = _context9.sent; | ||
_context9.next = 5; | ||
return response.text(); | ||
case 5: | ||
data = _context8.sent; | ||
data = _context9.sent; | ||
type = { | ||
@@ -423,14 +458,14 @@ xml: "application/xml", | ||
parser = new DOMParser(); | ||
return _context8.abrupt("return", parser.parseFromString(data, type[extension])); | ||
return _context9.abrupt("return", parser.parseFromString(data, type[extension])); | ||
case 9: | ||
case "end": | ||
return _context8.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
}, _callee9, this); | ||
})); | ||
function loadXml(_x8, _x9) { | ||
return _ref8.apply(this, arguments); | ||
function loadXml(_x9, _x10) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
@@ -443,27 +478,27 @@ | ||
value: function () { | ||
var _ref9 = asyncToGenerator(regeneratorRuntime.mark(function _callee9(item) { | ||
var _ref10 = asyncToGenerator(regeneratorRuntime.mark(function _callee10(item) { | ||
var fontName, font, data; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
fontName = AsyncPreloader.getFileName(item.src); | ||
font = new FontFaceObserver(fontName, item.options || {}); | ||
_context9.next = 4; | ||
_context10.next = 4; | ||
return font.load(); | ||
case 4: | ||
data = _context9.sent; | ||
return _context9.abrupt("return", fontName); | ||
data = _context10.sent; | ||
return _context10.abrupt("return", fontName); | ||
case 6: | ||
case "end": | ||
return _context9.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
}, _callee10, this); | ||
})); | ||
function loadFont(_x10) { | ||
return _ref9.apply(this, arguments); | ||
function loadFont(_x11) { | ||
return _ref10.apply(this, arguments); | ||
} | ||
@@ -470,0 +505,0 @@ |
@@ -158,10 +158,7 @@ import FontFaceObserver from 'fontfaceobserver'; | ||
}() | ||
// Loaders | ||
}, { | ||
key: "loadDefault", | ||
key: "loadManifest", | ||
value: function () { | ||
var _ref3 = asyncToGenerator(regeneratorRuntime.mark(function _callee3(item) { | ||
var response, data; | ||
var _ref3 = asyncToGenerator(regeneratorRuntime.mark(function _callee3(src) { | ||
var loadedManifest, loadedItems; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -172,12 +169,12 @@ while (1) { | ||
_context3.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
return AsyncPreloader.loadJson({ src: src }); | ||
case 2: | ||
response = _context3.sent; | ||
loadedManifest = _context3.sent; | ||
_context3.next = 5; | ||
return response.text(); | ||
return AsyncPreloader.loadItems(loadedManifest.items); | ||
case 5: | ||
data = _context3.sent; | ||
return _context3.abrupt("return", data); | ||
loadedItems = _context3.sent; | ||
return _context3.abrupt("return", loadedItems); | ||
@@ -192,10 +189,13 @@ case 7: | ||
function loadDefault(_x3) { | ||
function loadManifest(_x3) { | ||
return _ref3.apply(this, arguments); | ||
} | ||
return loadDefault; | ||
return loadManifest; | ||
}() | ||
// Loaders | ||
}, { | ||
key: "loadJson", | ||
key: "loadDefault", | ||
value: function () { | ||
@@ -214,3 +214,3 @@ var _ref4 = asyncToGenerator(regeneratorRuntime.mark(function _callee4(item) { | ||
_context4.next = 5; | ||
return response.json(); | ||
return response.text(); | ||
@@ -229,13 +229,13 @@ case 5: | ||
function loadJson(_x4) { | ||
function loadDefault(_x4) { | ||
return _ref4.apply(this, arguments); | ||
} | ||
return loadJson; | ||
return loadDefault; | ||
}() | ||
}, { | ||
key: "loadImage", | ||
key: "loadJson", | ||
value: function () { | ||
var _ref5 = asyncToGenerator(regeneratorRuntime.mark(function _callee5(item) { | ||
var response, data, image; | ||
var response, data; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
@@ -251,6 +251,41 @@ while (1) { | ||
_context5.next = 5; | ||
return response.json(); | ||
case 5: | ||
data = _context5.sent; | ||
return _context5.abrupt("return", data); | ||
case 7: | ||
case "end": | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
})); | ||
function loadJson(_x5) { | ||
return _ref5.apply(this, arguments); | ||
} | ||
return loadJson; | ||
}() | ||
}, { | ||
key: "loadImage", | ||
value: function () { | ||
var _ref6 = asyncToGenerator(regeneratorRuntime.mark(function _callee6(item) { | ||
var response, data, image; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
_context6.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context6.sent; | ||
_context6.next = 5; | ||
return response.blob(); | ||
case 5: | ||
data = _context5.sent; | ||
data = _context6.sent; | ||
image = new Image(); | ||
@@ -260,3 +295,3 @@ | ||
_context5.next = 10; | ||
_context6.next = 10; | ||
return new Promise(function (resolve, reject) { | ||
@@ -270,14 +305,14 @@ image.addEventListener("load", function () { | ||
case 10: | ||
return _context5.abrupt("return", _context5.sent); | ||
return _context6.abrupt("return", _context6.sent); | ||
case 11: | ||
case "end": | ||
return _context5.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee6, this); | ||
})); | ||
function loadImage(_x5) { | ||
return _ref5.apply(this, arguments); | ||
function loadImage(_x6) { | ||
return _ref6.apply(this, arguments); | ||
} | ||
@@ -290,18 +325,18 @@ | ||
value: function () { | ||
var _ref6 = asyncToGenerator(regeneratorRuntime.mark(function _callee6(item) { | ||
var _ref7 = asyncToGenerator(regeneratorRuntime.mark(function _callee7(item) { | ||
var response, data, video; | ||
return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
_context6.next = 2; | ||
_context7.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context6.sent; | ||
_context6.next = 5; | ||
response = _context7.sent; | ||
_context7.next = 5; | ||
return response.blob(); | ||
case 5: | ||
data = _context6.sent; | ||
data = _context7.sent; | ||
video = document.createElement("video"); | ||
@@ -311,3 +346,3 @@ | ||
_context6.next = 10; | ||
_context7.next = 10; | ||
return new Promise(function (resolve, reject) { | ||
@@ -321,14 +356,14 @@ video.addEventListener("canplaythrough", function () { | ||
case 10: | ||
return _context6.abrupt("return", _context6.sent); | ||
return _context7.abrupt("return", _context7.sent); | ||
case 11: | ||
case "end": | ||
return _context6.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
}, _callee7, this); | ||
})); | ||
function loadVideo(_x6) { | ||
return _ref6.apply(this, arguments); | ||
function loadVideo(_x7) { | ||
return _ref7.apply(this, arguments); | ||
} | ||
@@ -341,18 +376,18 @@ | ||
value: function () { | ||
var _ref7 = asyncToGenerator(regeneratorRuntime.mark(function _callee7(item) { | ||
var _ref8 = asyncToGenerator(regeneratorRuntime.mark(function _callee8(item) { | ||
var response, data, audio; | ||
return regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
_context7.next = 2; | ||
_context8.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context7.sent; | ||
_context7.next = 5; | ||
response = _context8.sent; | ||
_context8.next = 5; | ||
return response.blob(); | ||
case 5: | ||
data = _context7.sent; | ||
data = _context8.sent; | ||
audio = document.createElement("audio"); | ||
@@ -364,3 +399,3 @@ | ||
_context7.next = 12; | ||
_context8.next = 12; | ||
return new Promise(function (resolve, reject) { | ||
@@ -374,14 +409,14 @@ audio.addEventListener("canplaythrough", function () { | ||
case 12: | ||
return _context7.abrupt("return", _context7.sent); | ||
return _context8.abrupt("return", _context8.sent); | ||
case 13: | ||
case "end": | ||
return _context7.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
}, _callee8, this); | ||
})); | ||
function loadAudio(_x7) { | ||
return _ref7.apply(this, arguments); | ||
function loadAudio(_x8) { | ||
return _ref8.apply(this, arguments); | ||
} | ||
@@ -394,18 +429,18 @@ | ||
value: function () { | ||
var _ref8 = asyncToGenerator(regeneratorRuntime.mark(function _callee8(item, extension) { | ||
var _ref9 = asyncToGenerator(regeneratorRuntime.mark(function _callee9(item, extension) { | ||
var response, data, type, parser; | ||
return regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
_context8.next = 2; | ||
_context9.next = 2; | ||
return fetch(item.src, item.options || {}); | ||
case 2: | ||
response = _context8.sent; | ||
_context8.next = 5; | ||
response = _context9.sent; | ||
_context9.next = 5; | ||
return response.text(); | ||
case 5: | ||
data = _context8.sent; | ||
data = _context9.sent; | ||
type = { | ||
@@ -417,14 +452,14 @@ xml: "application/xml", | ||
parser = new DOMParser(); | ||
return _context8.abrupt("return", parser.parseFromString(data, type[extension])); | ||
return _context9.abrupt("return", parser.parseFromString(data, type[extension])); | ||
case 9: | ||
case "end": | ||
return _context8.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
}, _callee9, this); | ||
})); | ||
function loadXml(_x8, _x9) { | ||
return _ref8.apply(this, arguments); | ||
function loadXml(_x9, _x10) { | ||
return _ref9.apply(this, arguments); | ||
} | ||
@@ -437,27 +472,27 @@ | ||
value: function () { | ||
var _ref9 = asyncToGenerator(regeneratorRuntime.mark(function _callee9(item) { | ||
var _ref10 = asyncToGenerator(regeneratorRuntime.mark(function _callee10(item) { | ||
var fontName, font, data; | ||
return regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
return regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
fontName = AsyncPreloader.getFileName(item.src); | ||
font = new FontFaceObserver(fontName, item.options || {}); | ||
_context9.next = 4; | ||
_context10.next = 4; | ||
return font.load(); | ||
case 4: | ||
data = _context9.sent; | ||
return _context9.abrupt("return", fontName); | ||
data = _context10.sent; | ||
return _context10.abrupt("return", fontName); | ||
case 6: | ||
case "end": | ||
return _context9.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
}, _callee10, this); | ||
})); | ||
function loadFont(_x10) { | ||
return _ref9.apply(this, arguments); | ||
function loadFont(_x11) { | ||
return _ref10.apply(this, arguments); | ||
} | ||
@@ -464,0 +499,0 @@ |
{ | ||
"name": "async-preloader", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Assets preloader using ES2017 async/await and fetch.", | ||
@@ -5,0 +5,0 @@ "main": "lib/async-preloader.js", |
106
README.md
@@ -17,18 +17,47 @@ # async-preloader | ||
## Usage | ||
## API | ||
### LoadItem | ||
```js | ||
interface LoadItem { | ||
id?: string; | ||
src: string; | ||
loader?: string; | ||
} | ||
``` | ||
|Key|Description | ||
|:---------|:---------| | ||
|**id**|Optional id to retrieve the file using `AsyncPreloader.items.get(id)`| | ||
|**src**|Input for the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)| | ||
|**loader**|Optional string from one of the [LOADERS Map](https://github.com/dmnsgn/async-preloader/blob/master/src/index.js#L20). It needs to be specified for Font and Audio (webm|off). Otherwise the loader is inferred from the file extension or default to `Response.text()` if there is no extension.| | ||
### AsyncPreloader.loadItems(items: LoadItem[]) | ||
#### Arguments | ||
`items` (*Array*): Array of `LoadItem`s | ||
#### Returns | ||
(*Promise*): A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves with the loaded items. | ||
#### Example | ||
```js | ||
import AsyncPreloader from "async-preloader"; | ||
const pItems = AsyncPreloader.loadItems([ | ||
{ src: "/static/default" }, // A file without extension or loader specified uses `Response.text()` | ||
{ id: "myTextFile", src: "/static/text.txt" }, | ||
{ id: "myJsonFile", src: "/static/json.json" }, | ||
{ id: "myImageFile", src: "/static/image.jpg" }, | ||
{ id: "myVideoFile", src: "/static/video.mp4" }, | ||
{ id: "myAudioFile", src: "/static/audio.mp3" }, | ||
{ id: "myXmlFile", src: "/static/xml.xml" }, | ||
{ id: "mySvgFile", src: "/static/xml.svg" }, | ||
{ id: "myHtmlFile", src: "/static/xml.html" }, | ||
{ loader: "Font", src: "Open Sans" } // src is the font name here https://github.com/bramstein/fontfaceobserver | ||
{ "id": "myDefaultFile", "src": "assets/default" }, | ||
{ "id": "myTextFile", "src": "assets/text.txt" }, | ||
{ "id": "myJsonFile", "src": "assets/json.json" }, | ||
{ "id": "myImageFile", "src": "assets/image.jpg" }, | ||
{ "id": "myVideoFile", "src": "assets/video.mp4" }, | ||
{ "id": "myAudioFile", "src": "assets/audio.mp3" }, | ||
{ "id": "myXmlFile", "src": "assets/xml.xml" }, | ||
{ "id": "mySvgFile", "src": "assets/xml.svg" }, | ||
{ "id": "myHtmlFile", "src": "assets/xml.html" }, | ||
{ "id": "myFont", "src": "Open Sans Regular", "loader": "Font" } | ||
]); | ||
@@ -38,4 +67,2 @@ | ||
.then(items => { | ||
console.log("All items loaded", items); | ||
const element = AsyncPreloader.items.get("myVideoFile"); | ||
@@ -47,5 +74,58 @@ document.body.appendChild(element); | ||
--- | ||
Note: Font loader is using [FontFaceObserver](https://github.com/bramstein/fontfaceobserver) | ||
### AsyncPreloader.loadManifest(src: string) | ||
You can also load a manifest file. It works in a similar fashion as createjs's [PreloadJS](http://www.createjs.com/docs/preloadjs/classes/LoadQueue.html). | ||
#### Arguments | ||
`src` (*String*): Input for the Fetch API. It will load the file using the `JsonLoader` and look for an `"items"` key containing an array of `LoadItem`s. | ||
#### Returns | ||
(*Promise*): A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves with the loaded items. | ||
#### Example | ||
```js | ||
import AsyncPreloader from "async-preloader"; | ||
const pItems = AsyncPreloader.loadItems('assets/manifest.json'); | ||
pItems | ||
.then(items => useLoadedItemsFromManifest(items)) // or AsyncPreloader.items.get(pathOrId) | ||
.catch(error => console.error("Error loading items", error)); | ||
``` | ||
### AsyncPreloader.loadJson(item: LoadItem) | ||
It is also possible to use the [LOADERS](https://github.com/dmnsgn/async-preloader/blob/master/src/index.js#L20) individually. | ||
#### Arguments | ||
`item` (*LoadItem*): a `LoadItem`. | ||
#### Returns | ||
(*Promise*): A [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves with the loaded item. | ||
#### Example | ||
```js | ||
import AsyncPreloader from "async-preloader"; | ||
const pItem = AsyncPreloader.loadJson('assets/json.json'); | ||
pItem | ||
.then(item => useLoadedItemFromManifest(item)) | ||
.catch(error => console.error("Error loading item", error)); | ||
``` | ||
## License | ||
MIT © [Damien Seguin](https://github.com/dmnsgn) |
@@ -1,1 +0,1 @@ | ||
Test | ||
test string |
/** | ||
* @jest-environment jsdom-latest | ||
*/ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import fetchMock from "fetch-mock"; | ||
import AsyncPreloader from "../src"; | ||
import { items, expected, manifestSrc } from "./data"; | ||
import { mockFetch, unmockFetch } from "./mock"; | ||
// Mock | ||
const mockFetch = jest.fn((filePath, buffer) => { | ||
const file = path.resolve("test", filePath); | ||
let response; | ||
if (buffer) { | ||
response = fs.readFileSync(file); | ||
} else { | ||
response = fs.readFileSync(file, { encoding: "utf-8" }); | ||
} | ||
fetchMock.get(filePath, response); | ||
}); | ||
const unMockFetch = jest.fn(fetchMock.restore); | ||
// Data | ||
const items = new Map() | ||
.set("default", { id: "myDefaultFile", src: "assets/default" }) | ||
.set("txt", { id: "myTextFile", src: "assets/text.txt" }) | ||
.set("json", { id: "myJsonFile", src: "assets/json.json" }) | ||
.set("jpg", { id: "myImageFile", src: "assets/image.jpg" }) | ||
.set("mp4", { id: "myVideoFile", src: "assets/video.mp4" }) | ||
.set("mp3", { id: "myAudioFile", src: "assets/audio.mp3" }) | ||
.set("xml", { id: "myXmlFile", src: "assets/xml.xml" }) | ||
.set("svg", { id: "mySvgFile", src: "assets/xml.svg" }) | ||
.set("html", { id: "myHtmlFile", src: "assets/xml.html" }) | ||
.set("font", { id: "myFont", src: "Open Sans Regular", loader: "Font" }); | ||
// Suite | ||
@@ -51,7 +18,7 @@ describe("AsyncPreloader", () => { | ||
afterEach(() => { | ||
unMockFetch(); | ||
unmockFetch(); | ||
}); | ||
it("should load array of items", async () => { | ||
expect.assertions(1); | ||
expect.assertions(3); | ||
@@ -71,2 +38,4 @@ let itemsToLoad = Array.from(items.values()); | ||
expect(data).toBeInstanceOf(Array); | ||
expect(data).toHaveLength(items.size - 3); // TEMP: fix blob tests | ||
expect(data).toEqual(expect.arrayContaining(Array.from(expected.values()))); | ||
}); | ||
@@ -81,3 +50,3 @@ | ||
const data = await AsyncPreloader.loadItem(item); | ||
expect(data).toBe(`Default test\n`); | ||
expect(data).toBe(expected.get("string")); | ||
}); | ||
@@ -92,3 +61,3 @@ | ||
const data = await AsyncPreloader.loadItem(item); | ||
expect(data).toBeInstanceOf(Object); | ||
expect(data).toEqual(expected.get("json")); | ||
}); | ||
@@ -99,4 +68,4 @@ | ||
// const item = items.get("png"); | ||
// mockFetch(src, "image/item.src); | ||
// const item = items.get("jpg"); | ||
// mockFetch(item.src); | ||
@@ -111,3 +80,3 @@ // const data = await AsyncPreloader.loadItem(item); | ||
// const item = items.get("mp4"); | ||
// mockFetch(src, "video/item.src"); | ||
// mockFetch(item.src); | ||
@@ -122,3 +91,3 @@ // const data = await AsyncPreloader.loadItem(item); | ||
// const item = items.get("mp3"); | ||
// mockFetch(src, "audio/item.src"); | ||
// mockFetch(item.src); | ||
@@ -146,3 +115,3 @@ // const data = await AsyncPreloader.loadItem(item); | ||
const data = await AsyncPreloader.loadItem(item); | ||
expect(data).toBe(`Test\n`); | ||
expect(data).toBe(expected.get("string")); | ||
}); | ||
@@ -155,3 +124,3 @@ | ||
const data = await AsyncPreloader.loadItem(item); | ||
expect(data).toBe("Open Sans Regular"); | ||
expect(data).toBe(expected.get("font")); | ||
}); | ||
@@ -166,4 +135,15 @@ | ||
const data = await AsyncPreloader.loadItem({ src: item.src }); | ||
expect(data).toBe(`Default test\n`); | ||
expect(data).toBe(expected.get("string")); | ||
}); | ||
it("should load manifest, load its items and return the items", async () => { | ||
expect.assertions(3); | ||
mockFetch(manifestSrc); | ||
const data = await AsyncPreloader.loadManifest(manifestSrc); | ||
expect(data).toBeInstanceOf(Array); | ||
expect(data).toHaveLength(items.size - 3); // TEMP: fix blob tests | ||
expect(data).toEqual(expect.arrayContaining(Array.from(expected.values()))); | ||
}); | ||
}); | ||
@@ -170,0 +150,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8630335
29
1132
129