This repository has been archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathoperation.js
127 lines (120 loc) · 3.81 KB
/
operation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*******************************************************************************
* @license
* Copyright (c) 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
* License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html).
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
/*eslint-env browser, amd*/
/**
* @name orion.operation
* @namespace Provides an API for handling long running operations as promises.
*/
define(["orion/xhr", "orion/Deferred"], function(xhr, Deferred) {
function _isRunning(operationType) {
if (!operationType) {
return true;
}
if (operationType === "loadstart" || operationType === "progress") {
return true;
}
return false;
}
function _deleteTempOperation(operationLocation) {
xhr("DELETE", operationLocation, {
headers: {
"Orion-Version": "1"
},
timeout: 15000
});
}
function _cancelOperation(operationLocation) {
xhr("PUT", operationLocation, {
headers: {
"Orion-Version": "1"
},
data: JSON.stringify({
abort: true
}),
timeout: 15000
});
}
function _getOperation(operation, deferred, onResolve, onReject) {
xhr("GET", operation.location, {
headers: {
"Orion-Version": "1"
},
timeout: 15000
}).then(function(result) {
var operationJson = result.response ? JSON.parse(result.response) : null;
deferred.progress(operationJson);
if (_isRunning(operationJson.type)) {
setTimeout(function() {
_getOperation(operation , deferred, onResolve, onReject);
}, operation.timeout);
operation.timeout = Math.min(operation.timeout * 2, 2000);
return;
}
if (operationJson.type === "error" || operationJson.type === "abort") {
deferred.reject(onReject ? onReject(operationJson) : operationJson.Result);
} else {
deferred.resolve(onResolve ? onResolve(operationJson) : operationJson.Result.JsonData);
}
if (!operationJson.Location) {
_deleteTempOperation(operation.location); //This operation should not be kept
}
}, function(error) {
var errorMessage = error;
if (error.responseText !== undefined) {
errorMessage = error.responseText;
try {
errorMessage = JSON.parse(error.responseText);
} catch (e) {
//ignore
}
}
if (errorMessage.Message !== undefined) {
errorMessage.HttpCode = errorMessage.HttpCode === undefined ? error.status : errorMessage.HttpCode;
errorMessage.Severity = errorMessage.Severity === undefined ? "Error" : errorMessage.Severity;
deferred.reject(errorMessage);
} else {
deferred.reject({
Severity: "Error",
Message: errorMessage,
HttpCode: error.status
});
}
});
}
function _trackCancel(operationLocation, deferred) {
deferred.then(null, function(error) {
if (error instanceof Error && error.name === "Cancel") {
_cancelOperation(operationLocation);
}
});
}
/**
* Handles a long-running operation as a promise.
* @name orion.operation.handle
* @function
* @param {String} operationLocation
* @param {Function} [onSuccess] If provided, will be called to transform a successful operation into the resolve value of the
* returned promise.
* @param {Function} [onError] If provided, will be called to trasnform a failed operation into the reject value of the
* returned promise.
* @returns {orion.Promise}
*/
function handle(operationLocation, onSuccess, onError) {
var def = new Deferred();
_trackCancel(operationLocation, def);
_getOperation({location: operationLocation, timeout: 100}, def, onSuccess, onError);
return def;
}
return {
handle: handle
};
});