diff --git a/src/http/client/index.js b/src/http/client/index.js index 56cd5993..bcfd3095 100644 --- a/src/http/client/index.js +++ b/src/http/client/index.js @@ -2,10 +2,9 @@ * Base client. */ -import Promise from '../../promise'; import xhrClient from './xhr'; import nodeClient from './node'; -import {warn, when, isObject, isFunction, inBrowser} from '../../util'; +import {log, when, isObject, isFunction, inBrowser} from '../../util'; export default function (context) { @@ -45,7 +44,7 @@ export default function (context) { } } else { - warn(`Invalid interceptor of type ${typeof handler}, must be a function`); + log(`Invalid interceptor of type ${typeof handler}, must be a function`); } } } diff --git a/src/http/client/jsonp.js b/src/http/client/jsonp.js index 241e7b40..2680cd9b 100644 --- a/src/http/client/jsonp.js +++ b/src/http/client/jsonp.js @@ -2,8 +2,6 @@ * JSONP client (Browser). */ -import Promise from '../../promise'; - export default function (request) { return new Promise(resolve => { diff --git a/src/http/client/node.js b/src/http/client/node.js index d12b33e1..c3bfc40d 100644 --- a/src/http/client/node.js +++ b/src/http/client/node.js @@ -2,7 +2,6 @@ * Http client (Node). */ -import Promise from '../../promise'; import {each, trim} from '../../util'; export default function (request) { diff --git a/src/http/client/xdr.js b/src/http/client/xdr.js index 20656c74..a0298658 100644 --- a/src/http/client/xdr.js +++ b/src/http/client/xdr.js @@ -2,8 +2,6 @@ * XDomain client (Internet Explorer). */ -import Promise from '../../promise'; - export default function (request) { return new Promise(resolve => { diff --git a/src/http/client/xhr.js b/src/http/client/xhr.js index cc771872..81244000 100644 --- a/src/http/client/xhr.js +++ b/src/http/client/xhr.js @@ -2,7 +2,6 @@ * XMLHttp client (Browser). */ -import Promise from '../../promise'; import {each, trim, isFunction} from '../../util'; export default function (request) { diff --git a/src/http/index.js b/src/http/index.js index df4743de..b24f9b2d 100644 --- a/src/http/index.js +++ b/src/http/index.js @@ -14,7 +14,6 @@ import method from './interceptor/method'; import header from './interceptor/header'; import Client from './client/index'; import Request from './request'; -import Promise from '../promise'; import {assign, defaults, error, isString, isFunction} from '../util'; export default function Http(options) { diff --git a/src/http/response.js b/src/http/response.js index d397a5a5..8e6ab663 100644 --- a/src/http/response.js +++ b/src/http/response.js @@ -3,7 +3,6 @@ */ import Headers from './headers'; -import Promise from '../promise'; import {when, isBlob, isString} from '../util'; export default class Response { diff --git a/src/index.js b/src/index.js index 7308dee1..1a0d2ffe 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,6 @@ import Url from './url/index'; import Http from './http/index'; -import Promise from './promise'; import Resource from './resource'; import Util, {options} from './util'; @@ -19,7 +18,6 @@ function plugin(Vue) { Vue.url = Url; Vue.http = Http; Vue.resource = Resource; - Vue.Promise = Promise; Object.defineProperties(Vue.prototype, { @@ -39,12 +37,6 @@ function plugin(Vue) { get() { return Vue.resource.bind(this); } - }, - - $promise: { - get() { - return (executor) => new Vue.Promise(executor, this); - } } }); diff --git a/src/lib/promise.js b/src/lib/promise.js deleted file mode 100644 index b3cce746..00000000 --- a/src/lib/promise.js +++ /dev/null @@ -1,177 +0,0 @@ -/** - * Promises/A+ polyfill v1.1.4 (https://github.com/bramstein/promis) - */ - -const RESOLVED = 0; -const REJECTED = 1; -const PENDING = 2; - -import {nextTick} from '../util'; - -export default function Promise(executor) { - - this.state = PENDING; - this.value = undefined; - this.deferred = []; - - var promise = this; - - try { - executor(function (x) { - promise.resolve(x); - }, function (r) { - promise.reject(r); - }); - } catch (e) { - promise.reject(e); - } -} - -Promise.reject = function (r) { - return new Promise(function (resolve, reject) { - reject(r); - }); -}; - -Promise.resolve = function (x) { - return new Promise(function (resolve, reject) { - resolve(x); - }); -}; - -Promise.all = function all(iterable) { - return new Promise(function (resolve, reject) { - var count = 0, result = []; - - if (iterable.length === 0) { - resolve(result); - } - - function resolver(i) { - return function (x) { - result[i] = x; - count += 1; - - if (count === iterable.length) { - resolve(result); - } - }; - } - - for (var i = 0; i < iterable.length; i += 1) { - Promise.resolve(iterable[i]).then(resolver(i), reject); - } - }); -}; - -Promise.race = function race(iterable) { - return new Promise(function (resolve, reject) { - for (var i = 0; i < iterable.length; i += 1) { - Promise.resolve(iterable[i]).then(resolve, reject); - } - }); -}; - -var p = Promise.prototype; - -p.resolve = function resolve(x) { - var promise = this; - - if (promise.state === PENDING) { - if (x === promise) { - throw new TypeError('Promise settled with itself.'); - } - - var called = false; - - try { - var then = x && x['then']; - - if (x !== null && typeof x === 'object' && typeof then === 'function') { - then.call(x, function (x) { - if (!called) { - promise.resolve(x); - } - called = true; - - }, function (r) { - if (!called) { - promise.reject(r); - } - called = true; - }); - return; - } - } catch (e) { - if (!called) { - promise.reject(e); - } - return; - } - - promise.state = RESOLVED; - promise.value = x; - promise.notify(); - } -}; - -p.reject = function reject(reason) { - var promise = this; - - if (promise.state === PENDING) { - if (reason === promise) { - throw new TypeError('Promise settled with itself.'); - } - - promise.state = REJECTED; - promise.value = reason; - promise.notify(); - } -}; - -p.notify = function notify() { - var promise = this; - - nextTick(function () { - if (promise.state !== PENDING) { - while (promise.deferred.length) { - var deferred = promise.deferred.shift(), - onResolved = deferred[0], - onRejected = deferred[1], - resolve = deferred[2], - reject = deferred[3]; - - try { - if (promise.state === RESOLVED) { - if (typeof onResolved === 'function') { - resolve(onResolved.call(undefined, promise.value)); - } else { - resolve(promise.value); - } - } else if (promise.state === REJECTED) { - if (typeof onRejected === 'function') { - resolve(onRejected.call(undefined, promise.value)); - } else { - reject(promise.value); - } - } - } catch (e) { - reject(e); - } - } - } - }); -}; - -p.then = function then(onResolved, onRejected) { - var promise = this; - - return new Promise(function (resolve, reject) { - promise.deferred.push([onResolved, onRejected, resolve, reject]); - promise.notify(); - }); -}; - -p.catch = function (onRejected) { - return this.then(undefined, onRejected); -}; diff --git a/src/promise.js b/src/promise.js deleted file mode 100644 index 7311f083..00000000 --- a/src/promise.js +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Promise adapter. - */ - -import PromiseLib from './lib/promise'; - -if (typeof Promise === 'undefined') { - window.Promise = PromiseLib; -} - -export default function PromiseObj(executor, context) { - - if (executor instanceof Promise) { - this.promise = executor; - } else { - this.promise = new Promise(executor.bind(context)); - } - - this.context = context; -} - -PromiseObj.all = function (iterable, context) { - return new PromiseObj(Promise.all(iterable), context); -}; - -PromiseObj.resolve = function (value, context) { - return new PromiseObj(Promise.resolve(value), context); -}; - -PromiseObj.reject = function (reason, context) { - return new PromiseObj(Promise.reject(reason), context); -}; - -PromiseObj.race = function (iterable, context) { - return new PromiseObj(Promise.race(iterable), context); -}; - -var p = PromiseObj.prototype; - -p.bind = function (context) { - this.context = context; - return this; -}; - -p.then = function (fulfilled, rejected) { - - if (fulfilled && fulfilled.bind && this.context) { - fulfilled = fulfilled.bind(this.context); - } - - if (rejected && rejected.bind && this.context) { - rejected = rejected.bind(this.context); - } - - return new PromiseObj(this.promise.then(fulfilled, rejected), this.context); -}; - -p.catch = function (rejected) { - - if (rejected && rejected.bind && this.context) { - rejected = rejected.bind(this.context); - } - - return new PromiseObj(this.promise.catch(rejected), this.context); -}; - -p.finally = function (callback) { - - return this.then(function (value) { - callback.call(this); - return value; - }, function (reason) { - callback.call(this); - return Promise.reject(reason); - } - ); -}; diff --git a/src/util.js b/src/util.js index 2a62f784..316e46f2 100644 --- a/src/util.js +++ b/src/util.js @@ -2,20 +2,19 @@ * Utility functions. */ -import Promise from './promise'; +let _config = {}; -var {hasOwnProperty} = {}, {slice} = [], debug = false, ntick; +const {hasOwnProperty} = {}, {slice} = []; export const inBrowser = typeof window !== 'undefined'; -export default function ({config, nextTick}) { - ntick = nextTick; - debug = config.debug || !config.silent; +export default function ({config}) { + _config = config; } -export function warn(msg) { - if (typeof console !== 'undefined' && debug) { - console.warn('[VueResource warn]: ' + msg); +export function log(message, color = '#41B883') { + if (typeof console !== 'undefined' && _config.devtools) { + console.log(`%c vue-resource %c ${message} `, 'color: #fff; background: #35495E; padding: 1px; border-radius: 3px 0 0 3px;', `color: #fff; background: ${color}; padding: 1px; border-radius: 0 3px 3px 0;`); } } @@ -25,10 +24,6 @@ export function error(msg) { } } -export function nextTick(cb, ctx) { - return ntick(cb, ctx); -} - export function trim(str) { return str ? str.replace(/^\s*|\s*$/g, '') : ''; } diff --git a/test/index.js b/test/index.js index 97ea681d..c6556aa1 100644 --- a/test/index.js +++ b/test/index.js @@ -6,4 +6,3 @@ Vue.use(VueResource); require('./url'); require('./http'); require('./resource'); -require('./promise'); diff --git a/test/promise.js b/test/promise.js deleted file mode 100644 index e3a685b3..00000000 --- a/test/promise.js +++ /dev/null @@ -1,125 +0,0 @@ -import Promise from '../src/promise'; - -describe('Vue.promise ' + (window.Promise !== undefined ? '(native)' : '(polyfill)'), function () { - - it('then fulfill', function (done) { - - Promise.resolve(1).then(function (value) { - expect(value).toBe(1); - done(); - }); - - }); - - it('then reject', function (done) { - - Promise.reject(1).then(undefined, function (value) { - expect(value).toBe(1); - done(); - }); - - }); - - it('catch', function (done) { - - Promise.reject(1).catch(function (value) { - expect(value).toBe(1); - done(); - }); - - }); - - it('finally fulfill', function (done) { - - Promise.resolve(1).finally(function (arg) { - expect(arg).toBe(undefined); - }).then(function (arg) { - expect(arg).toBe(1); - done(); - }); - - }); - - it('finally reject', function (done) { - - Promise.reject(1).finally(function (arg) { - expect(arg).toBe(undefined); - }).catch(function (arg) { - expect(arg).toBe(1); - done(); - }); - - }); - - it('all', function (done) { - - Promise.all([ - - Promise.resolve(1), - Promise.resolve(2) - - ]).then(function (values) { - expect(values[0]).toBe(1); - expect(values[1]).toBe(2); - done(); - }); - - }); - - it('duplicate', function (done) { - - Promise.all([ - - Promise.resolve(1).then(function (value) { - expect(value).toBe(1); - }), - - Promise.resolve(2).then(function (value) { - expect(value).toBe(2); - }) - - ]).then(done); - - }); - - it('context', function (done) { - - var context = {foo: 'bar'}; - - Promise.resolve().bind(context).then(function () { - expect(this).toBe(context); - done(); - }); - - }); - - it('context chain fulfill', function (done) { - - var context = {foo: 'bar'}; - - Promise.resolve().bind(context).catch(undefined).finally(function () { - expect(this).toBe(context); - }).then(function () { - expect(this).toBe(context); - done(); - }); - - }); - - it('context chain reject', function (done) { - - var context = {foo: 'bar'}; - - Promise.reject().bind(context).catch(function () { - expect(this).toBe(context); - return Promise.reject(); - }).finally(function () { - expect(this).toBe(context); - }).catch(function () { - expect(this).toBe(context); - done(); - }); - - }); - -});