[refactor] bump version 1.0.2

pull/1/head
Ziqiang Li 2017-08-07 21:56:13 +08:00
parent 10ae6427ec
commit 68077ec0eb
2 changed files with 28 additions and 16 deletions

View File

@ -4,6 +4,7 @@
// Callback index. // Callback index.
let count = 0; let count = 0;
/** /**
* JSONP handler * JSONP handler
* *
@ -13,7 +14,7 @@ let count = 0;
* - timeout {Number} how long after the request until a timeout error * - timeout {Number} how long after the request until a timeout error
* is emitted (defaults to `15000`) * is emitted (defaults to `15000`)
*/ */
let jsonp = function( url, options ) { function jsonp( url, options ) {
options = options || {}; options = options || {};
let prefix = options.prefix || '__jp'; let prefix = options.prefix || '__jp';
let callback = options.callback || 'callback'; let callback = options.callback || 'callback';
@ -22,22 +23,34 @@ let jsonp = function( url, options ) {
let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head; let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head;
let script; let script;
let timer; let timer;
let cleanup;
let promise; let promise;
let noop = function() {};
// Generate a unique id for the request. // Generate a unique id for the request.
let id = prefix + (count++); let id = prefix + (count++);
cleanup = function() {
function noop() {}
function cleanup() {
// Remove the script tag. // Remove the script tag.
if ( script && script.parentNode ) { if ( script && script.parentNode ) {
script.parentNode.removeChild( script ); script.parentNode.removeChild( script );
} }
window[ id ] = noop; window[ id ] = noop;
if ( timer ) { clearTimeout( timer ); } if ( timer ) { clearTimeout( timer ); }
}; }
promise = new Promise( ( resolve, reject ) => {
function serialize( params ) {
let param = '';
for ( let key in params ) {
if ( params.hasOwnProperty( key ) ) {
param += `&${key}=${encodeURIComponent( params[ key ] )}`;
}
}
return param;
}
promise = new Promise( ( resolve, reject ) => {
if ( timeout ) { if ( timeout ) {
timer = setTimeout( function() { timer = setTimeout( () => {
cleanup(); cleanup();
reject( new Error( 'Timeout' ) ); reject( new Error( 'Timeout' ) );
}, timeout ); }, timeout );
@ -47,19 +60,18 @@ let jsonp = function( url, options ) {
resolve( data ); resolve( data );
}; };
params[ callback ] = id; params[ callback ] = id;
for ( let key in params ) { url += serialize( params );
url += (~url.indexOf( '?' ) ? '&' : '?') + key + '=' + encodeURIComponent( params[ key ] ); url = url.replace( '?&', '?' );
}
url = url.replace( '?&', '?' );
// Create script. // Create script.
script = document.createElement( 'script' ); script = document.createElement( 'script' );
script.src = url; script.src = url;
script.onerror = function() { script.onerror = function() {
cleanup(); cleanup();
reject( new Error( 'Network Error' ) ); reject( new Error( 'Network Error' ) );
}; };
target.parentNode.insertBefore( script, target ); target.parentNode.insertBefore( script, target );
} ); } );
return promise; return promise;
}; }
export default jsonp; export default jsonp;

View File

@ -1,6 +1,6 @@
{ {
"name": "simple-jsonp-promise", "name": "simple-jsonp-promise",
"version": "1.0.1", "version": "1.0.2",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {