Skip to content

Commit c32c31a

Browse files
author
Abhay Jatin Doshi
committed
A few small little updates...
ADDED: dist folder in the repository ADDED: package-lock.json because stackoverflow said I was supposed to :) ADDED: methods getStatus in both Process and ProcessQueue class REMOVED: unwnated npm dependencies
1 parent 0871be9 commit c32c31a

File tree

9 files changed

+300
-9
lines changed

9 files changed

+300
-9
lines changed

.gitignore

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

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ npm install
2222
node build.js
2323
```
2424

25+
## Dependencies
26+
* [UIkit](https://github.com/uikit/uikit) → Used for rendering the process UI
27+
2528
## API
2629
## Process
2730

@@ -57,6 +60,11 @@ node build.js
5760
```
5861
- updates the title in the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
5962

63+
```js
64+
getStatus()
65+
```
66+
- gets the current status of this process
67+
6068

6169
## ProcessQueue
6270

@@ -95,3 +103,7 @@ node build.js
95103
```
96104
- updates the title in the [DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model)
97105

106+
```js
107+
getStatus()
108+
```
109+
- gets the current status of this queue

dist/process.js

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
75+
getStatus(){
76+
return this._.status;
77+
}
78+
}
79+
class ProcessQueue {
80+
constructor(title){
81+
this._ = {};
82+
83+
this._.component = (function(){
84+
var div = document.createElement('div');
85+
86+
var subDiv = document.createElement('div');
87+
div.appendChild(subDiv);
88+
89+
var statusIcon = document.createElement('span');
90+
statusIcon.setAttribute('uk-spinner','ratio: .7');
91+
subDiv.appendChild(statusIcon);
92+
93+
var processTitle = document.createElement('span');
94+
if(title){
95+
processTitle.innerText = ' '+title;
96+
}
97+
subDiv.appendChild(processTitle);
98+
99+
var processElements = document.createElement('div');
100+
processElements.classList.add('uk-margin-medium-left');
101+
div.appendChild(processElements);
102+
103+
return {
104+
success: function(){
105+
statusIcon.removeAttribute('uk-spinner');
106+
statusIcon.classList.remove('uk-spinner');
107+
statusIcon.setAttribute('uk-icon','icon: check');
108+
subDiv.classList.add('uk-text-success');
109+
},
110+
failure: function(){
111+
statusIcon.removeAttribute('uk-spinner');
112+
statusIcon.classList.remove('uk-spinner');
113+
statusIcon.setAttribute('uk-icon','icon: close');
114+
subDiv.classList.add('uk-text-danger');
115+
},
116+
updateTitle: function(){
117+
processTitle.innerText = ' '+title
118+
},
119+
getProcessElement: function(){
120+
return processElements;
121+
},
122+
getElement: function(){
123+
return div;
124+
}
125+
};
126+
127+
})();
128+
129+
var parentData = this._;
130+
this._.status = ProcessStatus.INPROGRESS;
131+
this._.promise = (function(){
132+
var resolve;
133+
var reject;
134+
var promise = new Promise(function(res,rej){
135+
resolve = res;
136+
reject = rej;
137+
});
138+
139+
return {
140+
get: function(){
141+
return promise;
142+
},
143+
resolve: function(){
144+
resolve.apply(undefined,arguments);
145+
},
146+
reject: function(){
147+
reject.apply(undefined,arguments);
148+
}
149+
};
150+
})();
151+
152+
this._.processList = (function(){
153+
var processList = [];
154+
var resolvedCount = 0;
155+
var successCount = 0;
156+
var failureCount = 0;
157+
var parent = parentData;
158+
159+
var successCallback = function(){
160+
resolvedCount++;
161+
successCount++;
162+
if(resolvedCount == processList.length){
163+
if(failureCount == 0){
164+
parent.status = ProcessStatus.SUCCESS;
165+
parent.component.success();
166+
parent.promise.resolve();
167+
} else {
168+
parent.status = ProcessStatus.FAILED;
169+
parent.component.failure();
170+
parent.promise.reject();
171+
}
172+
}
173+
}
174+
175+
var failureCallback = function(){
176+
resolvedCount++;
177+
failureCount++;
178+
if(resolvedCount == processList.length){
179+
parent.status = ProcessStatus.FAILED;
180+
parent.component.failure();
181+
parent.promise.reject();
182+
}
183+
}
184+
185+
return {
186+
push : function(process){
187+
if(process instanceof Process || process instanceof ProcessQueue){
188+
process.then(successCallback, failureCallback);
189+
} else {
190+
throw new TypeError('Invalid process type pushed to the queue');
191+
}
192+
processList.push(process);
193+
process.appendToElement(parent.component.getProcessElement());
194+
}
195+
}
196+
})();
197+
198+
}
199+
200+
appendToElement(element){
201+
element.appendChild(this._.component.getElement());
202+
}
203+
204+
push(process){
205+
this._.processList.push(process);
206+
}
207+
208+
updateTitle(title){
209+
this._.component.updateTitle(title);
210+
}
211+
212+
then(resolve, reject){
213+
this._.promise.get().then(resolve, reject);
214+
}
215+
216+
catch(reject){
217+
this._.promise.get().catch(reject);
218+
}
219+
220+
getStatus(){
221+
return this._.status;
222+
}
223+
}
224+
ProcessStatus = Object.freeze({
225+
'INPROGRESS':'INPROGRESS',
226+
'SUCCESS':'SUCCESS',
227+
'FAILED':'FAILED'
228+
});

dist/process.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
{
22
"name": "process-manager",
3-
"version": "0.1",
4-
"build": "build.js",
3+
"version": "1.0.0",
4+
"description": "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...",
5+
"main": "build.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/abhayjatindoshi/process-manager.git"
12+
},
513
"author": "Abhay Jatin Doshi",
14+
"license": "ISC",
15+
"bugs": {
16+
"url": "https://github.com/abhayjatindoshi/process-manager/issues"
17+
},
18+
"homepage": "https://github.com/abhayjatindoshi/process-manager#readme",
619
"dependencies": {
7-
"babel-core": "^6.26.3",
820
"fs": "0.0.1-security",
9-
"uglify": "^0.1.5",
1021
"uglify-es": "^3.3.9"
1122
}
1223
}

src/Process.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ class Process {
7171
appendToElement(element){
7272
element.appendChild(this._.component.getElement());
7373
}
74+
75+
getStatus(){
76+
return this._.status;
77+
}
7478
}

src/ProcessQueue.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,8 @@ class ProcessQueue {
138138
catch(reject){
139139
this._.promise.get().catch(reject);
140140
}
141+
142+
getStatus(){
143+
return this._.status;
144+
}
141145
}

src/ProcessStatus.js

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

0 commit comments

Comments
 (0)