提交 a99079f1 编写于 作者: J Johannes Rieken

remove winjs-based polyfill promse #53526

上级 33349fa5
......@@ -11,7 +11,6 @@ import { SimpleWorkerClient, create as create1 } from './vs/base/common/worker/s
import { create as create2 } from './vs/editor/common/services/editorSimpleWorker';
import { QuickOpenWidget } from './vs/base/parts/quickopen/browser/quickOpenWidget';
import { SyncDescriptor0, SyncDescriptor1, SyncDescriptor2, SyncDescriptor3, SyncDescriptor4, SyncDescriptor5, SyncDescriptor6, SyncDescriptor7, SyncDescriptor8 } from './vs/platform/instantiation/common/descriptors';
import { PolyfillPromise } from './vs/base/common/winjs.polyfill.promise';
import { DiffNavigator } from './vs/editor/browser/widget/diffNavigator';
import * as editorAPI from './vs/editor/editor.api';
......
......@@ -136,7 +136,6 @@
"./vs/base/test/common/types.test.ts",
"./vs/base/test/common/utils.ts",
"./vs/base/test/common/uuid.test.ts",
"./vs/base/test/common/winjs.polyfill.promise.test.ts",
"./vs/base/test/common/winjs.promise.test.ts",
"./vs/base/test/node/console.test.ts",
"./vs/base/test/node/decoder.test.ts",
......@@ -772,4 +771,4 @@
"exclude": [
"./typings/require-monaco.d.ts"
]
}
\ No newline at end of file
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Promise as WinJSPromise } from './winjs.base';
import * as platform from 'vs/base/common/platform';
import { isThenable } from 'vs/base/common/async';
function isWinJSPromise(candidate: any): candidate is WinJSPromise {
return isThenable(candidate) && typeof (candidate as any).done === 'function';
}
declare class WinJSPromiseRemovals {
any<T=any>(promises: (T | PromiseLike<T>)[]): WinJSPromise<{ key: string; value: WinJSPromise<T>; }>;
}
/**
* A polyfill for the native promises. The implementation is based on
* WinJS promises but tries to gap differences between winjs promises
* and native promises.
*/
export class PolyfillPromise<T = any> implements Promise<T> {
static all(thenables: Thenable<any>[]): PolyfillPromise {
return new PolyfillPromise(WinJSPromise.join(thenables).then(null, values => {
// WinJSPromise returns a sparse array whereas
// native promises return the *first* error
for (var key in values) {
if (values.hasOwnProperty(key)) {
return values[key];
}
}
}));
}
static race(thenables: Thenable<any>[]): PolyfillPromise {
// WinJSPromise returns `{ key: <index/key>, value: <promise> }`
// from the `any` call and Promise.race just wants the value
return new PolyfillPromise((WinJSPromise as any as WinJSPromiseRemovals).any(thenables).then(entry => entry.value, err => err.value));
}
static resolve(value): PolyfillPromise {
return new PolyfillPromise(WinJSPromise.wrap(value));
}
static reject(value): PolyfillPromise {
return new PolyfillPromise(WinJSPromise.wrapError(value));
}
private _winjsPromise: WinJSPromise;
constructor(winjsPromise: WinJSPromise);
constructor(callback: (resolve: (value?: T) => void, reject: (err?: any) => void) => any);
constructor(initOrPromise: WinJSPromise | ((resolve: (value?: T) => void, reject: (err?: any) => void) => any)) {
if (isWinJSPromise(initOrPromise)) {
this._winjsPromise = initOrPromise;
} else {
this._winjsPromise = new WinJSPromise((resolve, reject) => {
let initializing = true;
initOrPromise(function (value) {
if (!initializing) {
resolve(value);
} else {
platform.setImmediate(() => resolve(value));
}
}, function (err) {
if (!initializing) {
reject(err);
} else {
platform.setImmediate(() => reject(err));
}
});
initializing = false;
});
}
}
then(onFulfilled?: any, onRejected?: any): PolyfillPromise {
let sync = true;
// To support chaining, we need to return the value of the
// onFulfilled and onRejected callback.
// WinJSPromise supports a flat-map style #then, ie. the callbacks
// passed to WinJSPromise#then can return a Promise.
let promise = new PolyfillPromise(this._winjsPromise.then(
onFulfilled && function (value) {
if (!sync) {
return onFulfilled(value);
} else {
return new WinJSPromise((resolve, reject) => {
platform.setImmediate(() => {
let result;
try {
result = onFulfilled(value);
}
catch (err2) {
reject(err2);
return;
}
resolve(result);
});
});
}
},
onRejected && function (err) {
if (!sync) {
return onRejected(err);
} else {
return new WinJSPromise((resolve, reject) => {
platform.setImmediate(() => {
let result;
try {
result = onRejected(err);
}
catch (err2) {
reject(err2);
return;
}
resolve(result);
});
});
}
}
));
sync = false;
return promise;
}
catch(onRejected?: any): PolyfillPromise {
return this.then(null, onRejected);
}
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { Promise as WinJSPromise } from 'vs/base/common/winjs.base';
import { PolyfillPromise } from 'vs/base/common/winjs.polyfill.promise';
suite('Polyfill Promise', function () {
test('sync-resolve, NativePromise', function () {
// native promise behaviour
const actual: string[] = [];
const promise = new Promise(resolve => {
actual.push('inCtor');
resolve(void 0);
}).then(() => actual.push('inThen'));
actual.push('afterCtor');
return promise.then(() => {
assert.deepEqual(actual, ['inCtor', 'afterCtor', 'inThen']);
});
});
test('sync-resolve, WinJSPromise', function () {
// winjs promise behaviour
const actual: string[] = [];
const promise = new WinJSPromise(resolve => {
actual.push('inCtor');
resolve(null);
}).then(() => actual.push('inThen'));
actual.push('afterCtor');
return promise.then(() => {
assert.deepEqual(actual, ['inCtor', 'inThen', 'afterCtor']);
});
});
test('sync-resolve, PolyfillPromise', function () {
// winjs promise behaviour
const actual: string[] = [];
const promise = new PolyfillPromise(resolve => {
actual.push('inCtor');
resolve(null);
}).then(() => actual.push('inThen'));
actual.push('afterCtor');
return promise.then(() => {
assert.deepEqual(actual, ['inCtor', 'afterCtor', 'inThen']);
});
});
test('sync-then, NativePromise', function () {
const actual: string[] = [];
const promise = Promise.resolve(123).then(() => actual.push('inThen'));
actual.push('afterThen');
return promise.then(() => {
assert.deepEqual(actual, ['afterThen', 'inThen']);
});
});
test('sync-then, WinJSPromise', function () {
const actual: string[] = [];
const promise = WinJSPromise.as(123).then(() => actual.push('inThen'));
actual.push('afterThen');
return promise.then(() => {
assert.deepEqual(actual, ['inThen', 'afterThen']);
});
});
test('sync-then, PolyfillPromise', function () {
const actual: string[] = [];
const promise = PolyfillPromise.resolve(123).then(() => actual.push('inThen'));
actual.push('afterThen');
return promise.then(() => {
assert.deepEqual(actual, ['afterThen', 'inThen']);
});
});
test('PolyfillPromise, executor has two params', function () {
return new PolyfillPromise(function () {
assert.equal(arguments.length, 2);
assert.equal(typeof arguments[0], 'function');
assert.equal(typeof arguments[1], 'function');
arguments[0]();
});
});
test('Promises polyfill does not support chaining then and catch #57722', function () {
return PolyfillPromise.resolve(1).then(function (x) { return x + 1; }).then(function (x) {
assert.equal(x, 2);
});
});
// run the same tests for the native and polyfill promise
(<any[]>[Promise, PolyfillPromise]).forEach(PromiseCtor => {
test(PromiseCtor.name + ', resolved value', function () {
return new PromiseCtor((resolve: Function) => resolve(1)).then((value: number) => assert.equal(value, 1));
});
test(PromiseCtor.name + ', rejected value', function () {
return new PromiseCtor((_: Function, reject: Function) => reject(1)).then(null, (value: number) => assert.equal(value, 1));
});
test(PromiseCtor.name + ', catch', function () {
return new PromiseCtor((_: Function, reject: Function) => reject(1)).catch((value: number) => assert.equal(value, 1));
});
test(PromiseCtor.name + ', static-resolve', function () {
return PromiseCtor.resolve(42).then((value: number) => assert.equal(value, 42));
});
test(PromiseCtor.name + ', static-reject', function () {
return PromiseCtor.reject(42).then(null, (value: number) => assert.equal(value, 42));
});
test(PromiseCtor.name + ', static-all, 1', function () {
return PromiseCtor.all([
PromiseCtor.resolve(1),
PromiseCtor.resolve(2)
]).then((values: number[]) => {
assert.deepEqual(values, [1, 2]);
});
});
test(PromiseCtor.name + ', static-all, 2', function () {
return PromiseCtor.all([
PromiseCtor.resolve(1),
3,
PromiseCtor.resolve(2)
]).then((values: number[]) => {
assert.deepEqual(values, [1, 3, 2]);
});
});
test(PromiseCtor.name + ', static-all, 3', function () {
return PromiseCtor.all([
PromiseCtor.resolve(1),
PromiseCtor.reject(13),
PromiseCtor.reject(12),
]).catch((values: number) => {
assert.deepEqual(values, 13);
});
});
test(PromiseCtor.name + ', static-race, 1', function () {
return PromiseCtor.race([
PromiseCtor.resolve(1),
PromiseCtor.resolve(2),
]).then((value: number) => {
assert.deepEqual(value, 1);
});
});
test(PromiseCtor.name + ', static-race, 2', function () {
return PromiseCtor.race([
PromiseCtor.reject(-1),
PromiseCtor.resolve(2),
]).catch((value: number) => {
assert.deepEqual(value, -1);
});
});
test(PromiseCtor.name + ', static-race, 3', function () {
return PromiseCtor.race([
PromiseCtor.resolve(1),
PromiseCtor.reject(2),
]).then((value: number) => {
assert.deepEqual(value, 1);
});
});
test(PromiseCtor.name + ', throw in ctor', function () {
return new PromiseCtor(() => {
throw new Error('sooo bad');
}).catch((err: Error) => {
assert.equal(err.message, 'sooo bad');
});
});
});
});
......@@ -362,6 +362,7 @@ declare namespace monaco {
*/
MAX_VALUE = 112
}
export class KeyMod {
static readonly CtrlCmd: number;
static readonly Shift: number;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册