From 00daa32f56ca56b37ec3347bed02d589f2887226 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 11 Mar 2015 17:37:07 -0700 Subject: [PATCH 1/2] [Core] Accelerate promises during mutation Use a fast-promise approach (instead of ) when handling mutation of domain objects, to ensure that mutation resolves during the current tick. Needed for drag interactions of WTD-931. --- .../core/src/capabilities/MutationCapability.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/platform/core/src/capabilities/MutationCapability.js b/platform/core/src/capabilities/MutationCapability.js index d0d6c2b360..14ff3a5265 100644 --- a/platform/core/src/capabilities/MutationCapability.js +++ b/platform/core/src/capabilities/MutationCapability.js @@ -21,6 +21,16 @@ define( }); } + // Utility function to cast to a promise, without waiting + // for nextTick if a value is non-promise-like. + function fastPromise(value) { + return (value || {}).then ? value : { + then: function (callback) { + return fastPromise(callback(value)); + } + }; + } + /** * The `mutation` capability allows a domain object's model to be * modified. Wrapping such modifications in calls made through @@ -73,8 +83,7 @@ define( // Invoke the provided mutator, then make changes to // the underlying model (if applicable.) - return $q.when(mutator(clone)) - .then(handleMutation); + return fastPromise(mutator(clone)).then(handleMutation); } return { From 222aa55dd7d3546c75c2dcb991909f8f30863664 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 11 Mar 2015 17:38:40 -0700 Subject: [PATCH 2/2] [Core] Remove dependency Remove dependency from the mutation capability; it recognizes and handles promises internally, instead. WTD-931. --- platform/core/bundle.json | 3 +-- .../core/src/capabilities/MutationCapability.js | 3 +-- .../test/capabilities/MutationCapabilitySpec.js | 14 +------------- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/platform/core/bundle.json b/platform/core/bundle.json index c986bc8a74..f22ed7f617 100644 --- a/platform/core/bundle.json +++ b/platform/core/bundle.json @@ -151,8 +151,7 @@ }, { "key": "mutation", - "implementation": "capabilities/MutationCapability.js", - "depends": [ "$q" ] + "implementation": "capabilities/MutationCapability.js" }, { "key": "delegation", diff --git a/platform/core/src/capabilities/MutationCapability.js b/platform/core/src/capabilities/MutationCapability.js index 14ff3a5265..9a36d60180 100644 --- a/platform/core/src/capabilities/MutationCapability.js +++ b/platform/core/src/capabilities/MutationCapability.js @@ -46,12 +46,11 @@ define( * }); * ``` * - * @param $q Angular's $q service, for promises * @param {DomainObject} domainObject the domain object * which will expose this capability * @constructor */ - function MutationCapability($q, domainObject) { + function MutationCapability(domainObject) { function mutate(mutator) { // Get the object's model and clone it, so the diff --git a/platform/core/test/capabilities/MutationCapabilitySpec.js b/platform/core/test/capabilities/MutationCapabilitySpec.js index 3c03ba5dce..83536347f3 100644 --- a/platform/core/test/capabilities/MutationCapabilitySpec.js +++ b/platform/core/test/capabilities/MutationCapabilitySpec.js @@ -13,21 +13,9 @@ define( domainObject = { getModel: function () { return testModel; } }, mutation; - function mockPromise(value) { - return { - then: function (callback) { - return (value && value.then) ? - value : mockPromise(callback(value)); - } - }; - } - beforeEach(function () { testModel = { number: 6 }; - mutation = new MutationCapability( - { when: mockPromise }, // $q - domainObject - ); + mutation = new MutationCapability(domainObject); }); it("allows mutation of a model", function () {