<commit_before>'use strict';

/**
 * @ngdoc function
 * @name Pear2Pear.service:Pear
 * @description
 * # Pear service
 * Provides controllers with a data model for pear to pear app
 * It serves as an abstraction between Pear data and backend (SwellRT)
 */

angular.module('Pear2Pear')
  .factory('pear', ['$rootScope', 'swellRT', '$q', '$timeout', function($rootScope, swellRT, $q, $timeout) {

    var proxy = {
      communities: {}
    };

    // map of opened projects
    var openedProjects = {};

    var def = $q.defer();

    var communities = {

      all: function() {
        return proxy.communities;
      },

      find: function(id) {
        var community = proxy.communities[id];
        return {
          community: community,
          projects: {
            all: function() {
              var promises = {};
              angular.forEach(community.projects, function(val){
                var projDef = $q.defer();
                promises[val] = projDef.promise;
                if (!openedProjects[val]){
                  window.SwellRT.openModel(val, function(model){
                    $timeout(function(){
                      var pr = swellRT.proxy(model);
                      openedProjects[val] = pr;
                      projDef.resolve(pr);
                    });
                  });
                } else {
                  projDef.resolve(openedProjects[val]);
                }
              });

              var projsDef = $q.all(promises);
              return projsDef;
            },
            destroy: function(projId){
              var i = community.projects.indexOf(projId);
              if (i > -1){
                community.projects.splice(i,1);
              }
            }
          }
        };
      },
      create: function(data, callback) {
        var id = window.SwellRT.createModel(function(model){
          var p = swellRT.proxy(model);
          p.name = data.name;
          p.id = id;
          p.projects = [];
          proxy.communities[id] = p;
          callback({
            community: p,
            projects: {
              // TODO avoid repeated function
              all: function() {
                var promises = {};
                angular.forEach(community.projects, function(val){
                  var projDef = $q.defer();
                  promises[val] = projDef.promise;
                  if (!openedProjects[val]){
                    window.SwellRT.openModel(val, function(model){
                      $timeout(function(){
                        var pr = swellRT.proxy(model);
                        openedProjects[val] = pr;
                        projDef.resolve(pr);
                      });
                    });
                  } else {
                    projDef.resolve(openedProjects[val]);
                  }
                });

                var projsDef = $q.all(promises);
                return projsDef;
              }
            }});
        });
      },
      destroy: function(id) {
        delete proxy.communities[id];
        return id;
      }
    };

    var projects = {

      find: function(id) {
        def = $q.defer();

        if (!openedProjects[id]) {
          window.SwellRT.openModel(id, function(model){
            var pr = swellRT.proxy(model);
            openedProjects[id] = pr;
            def.resolve(openedProjects[id]);
          }, function(error){
            def.reject(error);
          });
        } else {
          def.resolve(openedProjects[id]);
        }
        return def.promise;
      },
      create: function(callback) {
        var id = window.SwellRT.createModel(function(model){
          var p = {
            proj : {}
          };
          proxy.proj = swellRT.proxy(model);
          $timeout(function(){
            proxy.proj['id'] = id;
            proxy.proj['title'] = '';
            proxy.proj['chat'] = [];
            proxy.proj['pad'] = new swellRT.TextObject();
            proxy.proj['needs'] = [];
            proxy.proj['promoter'] = users.current();
            openedProjects[id] = proxy.proj;
            callback(proxy.proj);
          });
        });
      }
    };

    var users = {
      current: function() {
        return window.sessionStorage.getItem('userId');
      },
      setCurrent: function(name) {
        return window.sessionStorage.setItem('userId', name);
      },
      isCurrent: function(user) {
        return user === users.current();
      },
      loggedIn: function() {
        return users.current() !== 'undefined' && users.current() !== null;
      }
    };

    var addChatMessage = function(projectId, message) {
      projects.find(projectId).then(function(project){
        project.chat.push({
          text: message,
          who: users.current(),
          time: (new Date()).toJSON()
        });
      }, function(error){
        console.log(error);
      });
    };

    window.onSwellRTReady = function () {
      window.SwellRT.startSession(
        SwellRTConfig.server, SwellRTConfig.user, SwellRTConfig.pass,
        function() {
          // Open Community List
          window.SwellRT.openModel(
            SwellRTConfig.communityListWaveId,
            function(model) {
              proxy.communities = swellRT.proxy(model);
              if (!proxy.communities){
                proxy['communities'] = {};
              }
              def.resolve(proxy.model);
            },
            function(error){
              console.log(error);
            });
          },

        function(error) {
          console.log(error);
        });
    };

    if (window.SwellRT) {
      window.onSwellRTReady();
    }

    return {
      communities: communities,
      projects: projects,
      users: users,
      addChatMessage: addChatMessage,
      onLoad: function(f) {
        def.promise.then(f);
      }
    };
  }]);<commit_msg>add check condition to avoid multiple calls to onSwellRTReady method<commit_after>'use strict';

/**
 * @ngdoc function
 * @name Pear2Pear.service:Pear
 * @description
 * # Pear service
 * Provides controllers with a data model for pear to pear app
 * It serves as an abstraction between Pear data and backend (SwellRT)
 */

angular.module('Pear2Pear')
  .factory('pear', ['$rootScope', 'swellRT', '$q', '$timeout', function($rootScope, swellRT, $q, $timeout) {

    var proxy = {
      communities: {}
    };

    // map of opened projects
    var openedProjects = {};

    var def = $q.defer();

    var communities = {

      all: function() {
        return proxy.communities;
      },

      find: function(id) {
        var community = proxy.communities[id];
        return {
          community: community,
          projects: {
            all: function() {
              var promises = {};
              angular.forEach(community.projects, function(val){
                var projDef = $q.defer();
                promises[val] = projDef.promise;
                if (!openedProjects[val]){
                  window.SwellRT.openModel(val, function(model){
                    $timeout(function(){
                      var pr = swellRT.proxy(model);
                      openedProjects[val] = pr;
                      projDef.resolve(pr);
                    });
                  });
                } else {
                  projDef.resolve(openedProjects[val]);
                }
              });

              var projsDef = $q.all(promises);
              return projsDef;
            },
            destroy: function(projId){
              var i = community.projects.indexOf(projId);
              if (i > -1){
                community.projects.splice(i,1);
              }
            }
          }
        };
      },
      create: function(data, callback) {
        var id = window.SwellRT.createModel(function(model){
          var p = swellRT.proxy(model);
          p.name = data.name;
          p.id = id;
          p.projects = [];
          proxy.communities[id] = p;
          callback({
            community: p,
            projects: {
              // TODO avoid repeated function
              all: function() {
                var promises = {};
                angular.forEach(community.projects, function(val){
                  var projDef = $q.defer();
                  promises[val] = projDef.promise;
                  if (!openedProjects[val]){
                    window.SwellRT.openModel(val, function(model){
                      $timeout(function(){
                        var pr = swellRT.proxy(model);
                        openedProjects[val] = pr;
                        projDef.resolve(pr);
                      });
                    });
                  } else {
                    projDef.resolve(openedProjects[val]);
                  }
                });

                var projsDef = $q.all(promises);
                return projsDef;
              }
            }});
        });
      },
      destroy: function(id) {
        delete proxy.communities[id];
        return id;
      }
    };

    var projects = {

      find: function(id) {
        def = $q.defer();

        if (!openedProjects[id]) {
          window.SwellRT.openModel(id, function(model){
            var pr = swellRT.proxy(model);
            openedProjects[id] = pr;
            def.resolve(openedProjects[id]);
          }, function(error){
            def.reject(error);
          });
        } else {
          def.resolve(openedProjects[id]);
        }
        return def.promise;
      },
      create: function(callback) {
        var id = window.SwellRT.createModel(function(model){
          var p = {
            proj : {}
          };
          proxy.proj = swellRT.proxy(model);
          $timeout(function(){
            proxy.proj['id'] = id;
            proxy.proj['title'] = '';
            proxy.proj['chat'] = [];
            proxy.proj['pad'] = new swellRT.TextObject();
            proxy.proj['needs'] = [];
            proxy.proj['promoter'] = users.current();
            openedProjects[id] = proxy.proj;
            callback(proxy.proj);
          });
        });
      }
    };

    var users = {
      current: function() {
        return window.sessionStorage.getItem('userId');
      },
      setCurrent: function(name) {
        return window.sessionStorage.setItem('userId', name);
      },
      isCurrent: function(user) {
        return user === users.current();
      },
      loggedIn: function() {
        return users.current() !== 'undefined' && users.current() !== null;
      }
    };

    var addChatMessage = function(projectId, message) {
      projects.find(projectId).then(function(project){
        project.chat.push({
          text: message,
          who: users.current(),
          time: (new Date()).toJSON()
        });
      }, function(error){
        console.log(error);
      });
    };

    window.onSwellRTReady = function () {
      window.SwellRT.startSession(
        SwellRTConfig.server, SwellRTConfig.user, SwellRTConfig.pass,
        function() {
          // Open Community List
          window.SwellRT.openModel(
            SwellRTConfig.communityListWaveId,
            function(model) {
              proxy.communities = swellRT.proxy(model);
              if (!proxy.communities){
                proxy['communities'] = {};
              }
              def.resolve(proxy.model);
            },
            function(error){
              console.log(error);
            });
          },

        function(error) {
          console.log(error);
        });
      // to avoid multiple calls
      window.onSwellRTReady = null;
    };

    if (window.SwellRT && typeof window.onSwellRTReady === 'function') {
      window.onSwellRTReady();
    }

    return {
      communities: communities,
      projects: projects,
      users: users,
      addChatMessage: addChatMessage,
      onLoad: function(f) {
        def.promise.then(f);
      }
    };
  }]);
{'fraction_non_alphanumeric': 0.1272271016311167, 'fraction_numerical': 0.0006691760769552489, 'mean_word_length': 1.4168182737012331, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5489531', 'n_tokens_mistral': 3313, 'n_tokens_neox': 3142, 'n_words': 592}