monaco-editor
Version:
A browser based code editor
1,191 lines (1,100 loc) • 95.8 kB
JavaScript
/**
* Extracted from https://github.com/winjs/winjs
* Version: 4.4.0(ec3258a9f3a36805a187848984e3bb938044178d)
* Copyright (c) Microsoft Corporation.
* All Rights Reserved.
* Licensed under the MIT License.
*/
var __winjs_exports;
(function() {
var _modules = Object.create(null);//{};
_modules["WinJS/Core/_WinJS"] = {};
var _winjs = function(moduleId, deps, factory) {
var exports = {};
var exportsPassedIn = false;
var depsValues = deps.map(function(dep) {
if (dep === 'exports') {
exportsPassedIn = true;
return exports;
}
return _modules[dep];
});
var result = factory.apply({}, depsValues);
_modules[moduleId] = exportsPassedIn ? exports : result;
};
_winjs("WinJS/Core/_Global", [], function () {
"use strict";
// Appease jshint
/* global window, self, global */
var globalObject =
typeof window !== 'undefined' ? window :
typeof self !== 'undefined' ? self :
typeof global !== 'undefined' ? global :
{};
return globalObject;
});
_winjs("WinJS/Core/_BaseCoreUtils", ["WinJS/Core/_Global"], function baseCoreUtilsInit(_Global) {
"use strict";
var hasWinRT = !!_Global.Windows;
function markSupportedForProcessing(func) {
/// <signature helpKeyword="WinJS.Utilities.markSupportedForProcessing">
/// <summary locid="WinJS.Utilities.markSupportedForProcessing">
/// Marks a function as being compatible with declarative processing, such as WinJS.UI.processAll
/// or WinJS.Binding.processAll.
/// </summary>
/// <param name="func" type="Function" locid="WinJS.Utilities.markSupportedForProcessing_p:func">
/// The function to be marked as compatible with declarative processing.
/// </param>
/// <returns type="Function" locid="WinJS.Utilities.markSupportedForProcessing_returnValue">
/// The input function.
/// </returns>
/// </signature>
func.supportedForProcessing = true;
return func;
}
var actualSetImmediate = null;
return {
hasWinRT: hasWinRT,
markSupportedForProcessing: markSupportedForProcessing,
_setImmediate: function (callback) {
// BEGIN monaco change
if (actualSetImmediate === null) {
if (_Global.setImmediate) {
actualSetImmediate = _Global.setImmediate.bind(_Global);
} else if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
actualSetImmediate = process.nextTick.bind(process);
} else {
actualSetImmediate = _Global.setTimeout.bind(_Global);
}
}
actualSetImmediate(callback);
// END monaco change
}
};
});
_winjs("WinJS/Core/_WriteProfilerMark", ["WinJS/Core/_Global"], function profilerInit(_Global) {
"use strict";
return _Global.msWriteProfilerMark || function () { };
});
_winjs("WinJS/Core/_Base", ["WinJS/Core/_WinJS","WinJS/Core/_Global","WinJS/Core/_BaseCoreUtils","WinJS/Core/_WriteProfilerMark"], function baseInit(_WinJS, _Global, _BaseCoreUtils, _WriteProfilerMark) {
"use strict";
function initializeProperties(target, members, prefix) {
var keys = Object.keys(members);
var isArray = Array.isArray(target);
var properties;
var i, len;
for (i = 0, len = keys.length; i < len; i++) {
var key = keys[i];
var enumerable = key.charCodeAt(0) !== /*_*/95;
var member = members[key];
if (member && typeof member === 'object') {
if (member.value !== undefined || typeof member.get === 'function' || typeof member.set === 'function') {
if (member.enumerable === undefined) {
member.enumerable = enumerable;
}
if (prefix && member.setName && typeof member.setName === 'function') {
member.setName(prefix + "." + key);
}
properties = properties || {};
properties[key] = member;
continue;
}
}
if (!enumerable) {
properties = properties || {};
properties[key] = { value: member, enumerable: enumerable, configurable: true, writable: true };
continue;
}
if (isArray) {
target.forEach(function (target) {
target[key] = member;
});
} else {
target[key] = member;
}
}
if (properties) {
if (isArray) {
target.forEach(function (target) {
Object.defineProperties(target, properties);
});
} else {
Object.defineProperties(target, properties);
}
}
}
(function () {
var _rootNamespace = _WinJS;
if (!_rootNamespace.Namespace) {
_rootNamespace.Namespace = Object.create(Object.prototype);
}
function createNamespace(parentNamespace, name) {
var currentNamespace = parentNamespace || {};
if (name) {
var namespaceFragments = name.split(".");
if (currentNamespace === _Global && namespaceFragments[0] === "WinJS") {
currentNamespace = _WinJS;
namespaceFragments.splice(0, 1);
}
for (var i = 0, len = namespaceFragments.length; i < len; i++) {
var namespaceName = namespaceFragments[i];
if (!currentNamespace[namespaceName]) {
Object.defineProperty(currentNamespace, namespaceName,
{ value: {}, writable: false, enumerable: true, configurable: true }
);
}
currentNamespace = currentNamespace[namespaceName];
}
}
return currentNamespace;
}
function defineWithParent(parentNamespace, name, members) {
/// <signature helpKeyword="WinJS.Namespace.defineWithParent">
/// <summary locid="WinJS.Namespace.defineWithParent">
/// Defines a new namespace with the specified name under the specified parent namespace.
/// </summary>
/// <param name="parentNamespace" type="Object" locid="WinJS.Namespace.defineWithParent_p:parentNamespace">
/// The parent namespace.
/// </param>
/// <param name="name" type="String" locid="WinJS.Namespace.defineWithParent_p:name">
/// The name of the new namespace.
/// </param>
/// <param name="members" type="Object" locid="WinJS.Namespace.defineWithParent_p:members">
/// The members of the new namespace.
/// </param>
/// <returns type="Object" locid="WinJS.Namespace.defineWithParent_returnValue">
/// The newly-defined namespace.
/// </returns>
/// </signature>
var currentNamespace = createNamespace(parentNamespace, name);
if (members) {
initializeProperties(currentNamespace, members, name || "<ANONYMOUS>");
}
return currentNamespace;
}
function define(name, members) {
/// <signature helpKeyword="WinJS.Namespace.define">
/// <summary locid="WinJS.Namespace.define">
/// Defines a new namespace with the specified name.
/// </summary>
/// <param name="name" type="String" locid="WinJS.Namespace.define_p:name">
/// The name of the namespace. This could be a dot-separated name for nested namespaces.
/// </param>
/// <param name="members" type="Object" locid="WinJS.Namespace.define_p:members">
/// The members of the new namespace.
/// </param>
/// <returns type="Object" locid="WinJS.Namespace.define_returnValue">
/// The newly-defined namespace.
/// </returns>
/// </signature>
return defineWithParent(_Global, name, members);
}
var LazyStates = {
uninitialized: 1,
working: 2,
initialized: 3,
};
function lazy(f) {
var name;
var state = LazyStates.uninitialized;
var result;
return {
setName: function (value) {
name = value;
},
get: function () {
switch (state) {
case LazyStates.initialized:
return result;
case LazyStates.uninitialized:
state = LazyStates.working;
try {
_WriteProfilerMark("WinJS.Namespace._lazy:" + name + ",StartTM");
result = f();
} finally {
_WriteProfilerMark("WinJS.Namespace._lazy:" + name + ",StopTM");
state = LazyStates.uninitialized;
}
f = null;
state = LazyStates.initialized;
return result;
case LazyStates.working:
throw "Illegal: reentrancy on initialization";
default:
throw "Illegal";
}
},
set: function (value) {
switch (state) {
case LazyStates.working:
throw "Illegal: reentrancy on initialization";
default:
state = LazyStates.initialized;
result = value;
break;
}
},
enumerable: true,
configurable: true,
};
}
// helper for defining AMD module members
function moduleDefine(exports, name, members) {
var target = [exports];
var publicNS = null;
if (name) {
publicNS = createNamespace(_Global, name);
target.push(publicNS);
}
initializeProperties(target, members, name || "<ANONYMOUS>");
return publicNS;
}
// Establish members of the "WinJS.Namespace" namespace
Object.defineProperties(_rootNamespace.Namespace, {
defineWithParent: { value: defineWithParent, writable: true, enumerable: true, configurable: true },
define: { value: define, writable: true, enumerable: true, configurable: true },
_lazy: { value: lazy, writable: true, enumerable: true, configurable: true },
_moduleDefine: { value: moduleDefine, writable: true, enumerable: true, configurable: true }
});
})();
(function () {
function define(constructor, instanceMembers, staticMembers) {
/// <signature helpKeyword="WinJS.Class.define">
/// <summary locid="WinJS.Class.define">
/// Defines a class using the given constructor and the specified instance members.
/// </summary>
/// <param name="constructor" type="Function" locid="WinJS.Class.define_p:constructor">
/// A constructor function that is used to instantiate this class.
/// </param>
/// <param name="instanceMembers" type="Object" locid="WinJS.Class.define_p:instanceMembers">
/// The set of instance fields, properties, and methods made available on the class.
/// </param>
/// <param name="staticMembers" type="Object" locid="WinJS.Class.define_p:staticMembers">
/// The set of static fields, properties, and methods made available on the class.
/// </param>
/// <returns type="Function" locid="WinJS.Class.define_returnValue">
/// The newly-defined class.
/// </returns>
/// </signature>
constructor = constructor || function () { };
_BaseCoreUtils.markSupportedForProcessing(constructor);
if (instanceMembers) {
initializeProperties(constructor.prototype, instanceMembers);
}
if (staticMembers) {
initializeProperties(constructor, staticMembers);
}
return constructor;
}
function derive(baseClass, constructor, instanceMembers, staticMembers) {
/// <signature helpKeyword="WinJS.Class.derive">
/// <summary locid="WinJS.Class.derive">
/// Creates a sub-class based on the supplied baseClass parameter, using prototypal inheritance.
/// </summary>
/// <param name="baseClass" type="Function" locid="WinJS.Class.derive_p:baseClass">
/// The class to inherit from.
/// </param>
/// <param name="constructor" type="Function" locid="WinJS.Class.derive_p:constructor">
/// A constructor function that is used to instantiate this class.
/// </param>
/// <param name="instanceMembers" type="Object" locid="WinJS.Class.derive_p:instanceMembers">
/// The set of instance fields, properties, and methods to be made available on the class.
/// </param>
/// <param name="staticMembers" type="Object" locid="WinJS.Class.derive_p:staticMembers">
/// The set of static fields, properties, and methods to be made available on the class.
/// </param>
/// <returns type="Function" locid="WinJS.Class.derive_returnValue">
/// The newly-defined class.
/// </returns>
/// </signature>
if (baseClass) {
constructor = constructor || function () { };
var basePrototype = baseClass.prototype;
constructor.prototype = Object.create(basePrototype);
_BaseCoreUtils.markSupportedForProcessing(constructor);
Object.defineProperty(constructor.prototype, "constructor", { value: constructor, writable: true, configurable: true, enumerable: true });
if (instanceMembers) {
initializeProperties(constructor.prototype, instanceMembers);
}
if (staticMembers) {
initializeProperties(constructor, staticMembers);
}
return constructor;
} else {
return define(constructor, instanceMembers, staticMembers);
}
}
function mix(constructor) {
/// <signature helpKeyword="WinJS.Class.mix">
/// <summary locid="WinJS.Class.mix">
/// Defines a class using the given constructor and the union of the set of instance members
/// specified by all the mixin objects. The mixin parameter list is of variable length.
/// </summary>
/// <param name="constructor" locid="WinJS.Class.mix_p:constructor">
/// A constructor function that is used to instantiate this class.
/// </param>
/// <returns type="Function" locid="WinJS.Class.mix_returnValue">
/// The newly-defined class.
/// </returns>
/// </signature>
constructor = constructor || function () { };
var i, len;
for (i = 1, len = arguments.length; i < len; i++) {
initializeProperties(constructor.prototype, arguments[i]);
}
return constructor;
}
// Establish members of "WinJS.Class" namespace
_WinJS.Namespace.define("WinJS.Class", {
define: define,
derive: derive,
mix: mix
});
})();
return {
Namespace: _WinJS.Namespace,
Class: _WinJS.Class
};
});
_winjs("WinJS/Core/_ErrorFromName", ["WinJS/Core/_Base"], function errorsInit(_Base) {
"use strict";
var ErrorFromName = _Base.Class.derive(Error, function (name, message) {
/// <signature helpKeyword="WinJS.ErrorFromName">
/// <summary locid="WinJS.ErrorFromName">
/// Creates an Error object with the specified name and message properties.
/// </summary>
/// <param name="name" type="String" locid="WinJS.ErrorFromName_p:name">The name of this error. The name is meant to be consumed programmatically and should not be localized.</param>
/// <param name="message" type="String" optional="true" locid="WinJS.ErrorFromName_p:message">The message for this error. The message is meant to be consumed by humans and should be localized.</param>
/// <returns type="Error" locid="WinJS.ErrorFromName_returnValue">Error instance with .name and .message properties populated</returns>
/// </signature>
this.name = name;
this.message = message || name;
}, {
/* empty */
}, {
supportedForProcessing: false,
});
_Base.Namespace.define("WinJS", {
// ErrorFromName establishes a simple pattern for returning error codes.
//
ErrorFromName: ErrorFromName
});
return ErrorFromName;
});
_winjs("WinJS/Core/_Events", ["exports","WinJS/Core/_Base"], function eventsInit(exports, _Base) {
"use strict";
function createEventProperty(name) {
var eventPropStateName = "_on" + name + "state";
return {
get: function () {
var state = this[eventPropStateName];
return state && state.userHandler;
},
set: function (handler) {
var state = this[eventPropStateName];
if (handler) {
if (!state) {
state = { wrapper: function (evt) { return state.userHandler(evt); }, userHandler: handler };
Object.defineProperty(this, eventPropStateName, { value: state, enumerable: false, writable:true, configurable: true });
this.addEventListener(name, state.wrapper, false);
}
state.userHandler = handler;
} else if (state) {
this.removeEventListener(name, state.wrapper, false);
this[eventPropStateName] = null;
}
},
enumerable: true
};
}
function createEventProperties() {
/// <signature helpKeyword="WinJS.Utilities.createEventProperties">
/// <summary locid="WinJS.Utilities.createEventProperties">
/// Creates an object that has one property for each name passed to the function.
/// </summary>
/// <param name="events" locid="WinJS.Utilities.createEventProperties_p:events">
/// A variable list of property names.
/// </param>
/// <returns type="Object" locid="WinJS.Utilities.createEventProperties_returnValue">
/// The object with the specified properties. The names of the properties are prefixed with 'on'.
/// </returns>
/// </signature>
var props = {};
for (var i = 0, len = arguments.length; i < len; i++) {
var name = arguments[i];
props["on" + name] = createEventProperty(name);
}
return props;
}
var EventMixinEvent = _Base.Class.define(
function EventMixinEvent_ctor(type, detail, target) {
this.detail = detail;
this.target = target;
this.timeStamp = Date.now();
this.type = type;
},
{
bubbles: { value: false, writable: false },
cancelable: { value: false, writable: false },
currentTarget: {
get: function () { return this.target; }
},
defaultPrevented: {
get: function () { return this._preventDefaultCalled; }
},
trusted: { value: false, writable: false },
eventPhase: { value: 0, writable: false },
target: null,
timeStamp: null,
type: null,
preventDefault: function () {
this._preventDefaultCalled = true;
},
stopImmediatePropagation: function () {
this._stopImmediatePropagationCalled = true;
},
stopPropagation: function () {
}
}, {
supportedForProcessing: false,
}
);
var eventMixin = {
_listeners: null,
addEventListener: function (type, listener, useCapture) {
/// <signature helpKeyword="WinJS.Utilities.eventMixin.addEventListener">
/// <summary locid="WinJS.Utilities.eventMixin.addEventListener">
/// Adds an event listener to the control.
/// </summary>
/// <param name="type" locid="WinJS.Utilities.eventMixin.addEventListener_p:type">
/// The type (name) of the event.
/// </param>
/// <param name="listener" locid="WinJS.Utilities.eventMixin.addEventListener_p:listener">
/// The listener to invoke when the event is raised.
/// </param>
/// <param name="useCapture" locid="WinJS.Utilities.eventMixin.addEventListener_p:useCapture">
/// if true initiates capture, otherwise false.
/// </param>
/// </signature>
useCapture = useCapture || false;
this._listeners = this._listeners || {};
var eventListeners = (this._listeners[type] = this._listeners[type] || []);
for (var i = 0, len = eventListeners.length; i < len; i++) {
var l = eventListeners[i];
if (l.useCapture === useCapture && l.listener === listener) {
return;
}
}
eventListeners.push({ listener: listener, useCapture: useCapture });
},
dispatchEvent: function (type, details) {
/// <signature helpKeyword="WinJS.Utilities.eventMixin.dispatchEvent">
/// <summary locid="WinJS.Utilities.eventMixin.dispatchEvent">
/// Raises an event of the specified type and with the specified additional properties.
/// </summary>
/// <param name="type" locid="WinJS.Utilities.eventMixin.dispatchEvent_p:type">
/// The type (name) of the event.
/// </param>
/// <param name="details" locid="WinJS.Utilities.eventMixin.dispatchEvent_p:details">
/// The set of additional properties to be attached to the event object when the event is raised.
/// </param>
/// <returns type="Boolean" locid="WinJS.Utilities.eventMixin.dispatchEvent_returnValue">
/// true if preventDefault was called on the event.
/// </returns>
/// </signature>
var listeners = this._listeners && this._listeners[type];
if (listeners) {
var eventValue = new EventMixinEvent(type, details, this);
// Need to copy the array to protect against people unregistering while we are dispatching
listeners = listeners.slice(0, listeners.length);
for (var i = 0, len = listeners.length; i < len && !eventValue._stopImmediatePropagationCalled; i++) {
listeners[i].listener(eventValue);
}
return eventValue.defaultPrevented || false;
}
return false;
},
removeEventListener: function (type, listener, useCapture) {
/// <signature helpKeyword="WinJS.Utilities.eventMixin.removeEventListener">
/// <summary locid="WinJS.Utilities.eventMixin.removeEventListener">
/// Removes an event listener from the control.
/// </summary>
/// <param name="type" locid="WinJS.Utilities.eventMixin.removeEventListener_p:type">
/// The type (name) of the event.
/// </param>
/// <param name="listener" locid="WinJS.Utilities.eventMixin.removeEventListener_p:listener">
/// The listener to remove.
/// </param>
/// <param name="useCapture" locid="WinJS.Utilities.eventMixin.removeEventListener_p:useCapture">
/// Specifies whether to initiate capture.
/// </param>
/// </signature>
useCapture = useCapture || false;
var listeners = this._listeners && this._listeners[type];
if (listeners) {
for (var i = 0, len = listeners.length; i < len; i++) {
var l = listeners[i];
if (l.listener === listener && l.useCapture === useCapture) {
listeners.splice(i, 1);
if (listeners.length === 0) {
delete this._listeners[type];
}
// Only want to remove one element for each call to removeEventListener
break;
}
}
}
}
};
_Base.Namespace._moduleDefine(exports, "WinJS.Utilities", {
_createEventProperty: createEventProperty,
createEventProperties: createEventProperties,
eventMixin: eventMixin
});
});
_winjs("WinJS/Core/_Trace", ["WinJS/Core/_Global"], function traceInit(_Global) {
"use strict";
function nop(v) {
return v;
}
return {
_traceAsyncOperationStarting: (_Global.Debug && _Global.Debug.msTraceAsyncOperationStarting && _Global.Debug.msTraceAsyncOperationStarting.bind(_Global.Debug)) || nop,
_traceAsyncOperationCompleted: (_Global.Debug && _Global.Debug.msTraceAsyncOperationCompleted && _Global.Debug.msTraceAsyncOperationCompleted.bind(_Global.Debug)) || nop,
_traceAsyncCallbackStarting: (_Global.Debug && _Global.Debug.msTraceAsyncCallbackStarting && _Global.Debug.msTraceAsyncCallbackStarting.bind(_Global.Debug)) || nop,
_traceAsyncCallbackCompleted: (_Global.Debug && _Global.Debug.msTraceAsyncCallbackCompleted && _Global.Debug.msTraceAsyncCallbackCompleted.bind(_Global.Debug)) || nop
};
});
_winjs("WinJS/Promise/_StateMachine", ["WinJS/Core/_Global","WinJS/Core/_BaseCoreUtils","WinJS/Core/_Base","WinJS/Core/_ErrorFromName","WinJS/Core/_Events","WinJS/Core/_Trace"], function promiseStateMachineInit(_Global, _BaseCoreUtils, _Base, _ErrorFromName, _Events, _Trace) {
"use strict";
_Global.Debug && (_Global.Debug.setNonUserCodeExceptions = true);
var ListenerType = _Base.Class.mix(_Base.Class.define(null, { /*empty*/ }, { supportedForProcessing: false }), _Events.eventMixin);
var promiseEventListeners = new ListenerType();
// make sure there is a listeners collection so that we can do a more trivial check below
promiseEventListeners._listeners = {};
var errorET = "error";
var canceledName = "Canceled";
var tagWithStack = false;
var tag = {
promise: 0x01,
thenPromise: 0x02,
errorPromise: 0x04,
exceptionPromise: 0x08,
completePromise: 0x10,
};
tag.all = tag.promise | tag.thenPromise | tag.errorPromise | tag.exceptionPromise | tag.completePromise;
//
// Global error counter, for each error which enters the system we increment this once and then
// the error number travels with the error as it traverses the tree of potential handlers.
//
// When someone has registered to be told about errors (WinJS.Promise.callonerror) promises
// which are in error will get tagged with a ._errorId field. This tagged field is the
// contract by which nested promises with errors will be identified as chaining for the
// purposes of the callonerror semantics. If a nested promise in error is encountered without
// a ._errorId it will be assumed to be foreign and treated as an interop boundary and
// a new error id will be minted.
//
var error_number = 1;
//
// The state machine has a interesting hiccup in it with regards to notification, in order
// to flatten out notification and avoid recursion for synchronous completion we have an
// explicit set of *_notify states which are responsible for notifying their entire tree
// of children. They can do this because they know that immediate children are always
// ThenPromise instances and we can therefore reach into their state to access the
// _listeners collection.
//
// So, what happens is that a Promise will be fulfilled through the _completed or _error
// messages at which point it will enter a *_notify state and be responsible for to move
// its children into an (as appropriate) success or error state and also notify that child's
// listeners of the state transition, until leaf notes are reached.
//
var state_created, // -> working
state_working, // -> error | error_notify | success | success_notify | canceled | waiting
state_waiting, // -> error | error_notify | success | success_notify | waiting_canceled
state_waiting_canceled, // -> error | error_notify | success | success_notify | canceling
state_canceled, // -> error | error_notify | success | success_notify | canceling
state_canceling, // -> error_notify
state_success_notify, // -> success
state_success, // -> .
state_error_notify, // -> error
state_error; // -> .
// Noop function, used in the various states to indicate that they don't support a given
// message. Named with the somewhat cute name '_' because it reads really well in the states.
function _() { }
// Initial state
//
state_created = {
name: "created",
enter: function (promise) {
promise._setState(state_working);
},
cancel: _,
done: _,
then: _,
_completed: _,
_error: _,
_notify: _,
_progress: _,
_setCompleteValue: _,
_setErrorValue: _
};
// Ready state, waiting for a message (completed/error/progress), able to be canceled
//
state_working = {
name: "working",
enter: _,
cancel: function (promise) {
promise._setState(state_canceled);
},
done: done,
then: then,
_completed: completed,
_error: error,
_notify: _,
_progress: progress,
_setCompleteValue: setCompleteValue,
_setErrorValue: setErrorValue
};
// Waiting state, if a promise is completed with a value which is itself a promise
// (has a then() method) it signs up to be informed when that child promise is
// fulfilled at which point it will be fulfilled with that value.
//
state_waiting = {
name: "waiting",
enter: function (promise) {
var waitedUpon = promise._value;
// We can special case our own intermediate promises which are not in a
// terminal state by just pushing this promise as a listener without
// having to create new indirection functions
if (waitedUpon instanceof ThenPromise &&
waitedUpon._state !== state_error &&
waitedUpon._state !== state_success) {
pushListener(waitedUpon, { promise: promise });
} else {
var error = function (value) {
if (waitedUpon._errorId) {
promise._chainedError(value, waitedUpon);
} else {
// Because this is an interop boundary we want to indicate that this
// error has been handled by the promise infrastructure before we
// begin a new handling chain.
//
callonerror(promise, value, detailsForHandledError, waitedUpon, error);
promise._error(value);
}
};
error.handlesOnError = true;
waitedUpon.then(
promise._completed.bind(promise),
error,
promise._progress.bind(promise)
);
}
},
cancel: function (promise) {
promise._setState(state_waiting_canceled);
},
done: done,
then: then,
_completed: completed,
_error: error,
_notify: _,
_progress: progress,
_setCompleteValue: setCompleteValue,
_setErrorValue: setErrorValue
};
// Waiting canceled state, when a promise has been in a waiting state and receives a
// request to cancel its pending work it will forward that request to the child promise
// and then waits to be informed of the result. This promise moves itself into the
// canceling state but understands that the child promise may instead push it to a
// different state.
//
state_waiting_canceled = {
name: "waiting_canceled",
enter: function (promise) {
// Initiate a transition to canceling. Triggering a cancel on the promise
// that we are waiting upon may result in a different state transition
// before the state machine pump runs again.
promise._setState(state_canceling);
var waitedUpon = promise._value;
if (waitedUpon.cancel) {
waitedUpon.cancel();
}
},
cancel: _,
done: done,
then: then,
_completed: completed,
_error: error,
_notify: _,
_progress: progress,
_setCompleteValue: setCompleteValue,
_setErrorValue: setErrorValue
};
// Canceled state, moves to the canceling state and then tells the promise to do
// whatever it might need to do on cancelation.
//
state_canceled = {
name: "canceled",
enter: function (promise) {
// Initiate a transition to canceling. The _cancelAction may change the state
// before the state machine pump runs again.
promise._setState(state_canceling);
promise._cancelAction();
},
cancel: _,
done: done,
then: then,
_completed: completed,
_error: error,
_notify: _,
_progress: progress,
_setCompleteValue: setCompleteValue,
_setErrorValue: setErrorValue
};
// Canceling state, commits to the promise moving to an error state with an error
// object whose 'name' and 'message' properties contain the string "Canceled"
//
state_canceling = {
name: "canceling",
enter: function (promise) {
var error = new Error(canceledName);
error.name = error.message;
promise._value = error;
promise._setState(state_error_notify);
},
cancel: _,
done: _,
then: _,
_completed: _,
_error: _,
_notify: _,
_progress: _,
_setCompleteValue: _,
_setErrorValue: _
};
// Success notify state, moves a promise to the success state and notifies all children
//
state_success_notify = {
name: "complete_notify",
enter: function (promise) {
promise.done = CompletePromise.prototype.done;
promise.then = CompletePromise.prototype.then;
if (promise._listeners) {
var queue = [promise];
var p;
while (queue.length) {
p = queue.shift();
p._state._notify(p, queue);
}
}
promise._setState(state_success);
},
cancel: _,
done: null, /*error to get here */
then: null, /*error to get here */
_completed: _,
_error: _,
_notify: notifySuccess,
_progress: _,
_setCompleteValue: _,
_setErrorValue: _
};
// Success state, moves a promise to the success state and does NOT notify any children.
// Some upstream promise is owning the notification pass.
//
state_success = {
name: "success",
enter: function (promise) {
promise.done = CompletePromise.prototype.done;
promise.then = CompletePromise.prototype.then;
promise._cleanupAction();
},
cancel: _,
done: null, /*error to get here */
then: null, /*error to get here */
_completed: _,
_error: _,
_notify: notifySuccess,
_progress: _,
_setCompleteValue: _,
_setErrorValue: _
};
// Error notify state, moves a promise to the error state and notifies all children
//
state_error_notify = {
name: "error_notify",
enter: function (promise) {
promise.done = ErrorPromise.prototype.done;
promise.then = ErrorPromise.prototype.then;
if (promise._listeners) {
var queue = [promise];
var p;
while (queue.length) {
p = queue.shift();
p._state._notify(p, queue);
}
}
promise._setState(state_error);
},
cancel: _,
done: null, /*error to get here*/
then: null, /*error to get here*/
_completed: _,
_error: _,
_notify: notifyError,
_progress: _,
_setCompleteValue: _,
_setErrorValue: _
};
// Error state, moves a promise to the error state and does NOT notify any children.
// Some upstream promise is owning the notification pass.
//
state_error = {
name: "error",
enter: function (promise) {
promise.done = ErrorPromise.prototype.done;
promise.then = ErrorPromise.prototype.then;
promise._cleanupAction();
},
cancel: _,
done: null, /*error to get here*/
then: null, /*error to get here*/
_completed: _,
_error: _,
_notify: notifyError,
_progress: _,
_setCompleteValue: _,
_setErrorValue: _
};
//
// The statemachine implementation follows a very particular pattern, the states are specified
// as static stateless bags of functions which are then indirected through the state machine
// instance (a Promise). As such all of the functions on each state have the promise instance
// passed to them explicitly as a parameter and the Promise instance members do a little
// dance where they indirect through the state and insert themselves in the argument list.
//
// We could instead call directly through the promise states however then every caller
// would have to remember to do things like pumping the state machine to catch state transitions.
//
var PromiseStateMachine = _Base.Class.define(null, {
_listeners: null,
_nextState: null,
_state: null,
_value: null,
cancel: function () {
/// <signature helpKeyword="WinJS.PromiseStateMachine.cancel">
/// <summary locid="WinJS.PromiseStateMachine.cancel">
/// Attempts to cancel the fulfillment of a promised value. If the promise hasn't
/// already been fulfilled and cancellation is supported, the promise enters
/// the error state with a value of Error("Canceled").
/// </summary>
/// </signature>
this._state.cancel(this);
this._run();
},
done: function Promise_done(onComplete, onError, onProgress) {
/// <signature helpKeyword="WinJS.PromiseStateMachine.done">
/// <summary locid="WinJS.PromiseStateMachine.done">
/// Allows you to specify the work to be done on the fulfillment of the promised value,
/// the error handling to be performed if the promise fails to fulfill
/// a value, and the handling of progress notifications along the way.
///
/// After the handlers have finished executing, this function throws any error that would have been returned
/// from then() as a promise in the error state.
/// </summary>
/// <param name='onComplete' type='Function' locid="WinJS.PromiseStateMachine.done_p:onComplete">
/// The function to be called if the promise is fulfilled successfully with a value.
/// The fulfilled value is passed as the single argument. If the value is null,
/// the fulfilled value is returned. The value returned
/// from the function becomes the fulfilled value of the promise returned by
/// then(). If an exception is thrown while executing the function, the promise returned
/// by then() moves into the error state.
/// </param>
/// <param name='onError' type='Function' optional='true' locid="WinJS.PromiseStateMachine.done_p:onError">
/// The function to be called if the promise is fulfilled with an error. The error
/// is passed as the single argument. If it is null, the error is forwarded.
/// The value returned from the function is the fulfilled value of the promise returned by then().
/// </param>
/// <param name='onProgress' type='Function' optional='true' locid="WinJS.PromiseStateMachine.done_p:onProgress">
/// the function to be called if the promise reports progress. Data about the progress
/// is passed as the single argument. Promises are not required to support
/// progress.
/// </param>
/// </signature>
this._state.done(this, onComplete, onError, onProgress);
},
then: function Promise_then(onComplete, onError, onProgress) {
/// <signature helpKeyword="WinJS.PromiseStateMachine.then">
/// <summary locid="WinJS.PromiseStateMachine.then">
/// Allows you to specify the work to be done on the fulfillment of the promised value,
/// the error handling to be performed if the promise fails to fulfill
/// a value, and the handling of progress notifications along the way.
/// </summary>
/// <param name='onComplete' type='Function' locid="WinJS.PromiseStateMachine.then_p:onComplete">
/// The function to be called if the promise is fulfilled successfully with a value.
/// The value is passed as the single argument. If the value is null, the value is returned.
/// The value returned from the function becomes the fulfilled value of the promise returned by
/// then(). If an exception is thrown while this function is being executed, the promise returned
/// by then() moves into the error state.
/// </param>
/// <param name='onError' type='Function' optional='true' locid="WinJS.PromiseStateMachine.then_p:onError">
/// The function to be called if the promise is fulfilled with an error. The error
/// is passed as the single argument. If it is null, the error is forwarded.
/// The value returned from the function becomes the fulfilled value of the promise returned by then().
/// </param>
/// <param name='onProgress' type='Function' optional='true' locid="WinJS.PromiseStateMachine.then_p:onProgress">
/// The function to be called if the promise reports progress. Data about the progress
/// is passed as the single argument. Promises are not required to support
/// progress.
/// </param>
/// <returns type="WinJS.Promise" locid="WinJS.PromiseStateMachine.then_returnValue">
/// The promise whose value is the result of executing the complete or
/// error function.
/// </returns>
/// </signature>
// BEGIN monaco change
if (this.then !== Promise_then) {
this.then(onComplete, onError, onProgress);
return;
}
// END monaco change
return this._state.then(this, onComplete, onError, onProgress);
},
_chainedError: function (value, context) {
var result = this._state._error(this, value, detailsForChainedError, context);
this._run();
return result;
},
_completed: function (value) {
var result = this._state._completed(this, value);
this._run();
return result;
},
_error: function (value) {
var result = this._state._error(this, value, detailsForError);
this._run();
return result;
},
_progress: function (value) {
this._state._progress(this, value);
},
_setState: function (state) {
this._nextState = state;
},
_setCompleteValue: function (value) {
this._state._setCompleteValue(this, value);
this._run();
},
_setChainedErrorValue: function (value, context) {
var result = this._state._setErrorValue(this, value, detailsForChainedError, context);
this._run();
return result;
},
_setExceptionValue: function (value) {
var result = this._state._setErrorValue(this, value, detailsForException);
this._run();
return result;
},
_run: function () {
while (this._nextState) {
this._state = this._nextState;
this._nextState = null;
this._state.enter(this);
}
}
}, {
supportedForProcessing: false
});
//
// Implementations of shared state machine code.
//
function completed(promise, value) {
var targetState;
if (value && typeof value === "object" && typeof value.then === "function") {
targetState = state_waiting;
} else {
targetState = state_success_notify;
}
promise._value = value;
promise._setState(targetState);
}
function createErrorDetails(exception, error, promise, id, parent, handler) {
return {
exception: exception,
error: error,
promise: promise,
handler: handler,
id: id,
parent: parent
};
}
function detailsForHandledError(promise, errorValue, context, handler) {
var exception = context._isException;
var errorId = context._errorId;
return createErrorDetails(
exception ? errorValue : null,
exception ? null : errorValue,
promise,
errorId,
context,
handler
);
}
function detailsForChainedError(promise, errorValue, context) {
var exception = context._isException;
var errorId = context._errorId;
setErrorInfo(promise, errorId, exception);
return createErrorDetails(
exception ? errorValue : null,
exception ? null : errorValue,
promise,
errorId,
context
);
}
function detailsForError(promise, errorValue) {
var errorId = ++error_number;
setErrorInfo(promise, errorId);
return createErrorDetails(
null,
errorValue,
promise,
errorId
);
}
function detailsForException(promise, exceptionValue) {
var errorId = ++error_number;
setErrorInfo(promise, errorId, true);
return createErrorDetails(
exceptionValue,
null,
promise,
errorId
);
}
function done(promise, onComplete, onError, onProgress) {
var asyncOpID = _Trace._traceAsyncOperationStarting("WinJS.Promise.done");
pushListener(promise, { c: onComplete, e: onError, p: onProgress, asyncOpID: asyncOpID });
}
function error(promise, value, onerrorDetails, context) {
promise._value = value;
callonerror(promise, value, onerrorDetails, context);
promise._setState(state_error_notify);
}
function notifySuccess(promise, queue) {
var value = promise._value;
var listeners = promise._listeners;
if (!listeners) {
return;
}
promise._listeners = null;
var i, len;
for (i = 0, len = Array.isArray(listeners) ? listeners.length : 1; i < len; i++) {
var listener = len === 1 ? listeners : listeners[i];
var onComplete = listener.c;
var target = listener.promise;
_Trace._traceAsyncOperationCompleted(listener.asyncOpID, _Global.Debug && _Global.Debug.MS_ASYNC_OP_STATUS_SUCCESS);
if (target) {
_Trace._traceAsyncCallbackStarting(listener.asyncOpID);
try {
target._setCompleteValue(onComplete ? onComplete(value) : val