Esempio n. 1
0
export function suspendListeners(obj, eventNames, target, method, callback) {
  if (!method && 'function' === typeof target) {
    method = target;
    target = null;
  }

  var suspendedActions = [],
      actionsList = [],
      eventName, actions, i, l;

  for (i=0, l=eventNames.length; i<l; i++) {
    eventName = eventNames[i];
    actions = actionsFor(obj, eventName);
    var actionIndex = indexOf(actions, target, method);

    if (actionIndex !== -1) {
      actions[actionIndex+2] |= SUSPENDED;
      suspendedActions.push(actionIndex);
      actionsList.push(actions);
    }
  }

  function tryable() { return callback.call(target); }

  function finalizer() {
    for (var i = 0, l = suspendedActions.length; i < l; i++) {
      var actionIndex = suspendedActions[i];
      actionsList[i][actionIndex+2] &= ~SUSPENDED;
    }
  }

  return tryFinally(tryable, finalizer);
}
Esempio n. 2
0
QUnit.test("no failure, return from finally", function() {
  finalizerResult = 'finalizer return value';

  equal(tryFinally(tryable, finalizer), finalizerResult, 'crrect return value');

  equal(tryCount, 1, 'tryable was called once');
  equal(finalizeCount, 1, 'finalize was called once');
});
Esempio n. 3
0
function callTryFinallyWithError() {
  var errorWasThrown;
  try {
    tryFinally(tryable, finalizer);
  } catch(e) {
    errorWasThrown = true;
    equal(e, error, 'correct error was thrown');
  }

  equal(errorWasThrown, true, 'error was thrown');
}
Esempio n. 4
0
export function suspendListener(obj, eventName, target, method, callback) {
  if (!method && 'function' === typeof target) {
    method = target;
    target = null;
  }

  var actions = actionsFor(obj, eventName);
  var actionIndex = indexOf(actions, target, method);

  if (actionIndex !== -1) {
    actions[actionIndex+2] |= SUSPENDED; // mark the action as suspended
  }

  function tryable()   { return callback.call(target); }
  function finalizer() { if (actionIndex !== -1) { actions[actionIndex+2] &= ~SUSPENDED; } }

  return tryFinally(tryable, finalizer);
}
Esempio n. 5
0
QUnit.test("no failure", function() {
  equal(tryFinally(tryable, finalizer), tryableResult, 'correct return value');

  equal(tryCount, 1, 'tryable was called once');
  equal(finalizeCount, 1, 'finalize was called once');
});
Esempio n. 6
0
/**
  Make a series of property changes together in an
  exception-safe way.

  ```javascript
  Ember.changeProperties(function() {
    obj1.set('foo', mayBlowUpWhenSet);
    obj2.set('bar', baz);
  });
  ```

  @method changeProperties
  @param {Function} callback
  @param [binding]
*/
function changeProperties(cb, binding) {
  beginPropertyChanges();
  tryFinally(cb, endPropertyChanges, binding);
}