image-promise
Advanced tools
Comparing version 3.0.0 to 3.0.1
/*! npm.im/image-promise */ | ||
'use strict'; | ||
function load(src) { | ||
function trackLoading(image, src) { | ||
if (src) { | ||
image.src = src; | ||
} | ||
var promise = new Promise(function (resolve, reject) { | ||
if (image.complete) { | ||
resolve(image); | ||
} else { | ||
image.addEventListener('load', function () { return resolve(image); }); | ||
image.addEventListener('error', function () { return reject(image); }); | ||
} | ||
}); | ||
promise.image = image; | ||
return promise; | ||
} | ||
function load(image) { | ||
// if argument is an array, treat as | ||
// load(['1.jpg', '2.jpg']) | ||
if (src.map) { | ||
return Promise.all(src.map(load)); | ||
if (image.map) { | ||
return Promise.all(image.map(load)); | ||
} | ||
// check whether an <img> was passed as argument | ||
var image = new Image(); | ||
if (src.src) { | ||
image = src; | ||
src = src.src; | ||
// if image is just a <img>, don't cache it | ||
if (image.src) { | ||
return trackLoading(image); | ||
} | ||
if (!load[src]) { | ||
load[src] = new Promise(function (resolve, reject) { | ||
image.addEventListener('load', function () { return resolve(image); }); | ||
image.addEventListener('error', function () { return reject(image); }); | ||
if (!image.src) { | ||
image.src = src; | ||
} | ||
if (image.complete) { | ||
resolve(image); | ||
} | ||
}); | ||
load[src].image = image; | ||
// load is treated as a map, assumes all image paths don't clash with Function.prototype | ||
if (!load[image]) { | ||
load[image] = trackLoading(new Image(), image); | ||
} | ||
return load[src]; | ||
return load[image]; | ||
} | ||
load.unload = function (src) { | ||
// an <img> was passed as argument | ||
if (src.src) { | ||
src = src.src; | ||
load.unload = function (image) { | ||
if (!image.src) { | ||
// an <img> was passed as argument, so nothing to unload | ||
return; | ||
} | ||
@@ -44,8 +47,8 @@ | ||
// load(['1.jpg', '2.jpg']) | ||
if (src.map) { | ||
src.map(load.unload); | ||
} else if (load[src]) { | ||
if (image.map) { | ||
image.map(load.unload); | ||
} else if (load[image]) { | ||
// GC, http://www.fngtps.com/2010/mobile-safari-image-resource-limit-workaround/ | ||
load[src].image.src = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='; | ||
delete load[src]; | ||
load[image].image.src = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='; | ||
delete load[image]; | ||
} | ||
@@ -52,0 +55,0 @@ }; |
/*! npm.im/image-promise */ | ||
function load(src) { | ||
function trackLoading(image, src) { | ||
if (src) { | ||
image.src = src; | ||
} | ||
var promise = new Promise(function (resolve, reject) { | ||
if (image.complete) { | ||
resolve(image); | ||
} else { | ||
image.addEventListener('load', function () { return resolve(image); }); | ||
image.addEventListener('error', function () { return reject(image); }); | ||
} | ||
}); | ||
promise.image = image; | ||
return promise; | ||
} | ||
function load(image) { | ||
// if argument is an array, treat as | ||
// load(['1.jpg', '2.jpg']) | ||
if (src.map) { | ||
return Promise.all(src.map(load)); | ||
if (image.map) { | ||
return Promise.all(image.map(load)); | ||
} | ||
// check whether an <img> was passed as argument | ||
var image = new Image(); | ||
if (src.src) { | ||
image = src; | ||
src = src.src; | ||
// if image is just a <img>, don't cache it | ||
if (image.src) { | ||
return trackLoading(image); | ||
} | ||
if (!load[src]) { | ||
load[src] = new Promise(function (resolve, reject) { | ||
image.addEventListener('load', function () { return resolve(image); }); | ||
image.addEventListener('error', function () { return reject(image); }); | ||
if (!image.src) { | ||
image.src = src; | ||
} | ||
if (image.complete) { | ||
resolve(image); | ||
} | ||
}); | ||
load[src].image = image; | ||
// load is treated as a map, assumes all image paths don't clash with Function.prototype | ||
if (!load[image]) { | ||
load[image] = trackLoading(new Image(), image); | ||
} | ||
return load[src]; | ||
return load[image]; | ||
} | ||
load.unload = function (src) { | ||
// an <img> was passed as argument | ||
if (src.src) { | ||
src = src.src; | ||
load.unload = function (image) { | ||
if (!image.src) { | ||
// an <img> was passed as argument, so nothing to unload | ||
return; | ||
} | ||
@@ -42,8 +45,8 @@ | ||
// load(['1.jpg', '2.jpg']) | ||
if (src.map) { | ||
src.map(load.unload); | ||
} else if (load[src]) { | ||
if (image.map) { | ||
image.map(load.unload); | ||
} else if (load[image]) { | ||
// GC, http://www.fngtps.com/2010/mobile-safari-image-resource-limit-workaround/ | ||
load[src].image.src = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='; | ||
delete load[src]; | ||
load[image].image.src = 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='; | ||
delete load[image]; | ||
} | ||
@@ -50,0 +53,0 @@ }; |
{ | ||
"name": "image-promise", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "Load an image and return a promise in the browser, in 0.3KB", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -7,3 +7,3 @@ # image-promise | ||
[![Travis build status](https://api.travis-ci.org/bfred-it/image-promise.svg?branch=master)](https://travis-ci.org/bfred-it/image-promise) | ||
[![gzipped size](https://img.shields.io/npm/v/image-promise.svg)](https://www.npmjs.com/package/image-promise) | ||
[![npm version](https://img.shields.io/npm/v/image-promise.svg)](https://www.npmjs.com/package/image-promise) | ||
@@ -10,0 +10,0 @@ ## Install |
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
6631
95