Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for configuration and non-functional handlers (when.execute) #20

Closed
wants to merge 5 commits into from

2 participants

@gamtiq

Hello,

I have added support for configuration parameters, public API customization and non-functional handlers/callbacks (when.execute method that we discussed).
Maybe you will find it is useful.

Best regards,
Denis

@briancavalier

I haven't seen a need for this from the community, so I'm going to close. If the topic comes up again, and there's sufficient interest, we can revisit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 14, 2012
  1. @gamtiq

    "customize", "execute" and "functionalHandler" configuration paramete…

    gamtiq authored
    …rs are added. The possibility to use non-functional handlers/callbacks is added.
Commits on Mar 30, 2012
  1. @gamtiq

    Update to 1.0.4

    gamtiq authored
Commits on Apr 8, 2013
  1. @gamtiq

    Merge remote-tracking branch 'upstream/master'

    gamtiq authored
    Conflicts:
    	README.md
    	when.js
  2. @gamtiq
Commits on Aug 7, 2013
  1. @gamtiq

    Merge remote-tracking branch 'upstream/master'

    gamtiq authored
    Conflicts:
    	when.js
This page is out of date. Refresh to see the latest.
Showing with 40 additions and 7 deletions.
  1. +40 −7 when.js
View
47 when.js
@@ -4,6 +4,19 @@
* A lightweight CommonJS Promises/A and when() implementation
* when is part of the cujo.js family of libraries (http://cujojs.com/)
*
+ * External/global configuration object with name 'when' (e.g. window.when) can be used to configure behavior of when.
+ * Fields of the object determine values of configuration settings.
+ * <p>
+ * The following configuration settings are supported (name - type - description):
+ * <ul>
+ * <li>customize - Function - function that should be called before returning module definition;
+ * the module definition is passed as an argument; for example, can be used to change public API
+ * or clear/delete global config.
+ * <li>execute - Function - function that should be used to call/execute any handler/callback instead of
+ * default implementation; a handler is passed as the first argument,
+ * the resolution/rejection/progress value is passed as the second argument.
+ * </ul>
+ *
* Licensed under the MIT License at:
* http://www.opensource.org/licenses/mit-license.php
*
@@ -33,6 +46,21 @@ define(function () {
when.isPromise = isPromise; // Determine if a thing is a promise
+ var config = when.config = (global['when'] && Object(global['when'])) || {}; // Configuration
+
+ /**
+ * Calls/executes the given handler.
+ *
+ * @param {Function} handler the handler that should be executed.
+ * @param {*} value the value that should be passed to the handler.
+ * @returns {*} the handler's call result.
+ */
+ var execute = when.execute =
+ typeof config.execute === "function"
+ ? config.execute
+ : function(handler, value) {
+ return handler(value);
+ };
/**
* Register an observer for a promise or immediate value.
@@ -370,8 +398,8 @@ define(function () {
function fulfilled(value) {
var self = new Promise(function (onFulfilled) {
try {
- return typeof onFulfilled == 'function'
- ? coerce(onFulfilled(value)) : self;
+ return onFulfilled && (typeof onFulfilled == 'function' || typeof onFulfilled == 'object')
+ ? coerce(execute(onFulfilled, value)) : self;
} catch (e) {
return rejected(e);
}
@@ -391,8 +419,8 @@ define(function () {
function rejected(reason) {
var self = new Promise(function (_, onRejected) {
try {
- return typeof onRejected == 'function'
- ? coerce(onRejected(reason)) : self;
+ return onRejected && (typeof onRejected == 'function' || typeof onRejected == 'object')
+ ? coerce(execute(onRejected, reason)) : self;
} catch (e) {
return rejected(e);
}
@@ -412,8 +440,8 @@ define(function () {
function progressing(update) {
var self = new Promise(function (_, __, onProgress) {
try {
- return typeof onProgress == 'function'
- ? progressing(onProgress(update)) : self;
+ return onProgress && (typeof onProgress == 'function' || typeof onProgress == 'object')
+ ? progressing(execute(onProgress, update)) : self;
} catch (e) {
return progressing(e);
}
@@ -433,7 +461,7 @@ define(function () {
enqueue(function() {
var handler, i = 0;
while (handler = handlers[i++]) {
- handler(value);
+ execute(handler, value);
}
});
}
@@ -829,6 +857,11 @@ define(function () {
return x;
}
+
+ if (config.customize) {
+ execute(config.customize, when);
+ }
+
return when;
});
})(
Something went wrong with that request. Please try again.