Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var TaskManager = TaskManagerPackage();
- var userId = 1;
- // Get TaskManager Environment By UserID
- // And created task with some calculations
- TaskManager(userId).addTask(function(){
- var self = this; // save link to Task instance
- this.trigger('test', {hello: 'hello world!'}); // trigger test event
- self.done(); // call task done
- }).settings(function(){
- this.some = 'var';
- }).on('done', function(){
- console.log('completed', this.some); // completed var
- }).on('test',function(context){
- console.log(context.hello); // hello world!
- }).start();
- // hello world!
- // completed var
- function TaskManagerPackage(){
- var instance = {};
- var tasks = [];
- function TaskEnvirovment(uid){
- function Task(__function){
- var events = {};
- var users = [];
- this.index = -1;
- this.name = '';
- this.removed = false;
- this.getSubscribers = function(){
- return users;
- };
- this.remove = function(){
- if(this.index > -1){
- tasks.splice(this.index, 1);
- }
- this.removed = true;
- };
- this.settings = function(__c){
- var c = __c || function(){};
- c.call(this);
- return this;
- };
- this.subscribe = function(uid){
- var self = this;
- if(uid.constructor === Array){
- for(var i = 0; i < uid.length; i++){
- if(typeof uid[i] == 'number'){
- self.subscribe(uid[i]);
- }
- }
- }else if(typeof uid == 'number' && users.indexOf(uid) < 0){
- users.push(uid);
- }
- return this;
- };
- this.unsubscribe = function(uid){
- var self = this;
- if(uid.constructor === Array){
- for(var i = 0; i < uid.length; i++){
- if(typeof uid[i] == 'number'){
- self.unsubscribe(uid[i]);
- }
- }
- }else if(typeof uid == 'number'){
- var index = users.indexOf(uid);
- if(users.indexOf(uid) > -1){
- users.splice(index, 1);
- }
- }
- return this;
- };
- this.done = function(){
- this.trigger('done');
- };
- this.trigger = function(name, __context){
- var self = this;
- var context = __context || {};
- if(events.hasOwnProperty(name)){
- events[name].forEach(function(c){
- c.call(self, context);
- });
- }
- return this;
- };
- this.on = function(name, __c){
- var c = __c || function(){};
- if(!events.hasOwnProperty(name)){
- events[name] = [];
- }
- if(typeof c == 'function'){
- events[name].push(c);
- }
- return this;
- };
- this.start = function(){
- __function.call(this);
- };
- }
- this.uid = uid;
- this.count = 0;
- this.taskIdSubscribes = [];
- this.addTask = function(__function){
- var task = new Task(__function);
- var l = tasks.push(task);
- var index = l - 1;
- task.index = index;
- this.taskIdSubscribes.push(index);
- return tasks[index];
- }
- }
- return function(uid){
- if(!instance.hasOwnProperty(uid)){
- instance[uid] = new TaskEnvirovment(uid);
- }
- return instance[uid];
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement