Advertisement
ivansky

JS Task Function Manager

May 2nd, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. var TaskManager = TaskManagerPackage();
  4.  
  5. var userId = 1;
  6.  
  7. // Get TaskManager Environment By UserID
  8. // And created task with some calculations
  9. TaskManager(userId).addTask(function(){
  10.  
  11.     var self = this; // save link to Task instance
  12.  
  13.     this.trigger('test', {hello: 'hello world!'}); // trigger test event
  14.  
  15.     self.done(); // call task done
  16.  
  17. }).settings(function(){
  18.  
  19.     this.some = 'var';
  20.  
  21. }).on('done', function(){
  22.  
  23.     console.log('completed', this.some); // completed var
  24.  
  25. }).on('test',function(context){
  26.  
  27.     console.log(context.hello); // hello world!
  28.  
  29. }).start();
  30.  
  31. // hello world!
  32. // completed var
  33.  
  34. function TaskManagerPackage(){
  35.  
  36.     var instance = {};
  37.     var tasks = [];
  38.  
  39.     function TaskEnvirovment(uid){
  40.  
  41.         function Task(__function){
  42.  
  43.             var events = {};
  44.  
  45.             var users = [];
  46.  
  47.             this.index = -1;
  48.  
  49.             this.name = '';
  50.  
  51.             this.removed = false;
  52.  
  53.             this.getSubscribers = function(){
  54.                 return users;
  55.             };
  56.  
  57.             this.remove = function(){
  58.  
  59.                 if(this.index > -1){
  60.  
  61.                     tasks.splice(this.index, 1);
  62.  
  63.                 }
  64.  
  65.                 this.removed = true;
  66.  
  67.             };
  68.  
  69.             this.settings = function(__c){
  70.                 var c = __c || function(){};
  71.  
  72.                 c.call(this);
  73.  
  74.                 return this;
  75.             };
  76.  
  77.             this.subscribe = function(uid){
  78.  
  79.                 var self = this;
  80.  
  81.                 if(uid.constructor === Array){
  82.  
  83.                     for(var i = 0; i < uid.length; i++){
  84.  
  85.                         if(typeof uid[i] == 'number'){
  86.  
  87.                             self.subscribe(uid[i]);
  88.  
  89.                         }
  90.  
  91.                     }
  92.  
  93.                 }else if(typeof uid == 'number' && users.indexOf(uid) < 0){
  94.  
  95.                     users.push(uid);
  96.  
  97.                 }
  98.  
  99.                 return this;
  100.             };
  101.  
  102.             this.unsubscribe = function(uid){
  103.  
  104.                 var self = this;
  105.  
  106.                 if(uid.constructor === Array){
  107.  
  108.                     for(var i = 0; i < uid.length; i++){
  109.  
  110.                         if(typeof uid[i] == 'number'){
  111.  
  112.                             self.unsubscribe(uid[i]);
  113.  
  114.                         }
  115.  
  116.                     }
  117.  
  118.                 }else if(typeof uid == 'number'){
  119.  
  120.                     var index = users.indexOf(uid);
  121.  
  122.                     if(users.indexOf(uid) > -1){
  123.                         users.splice(index, 1);
  124.                     }
  125.  
  126.                 }
  127.  
  128.                 return this;
  129.             };
  130.  
  131.             this.done = function(){
  132.                 this.trigger('done');
  133.             };
  134.  
  135.             this.trigger = function(name, __context){
  136.  
  137.                 var self = this;
  138.  
  139.                 var context = __context || {};
  140.  
  141.                 if(events.hasOwnProperty(name)){
  142.  
  143.                     events[name].forEach(function(c){
  144.  
  145.                         c.call(self, context);
  146.  
  147.                     });
  148.  
  149.                 }
  150.  
  151.                 return this;
  152.             };
  153.  
  154.             this.on = function(name, __c){
  155.                 var c = __c || function(){};
  156.  
  157.                 if(!events.hasOwnProperty(name)){
  158.                     events[name] = [];
  159.                 }
  160.  
  161.                 if(typeof c == 'function'){
  162.                     events[name].push(c);
  163.                 }
  164.  
  165.                 return this;
  166.             };
  167.  
  168.             this.start = function(){
  169.                 __function.call(this);
  170.             };
  171.  
  172.         }
  173.  
  174.         this.uid = uid;
  175.  
  176.         this.count = 0;
  177.  
  178.         this.taskIdSubscribes = [];
  179.  
  180.         this.addTask = function(__function){
  181.  
  182.             var task = new Task(__function);
  183.  
  184.             var l = tasks.push(task);
  185.  
  186.             var index = l - 1;
  187.  
  188.             task.index = index;
  189.  
  190.             this.taskIdSubscribes.push(index);
  191.  
  192.             return tasks[index];
  193.  
  194.         }
  195.  
  196.     }
  197.  
  198.     return function(uid){
  199.  
  200.         if(!instance.hasOwnProperty(uid)){
  201.             instance[uid] = new TaskEnvirovment(uid);
  202.         }
  203.  
  204.         return instance[uid];
  205.  
  206.     };
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement