Bug 1436557 - Move calOperationGroup to calDataUtils.jsm. r=MakeMyDay

MozReview-Commit-ID: BkVndYLfrOw

--HG--
extra : rebase_source : 1cf414fce5e13a6ce14c9a5cf73767662ae1c24e
This commit is contained in:
Philipp Kewisch 2018-02-07 23:23:34 +01:00
Родитель df27dd0de4
Коммит a2f97d7763
6 изменённых файлов: 85 добавлений и 100 удалений

Просмотреть файл

@ -91,10 +91,87 @@ class ObserverSet extends ListenerSet {
} }
} }
/**
* This object implements calIOperation and could group multiple sub
* operations into one. You can pass a cancel function which is called once
* the operation group is cancelled.
* Users must call notifyCompleted() once all sub operations have been
* successful, else the operation group will stay pending.
* The reason for the latter is that providers currently should (but need
* not) implement (and return) calIOperation handles, thus there may be pending
* calendar operations (without handle).
*/
class OperationGroup {
static nextGroupId() {
if (typeof OperationGroup.mOpGroupId == "undefined") {
OperationGroup.mOpGroupId = 0;
}
return OperationGroup.mOpGroupId++;
}
constructor(aCancelFunc) {
this.mId = cal.getUUID() + "-" + OperationGroup.nextGroupId();
this.mIsPending = true;
this.mCancelFunc = aCancelFunc;
this.mSubOperations = [];
this.mStatus = Components.results.NS_OK;
}
get id() { return this.mId; }
get isPending() { return this.mIsPending; }
get status() { return this.mStatus; }
get isEmpty() { return this.mSubOperations.length == 0; }
add(aOperation) {
if (aOperation && aOperation.isPending) {
this.mSubOperations.push(aOperation);
}
}
remove(aOperation) {
if (aOperation) {
this.mSubOperations = this.mSubOperations.filter(operation => aOperation.id != operation.id);
}
}
notifyCompleted(aStatus) {
cal.ASSERT(this.isPending, "[OperationGroup_notifyCompleted] this.isPending");
if (this.isPending) {
this.mIsPending = false;
if (aStatus) {
this.mStatus = aStatus;
}
}
}
cancel(aStatus=Components.interfaces.calIErrors.OPERATION_CANCELLED) {
if (this.isPending) {
this.notifyCompleted(aStatus);
let cancelFunc = this.mCancelFunc;
if (cancelFunc) {
this.mCancelFunc = null;
cancelFunc();
}
let subOperations = this.mSubOperations;
this.mSubOperations = [];
for (let operation of subOperations) {
operation.cancel(Components.interfaces.calIErrors.OPERATION_CANCELLED);
}
}
}
toString() {
return `[OperationGroup id=${this.id}]`;
}
}
var caldata = { var caldata = {
ListenerSet: ListenerSet, ListenerSet: ListenerSet,
ObserverSet: ObserverSet, ObserverSet: ObserverSet,
PropertyMap: PropertyMap, PropertyMap: PropertyMap,
OperationGroup: OperationGroup,
/** /**
* Use the binary search algorithm to search for an item in an array. * Use the binary search algorithm to search for an item in an array.

Просмотреть файл

@ -9,7 +9,7 @@ function calCalendarSearchListener(numOperations, finalListener) {
this.mNumOperations = numOperations; this.mNumOperations = numOperations;
this.mResults = []; this.mResults = [];
this.opGroup = new cal.calOperationGroup(() => { this.opGroup = new cal.data.OperationGroup(() => {
this.notifyResult(null); this.notifyResult(null);
}); });
} }

Просмотреть файл

@ -9,7 +9,7 @@ function calFreeBusyListener(numOperations, finalListener) {
this.mFinalListener = finalListener; this.mFinalListener = finalListener;
this.mNumOperations = numOperations; this.mNumOperations = numOperations;
this.opGroup = new cal.calOperationGroup(() => { this.opGroup = new cal.data.OperationGroup(() => {
this.notifyResult(null); this.notifyResult(null);
}); });
} }

Просмотреть файл

@ -10,7 +10,7 @@
/* exported getCalendarDirectory, attendeeMatchesAddresses, /* exported getCalendarDirectory, attendeeMatchesAddresses,
* calTryWrappedJSObject, compareObjects, LOG, WARN, ERROR, showError, * calTryWrappedJSObject, compareObjects, LOG, WARN, ERROR, showError,
* sendMailTo, applyAttributeToMenuChildren, isPropertyValueSame, * sendMailTo, applyAttributeToMenuChildren, isPropertyValueSame,
* calIterateEmailIdentities, calGetString * calIterateEmailIdentities, calGetString, getUUID
*/ */
ChromeUtils.import("resource:///modules/mailServices.js"); ChromeUtils.import("resource:///modules/mailServices.js");
@ -258,98 +258,6 @@ function sendMailTo(aRecipient, aSubject, aBody, aIdentity) {
MailServices.compose.OpenComposeWindowWithParams(null, msgParams); MailServices.compose.OpenComposeWindowWithParams(null, msgParams);
} }
/**
* This object implements calIOperation and could group multiple sub
* operations into one. You can pass a cancel function which is called once
* the operation group is cancelled.
* Users must call notifyCompleted() once all sub operations have been
* successful, else the operation group will stay pending.
* The reason for the latter is that providers currently should (but need
* not) implement (and return) calIOperation handles, thus there may be pending
* calendar operations (without handle).
*/
function calOperationGroup(cancelFunc) {
this.wrappedJSObject = this;
if (calOperationGroup.mOpGroupId === undefined) {
calOperationGroup.mOpGroupId = 0;
}
if (calOperationGroup.mOpGroupPrefix === undefined) {
calOperationGroup.mOpGroupPrefix = getUUID() + "-";
}
this.mCancelFunc = cancelFunc;
this.mId = calOperationGroup.mOpGroupPrefix + calOperationGroup.mOpGroupId++;
this.mSubOperations = [];
}
calOperationGroup.prototype = {
mCancelFunc: null,
mId: null,
mIsPending: true,
mStatus: Components.results.NS_OK,
mSubOperations: null,
add: function(aOperation) {
if (aOperation && aOperation.isPending) {
this.mSubOperations.push(aOperation);
}
},
remove: function(aOperation) {
if (aOperation) {
this.mSubOperations = this.mSubOperations.filter(operation => aOperation.id != operation.id);
}
},
get isEmpty() {
return (this.mSubOperations.length == 0);
},
notifyCompleted: function(status) {
ASSERT(this.isPending, "[calOperationGroup_notifyCompleted] this.isPending");
if (this.isPending) {
this.mIsPending = false;
if (status) {
this.mStatus = status;
}
}
},
toString: function() {
return "[calOperationGroup] id=" + this.id;
},
// calIOperation:
get id() {
return this.mId;
},
get isPending() {
return this.mIsPending;
},
get status() {
return this.mStatus;
},
cancel: function(status) {
if (this.isPending) {
if (!status) {
status = Components.interfaces.calIErrors.OPERATION_CANCELLED;
}
this.notifyCompleted(status);
let cancelFunc = this.mCancelFunc;
if (cancelFunc) {
this.mCancelFunc = null;
cancelFunc();
}
let subOperations = this.mSubOperations;
this.mSubOperations = [];
for (let operation of subOperations) {
operation.cancel(Components.interfaces.calIErrors.OPERATION_CANCELLED);
}
}
}
};
/** /**
* TODO: The following UI-related functions need to move somewhere different, * TODO: The following UI-related functions need to move somewhere different,
* i.e calendar-ui-utils.js * i.e calendar-ui-utils.js

Просмотреть файл

@ -451,7 +451,7 @@ calCompositeGetListenerHelper.prototype = {
get opGroup() { get opGroup() {
if (!this.mOpGroup) { if (!this.mOpGroup) {
this.mOpGroup = new cal.calOperationGroup(() => { this.mOpGroup = new cal.data.OperationGroup(() => {
let listener = this.mRealListener; let listener = this.mRealListener;
this.mRealListener = null; this.mRealListener = null;
if (listener) { if (listener) {

Просмотреть файл

@ -12,7 +12,7 @@ function really_run_test() {
test_attendeeMatchesAddresses(); test_attendeeMatchesAddresses();
test_getDefaultStartDate(); test_getDefaultStartDate();
test_getStartEndProps(); test_getStartEndProps();
test_calOperationGroup(); test_OperationGroup();
test_sameDay(); test_sameDay();
test_binarySearch(); test_binarySearch();
} }
@ -122,17 +122,17 @@ function test_getStartEndProps() {
/2147500033/); /2147500033/);
} }
function test_calOperationGroup() { function test_OperationGroup() {
let cancelCalled = false; let cancelCalled = false;
function cancelFunc() { function cancelFunc() {
cancelCalled = true; cancelCalled = true;
return true; return true;
} }
let group = new cal.calOperationGroup(cancelFunc); let group = new cal.data.OperationGroup(cancelFunc);
ok(group.isEmpty); ok(group.isEmpty);
equal(group.id, cal.calOperationGroup.mOpGroupPrefix + "0"); ok(group.id.endsWith("-0"));
equal(group.status, Components.results.NS_OK); equal(group.status, Components.results.NS_OK);
equal(group.isPending, true); equal(group.isPending, true);