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 pathoperationsClient.js
168 lines (152 loc) · 6.8 KB
/
operationsClient.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*******************************************************************************
* @license
* Copyright (c) 2011, 2014 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*/
define(['i18n!orion/operations/nls/messages', "orion/Deferred"], function(messages, Deferred) {
var NAMESPACE = "/operations";
function _doServiceCall(operationsService, funcName, funcArgs) {
return operationsService[funcName].apply(operationsService, funcArgs);
}
function _getOperations(operationsService, options) {
return _doServiceCall(operationsService, "getOperations", [options]); //$NON-NLS-0$
}
function NoMatchingOperationsClient(location) {
this._location = location;
}
function returnNoMatchingError() {
var result = new Deferred();
result.reject(messages["NoMatchingOpSrvLocation"] + this._location);
return result;
}
NoMatchingOperationsClient.prototype = {
getOperation: returnNoMatchingError,
getOperations: returnNoMatchingError,
removeCompletedOperations: returnNoMatchingError,
removeOperation: returnNoMatchingError,
cancelOperation: returnNoMatchingError
};
NoMatchingOperationsClient.prototype.constructor = NoMatchingOperationsClient;
function OperationsClient(serviceRegistry) {
this._services = [];
this._patterns = [];
this._operationListeners = [];
this._currentLongpollingIds = [];
this._preferenceService = serviceRegistry.getService("orion.core.preference"); //$NON-NLS-0$
var operationsServices = serviceRegistry.getServiceReferences("orion.core.operation"); //$NON-NLS-0$
for (var i = 0; i < operationsServices.length; i++) {
var servicePtr = operationsServices[i];
var operationsService = serviceRegistry.getService(servicePtr);
this._services[i] = operationsService;
var patternString = operationsServices[i].getProperty("pattern") || ".*"; //$NON-NLS-1$ //$NON-NLS-0$
if (patternString[0] !== "^") { //$NON-NLS-0$
patternString = "^" + patternString; //$NON-NLS-0$
}
this._patterns[i] = new RegExp(patternString);
}
this._getService = function(location) {
if (!location) {
return this._services[0];
}
for (var j = 0; j < this._patterns.length; ++j) {
if (this._patterns[j].test(location)) {
return this._services[j];
}
}
this._preferenceService.remove(NAMESPACE, location);
return new NoMatchingOperationsClient(location);
};
}
function _mergeOperations(lists) {
var result = {
Children: []
};
for (var i = 0; i < lists.length; i++) {
result.Children = result.Children.concat(lists[i].Children);
}
return result;
}
function _notifyChangeListeners(result) {
for (var i = 0; i < this._operationListeners.length; i++) {
this._operationListeners[i](result);
}
}
OperationsClient.prototype = {
addOperation: function(operationLocation, data) {
var that = this;
return this._preferenceService.get(NAMESPACE).then(function(globalOperations) {
var operationLocations = Object.keys(globalOperations);
var now = Date.now();
for (var j = 0; j < operationLocations.length; j++) {
var location = operationLocations[j];
var expires = globalOperations[location].expires;
if (expires < now) {
delete globalOperations[location];
}
}
globalOperations[operationLocation] = data;
return that._preferenceService.put(NAMESPACE, globalOperations, {clear: true});
});
},
getOperations: function() {
return this._preferenceService.get(NAMESPACE);
},
getOperation: function(operationLocation) {
return _doServiceCall(this._getService(operationLocation), "getOperation", arguments); //$NON-NLS-0$
},
removeCompletedOperations: function() {
var results = [];
var that = this;
for (var i = 0; i < this._services.length; i++) {
var pattern = this._patterns[i];
var def = new Deferred();
results[i] = def.promise;
_doServiceCall(this._services[i], "removeCompletedOperations").then(function(def) {
return function(operationsLeft) {
if (!Array.isArray(operationsLeft)) {
return;
}
that._preferenceService.get(NAMESPACE).then(function(globalOperations) {
var operationLocations = Object.keys(globalOperations);
for (var j = 0; j < operationLocations.length; j++) {
var location = operationLocations[j];
if (pattern.test(location)) {
if (operationsLeft.indexOf(location) < 0) {
delete globalOperations[location];
}
}
}
that._preferenceService.put(NAMESPACE, globalOperations, {clear: true}).then(def.resolve, def.reject);
});
};
}(def), function(def) {
return function(error) {
def.reject(error);
};
}(def));
}
return Deferred.all(results);
},
removeOperation: function(operationLocation) {
var that = this;
return _doServiceCall(this._getService(operationLocation), "removeOperation", arguments).then(function(result) {
return that._preferenceService.remove(NAMESPACE, operationLocation);
}, function(error) {
return error;
}, function(progress) {
return progress;
});
}
};
OperationsClient.prototype.constructor = OperationsClient;
return {
OperationsClient: OperationsClient
};
});