Skip to content

Commit 0871be9

Browse files
author
Abhay Jatin Doshi
committed
It's just a beginning, Initial commit
0 parents  commit 0871be9

File tree

7 files changed

+352
-0
lines changed

7 files changed

+352
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package-lock.json
2+
.vscode/
3+
dist/
4+
node_modules/

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Process manager
2+
This manager helps you to handle multiple promises effortlessly. This library will handle processes and its sub processes and its sub process and so on...
3+
4+
## Simplest Code
5+
```js
6+
var processQueue = new ProcessQueue;
7+
var process1 = new Process(function(){/* promise invoker function */});
8+
processQueue.push(process1);
9+
var process2...
10+
var process3 ...
11+
```
12+
13+
## Developers
14+
**Build dist**
15+
Just a simple node program run on build.js file.
16+
* Make sure you have npm and node installed. If not [click here](https://nodejs.org/en/download/)
17+
* On terminal do the following
18+
```sh
19+
git clone https://github.com/abhayjatindoshi/process-manager.git
20+
cd process-manager/
21+
npm install
22+
node build.js
23+
```
24+
25+
## API
26+
## Process
27+
28+
**Syntax**
29+
30+
```js
31+
new Process(invokingFunction[,title]);
32+
new Process(promise[,title]);
33+
```
34+
- invokingFunction → function that should be invoked inside the [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
35+
- promise → or [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) object itself
36+
- title → initial title to display on [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
37+
38+
**Methods**
39+
40+
```js
41+
appendToElement(DOMElement);
42+
```
43+
- DOMElement → to attach the Process UI to a [DOMElement](https://developer.mozilla.org/en-US/docs/Web/API/Element)
44+
45+
```js
46+
then(resolve,reject);
47+
```
48+
- resolve, reject → functions that we pass to a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
49+
50+
```js
51+
catch(reject);
52+
```
53+
- reject → function that we pass to a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
54+
55+
```js
56+
updateTitle(title);
57+
```
58+
- updates the title in the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
59+
60+
61+
## ProcessQueue
62+
63+
**Syntax**
64+
65+
```js
66+
new ProccessQueue([title]);
67+
```
68+
- title → initial title to display on [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
69+
70+
**Methods**
71+
72+
```js
73+
appendToElement(DOMElement);
74+
```
75+
- DOMElement → to attach the ProcessQueue to a [DOMElement](https://developer.mozilla.org/en-US/docs/Web/API/Element)
76+
77+
```js
78+
push(Process);
79+
push(ProcessQueue);
80+
```
81+
- any `Process` or `ProcessQueue` type of object can be pushed into the queue
82+
83+
```js
84+
then(resolve,reject);
85+
```
86+
- resolve, reject → functions that we pass to a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
87+
88+
```js
89+
catch(reject);
90+
```
91+
- reject → function that we pass to a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
92+
93+
```js
94+
updateTitle(title);
95+
```
96+
- updates the title in the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
97+

build.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
global.fs = require('fs');
2+
global.uglify = require("uglify-es");
3+
4+
const SRC_DIR = './src/';
5+
const DIST_DIR = './dist/';
6+
7+
var sourceFileNames = fs.readdirSync(SRC_DIR);
8+
var code = '';
9+
var minifiedCode = '';
10+
for(var i = 0; i < sourceFileNames.length ; i++){
11+
var originalCode = fs.readFileSync(SRC_DIR+sourceFileNames[i]);
12+
code += originalCode.toString('utf8') + '\n';
13+
}
14+
minifiedCode = uglify.minify(code);
15+
if(!fs.existsSync(DIST_DIR)){
16+
fs.mkdirSync(DIST_DIR);
17+
}
18+
fs.writeFileSync(DIST_DIR+'process.js',code);
19+
fs.writeFileSync(DIST_DIR+'process.min.js',minifiedCode.code);

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "process-manager",
3+
"version": "0.1",
4+
"build": "build.js",
5+
"author": "Abhay Jatin Doshi",
6+
"dependencies": {
7+
"babel-core": "^6.26.3",
8+
"fs": "0.0.1-security",
9+
"uglify": "^0.1.5",
10+
"uglify-es": "^3.3.9"
11+
}
12+
}

src/Process.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class Process {
2+
constructor(invoker,title){
3+
this._ = {};
4+
if(invoker instanceof Function){
5+
this._.promise = new Promise(invoker);
6+
} else if (invoker instanceof Promise){
7+
this._.promise = invoker;
8+
} else {
9+
throw new TypeError('Invalid invoker passed for Process constructor');
10+
}
11+
12+
this._.component = (function(){
13+
var div = document.createElement('div');
14+
15+
var statusIcon = document.createElement('span');
16+
statusIcon.setAttribute('uk-spinner','ratio: .7');
17+
div.appendChild(statusIcon);
18+
19+
var processTitle = document.createElement('span');
20+
if(title){
21+
processTitle.innerText = ' '+title;
22+
}
23+
div.appendChild(processTitle);
24+
25+
return {
26+
success: function(){
27+
statusIcon.removeAttribute('uk-spinner');
28+
statusIcon.classList.remove('uk-spinner');
29+
statusIcon.setAttribute('uk-icon','icon: check');
30+
div.classList.add('uk-text-success');
31+
},
32+
failure: function(){
33+
statusIcon.removeAttribute('uk-spinner');
34+
statusIcon.classList.remove('uk-spinner');
35+
statusIcon.setAttribute('uk-icon','icon: close');
36+
div.classList.add('uk-text-danger');
37+
},
38+
updateTitle: function(title){
39+
processTitle.innerText = ' '+title
40+
},
41+
getElement: function(){
42+
return div;
43+
}
44+
};
45+
})();
46+
47+
var parent = this._;
48+
this._.status = ProcessStatus.INPROGRESS;
49+
this._.promise.then(function(){
50+
parent.status = ProcessStatus.SUCCESS;
51+
parent.component.success();
52+
},function(){
53+
parent.status = ProcessStatus.FAILED;
54+
parent.component.failure();
55+
});
56+
57+
}
58+
59+
updateTitle(title){
60+
this._.component.updateTitle(title);
61+
}
62+
63+
then(resolve, reject){
64+
this._.promise.then(resolve, reject);
65+
}
66+
67+
catch(reject){
68+
this._.promise.catch(reject);
69+
}
70+
71+
appendToElement(element){
72+
element.appendChild(this._.component.getElement());
73+
}
74+
}

src/ProcessQueue.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
class ProcessQueue {
2+
constructor(title){
3+
this._ = {};
4+
5+
this._.component = (function(){
6+
var div = document.createElement('div');
7+
8+
var subDiv = document.createElement('div');
9+
div.appendChild(subDiv);
10+
11+
var statusIcon = document.createElement('span');
12+
statusIcon.setAttribute('uk-spinner','ratio: .7');
13+
subDiv.appendChild(statusIcon);
14+
15+
var processTitle = document.createElement('span');
16+
if(title){
17+
processTitle.innerText = ' '+title;
18+
}
19+
subDiv.appendChild(processTitle);
20+
21+
var processElements = document.createElement('div');
22+
processElements.classList.add('uk-margin-medium-left');
23+
div.appendChild(processElements);
24+
25+
return {
26+
success: function(){
27+
statusIcon.removeAttribute('uk-spinner');
28+
statusIcon.classList.remove('uk-spinner');
29+
statusIcon.setAttribute('uk-icon','icon: check');
30+
subDiv.classList.add('uk-text-success');
31+
},
32+
failure: function(){
33+
statusIcon.removeAttribute('uk-spinner');
34+
statusIcon.classList.remove('uk-spinner');
35+
statusIcon.setAttribute('uk-icon','icon: close');
36+
subDiv.classList.add('uk-text-danger');
37+
},
38+
updateTitle: function(){
39+
processTitle.innerText = ' '+title
40+
},
41+
getProcessElement: function(){
42+
return processElements;
43+
},
44+
getElement: function(){
45+
return div;
46+
}
47+
};
48+
49+
})();
50+
51+
var parentData = this._;
52+
this._.status = ProcessStatus.INPROGRESS;
53+
this._.promise = (function(){
54+
var resolve;
55+
var reject;
56+
var promise = new Promise(function(res,rej){
57+
resolve = res;
58+
reject = rej;
59+
});
60+
61+
return {
62+
get: function(){
63+
return promise;
64+
},
65+
resolve: function(){
66+
resolve.apply(undefined,arguments);
67+
},
68+
reject: function(){
69+
reject.apply(undefined,arguments);
70+
}
71+
};
72+
})();
73+
74+
this._.processList = (function(){
75+
var processList = [];
76+
var resolvedCount = 0;
77+
var successCount = 0;
78+
var failureCount = 0;
79+
var parent = parentData;
80+
81+
var successCallback = function(){
82+
resolvedCount++;
83+
successCount++;
84+
if(resolvedCount == processList.length){
85+
if(failureCount == 0){
86+
parent.status = ProcessStatus.SUCCESS;
87+
parent.component.success();
88+
parent.promise.resolve();
89+
} else {
90+
parent.status = ProcessStatus.FAILED;
91+
parent.component.failure();
92+
parent.promise.reject();
93+
}
94+
}
95+
}
96+
97+
var failureCallback = function(){
98+
resolvedCount++;
99+
failureCount++;
100+
if(resolvedCount == processList.length){
101+
parent.status = ProcessStatus.FAILED;
102+
parent.component.failure();
103+
parent.promise.reject();
104+
}
105+
}
106+
107+
return {
108+
push : function(process){
109+
if(process instanceof Process || process instanceof ProcessQueue){
110+
process.then(successCallback, failureCallback);
111+
} else {
112+
throw new TypeError('Invalid process type pushed to the queue');
113+
}
114+
processList.push(process);
115+
process.appendToElement(parent.component.getProcessElement());
116+
}
117+
}
118+
})();
119+
120+
}
121+
122+
appendToElement(element){
123+
element.appendChild(this._.component.getElement());
124+
}
125+
126+
push(process){
127+
this._.processList.push(process);
128+
}
129+
130+
updateTitle(title){
131+
this._.component.updateTitle(title);
132+
}
133+
134+
then(resolve, reject){
135+
this._.promise.get().then(resolve, reject);
136+
}
137+
138+
catch(reject){
139+
this._.promise.get().catch(reject);
140+
}
141+
}

src/ProcessStatus.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ProcessStatus = Object.freeze({
2+
'INPROGRESS':0,
3+
'SUCCESS':1,
4+
'FAILED':2
5+
});

0 commit comments

Comments
 (0)