module.exports = function (options) {
    BBPromise.coroutine.addYieldHandler(bluebird_co.toPromise);
    
    Object.defineProperty(Layer.prototype, "handle", {
        enumerable: true,
        get: function () {
            return this.__handle;
        },
        set: function (fn) {
            this.__handle = bluebird_co.isGeneratorFunction(fn) ? wrap(fn) : fn;
        }
    });
    
    if (options){
        if (options.yieldHandler){
            bluebird_co.addYieldHandler(options.yieldHandler);
        }
        if(options.globals){
            global.Promise = BBPromise;
            global.co = co;
        }
    }

    return {
        Promise: BBPromise,
        co: co
    }
};
Beispiel #2
0
export function async(obj, func, ...args) {
  if (typeof obj == "function") {
    [func, obj] = [obj, null];
  }

  if (typeof obj == "object") {
    func = func.bind(obj);
  }

  BB.coroutine.addYieldHandler(function(yieldedValue) {
    if (typeof yieldedValue !== 'function') {
      return BB.resolve(yieldedValue);
    }
  });

  return BB.coroutine(func).apply(func, [...args]);
}
Beispiel #3
0
var Promise = require('bluebird');
Promise.coroutine.addYieldHandler(function(yieldedValue) {
  if (Array.isArray(yieldedValue)) return Promise.all(yieldedValue);
});


function s4() {
  return Math.floor((1+Math.random())*0x10000).toString(16).substring(1);
};

function uuid() {
  return s4()+s4()+s4()+s4()+s4()+s4()+s4()+s4();
}

function It(testName, generatorFn) {
  it(testName, function(done) {
    Promise.coroutine(generatorFn)(done);
  })
}
function sleep(timer) {
  return Promise.defer(function(resolve, reject) {
    setTimeout(resolve, timer);
  });
}

module.exports.uuid = uuid
module.exports.It = It
module.exports.sleep = sleep
App.module('Collection', function (Collection) {
  'use strict';

  var Promise = require('bluebird');

  var cache = null;

  Promise.coroutine.addYieldHandler(function (yieldedValue) {
    if (Array.isArray(yieldedValue)) {
      return Promise.all(yieldedValue);  
    }
  });

  Collection.Organizations = Backbone.Collection.extend({
    model: App.Model.Organization,

    parse: function (res) {
      res.forEach(function (org) {
        if (_.has(org, 'repos')) {
          org.repos = new Collection.Repositories(org.repos);
        }
      });
      return res;
    }
  });

  var hasCache = function () {
    return !!cache;
  };

  var storeCache = function (collection) {
    cache = JSON.parse(JSON.stringify(collection));
  };

  var restoreCache = function () {
    var collection = new Collection.Organizations();
    collection.set(cache, { parse: true });
    return collection;
  };

  var deleteCache = function () {
    cache = null;
  };

  var API = {
    fetchRemoteOrgs: Promise.coroutine(function* (options) {

      if (options.clearCache) {
        deleteCache();
      }

      if (hasCache()) {
        return restoreCache();
      }

      var userConfig = yield App.request('fetchUserConfig');
      var client = App.request('githubClient', userConfig);

      var orgs = yield client.orgsPromise();
      var orgsCollection = new Collection.Organizations(orgs);

      var orgRepos = yield orgsCollection.map(function (org) {
          return client.reposFromOrgPromise({
            org: org.get('login')
          });
      });

      orgsCollection.each(function (org, i) {
        org.set('repos', new Collection.Repositories(orgRepos[i]));
      });

      storeCache(orgsCollection);

      return orgsCollection;
    })
  };

  App.reqres.setHandler('fetchOrganizations', function (options) {
    options = options || {};
    return API.fetchRemoteOrgs(options);
  });
});