Skip to content

Commit 80e39f4

Browse files
committed
Added provider
1 parent 0f8be74 commit 80e39f4

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

src/core/Provider.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import Event from './event';
2+
3+
/**
4+
* Provider of data. In general, add provider to the controller using
5+
* the define method. Then for any component which is to be associated
6+
* with a provider, use the name of the provider.
7+
* @class
8+
*/
9+
export default class Provider extends EventTarget {
10+
#origin;
11+
#timer;
12+
#interval;
13+
14+
constructor(origin) {
15+
super();
16+
this.#origin = origin ? new URL(origin) : '/';
17+
}
18+
19+
/**
20+
* Get the origin of the provider
21+
* @returns {String}
22+
* @readonly
23+
* @memberof Provider
24+
*/
25+
get origin() {
26+
return this.#origin;
27+
}
28+
29+
/**
30+
* Cancel any existing request interval timer.
31+
* @memberof Provider
32+
*/
33+
cancel() {
34+
if (this.#timer) {
35+
clearTimeout(this.#timer);
36+
this.#timer = null;
37+
}
38+
}
39+
40+
fetch(path, request, interval) {
41+
// Create an absolute URL
42+
let url = new URL(path, this.#origin);
43+
44+
// Cancel any existing requests
45+
this.cancel();
46+
47+
// Fetch the data
48+
this.#fetch(url, request);
49+
50+
// Set the interval for the next fetch
51+
if (interval) {
52+
this.#interval = interval;
53+
this.#timer = setTimeout(() => {
54+
this.#fetch(url, request);
55+
}, interval);
56+
} else {
57+
this.#timer = null;
58+
}
59+
}
60+
61+
#fetch(url, request) {
62+
this.dispatchEvent(new CustomEvent(Event.EVENT_START, {
63+
detail: url
64+
}));
65+
fetch(url, request).then((response) => {
66+
if (!response.ok) {
67+
throw new Error(`status: ${response.status}`);
68+
}
69+
const contentType = response.headers ? response.headers.get('Content-Type') || '' : '';
70+
switch (contentType.split(';')[0]) {
71+
case 'application/json':
72+
case 'text/json':
73+
return response.json();
74+
case 'text/plain':
75+
case 'text/html':
76+
return response.text();
77+
default:
78+
return response.blob();
79+
}
80+
}).then((data) => {
81+
if (typeof data == "string") {
82+
this.#string(data);
83+
} else if (data instanceof Array) {
84+
this.#array(data);
85+
} else if (data instanceof Object) {
86+
this.#object(data);
87+
} else {
88+
this.#blob(data);
89+
}
90+
}).catch((error) => {
91+
this.dispatchEvent(new ErrorEvent(Event.EVENT_ERROR, {
92+
error: error,
93+
message: `${error}`
94+
}));
95+
}).finally(() => {
96+
this.dispatchEvent(new CustomEvent(Event.EVENT_DONE, {
97+
detail: url
98+
}));
99+
if (this.#timer && this.#interval) {
100+
this.cancel();
101+
this.#timer = setTimeout(() => {
102+
this.#fetch(url, request);
103+
}, this.#interval);
104+
}
105+
});
106+
}
107+
108+
/**
109+
* Private method to process array of objects
110+
*/
111+
#array(data) {
112+
data.forEach((item) => {
113+
this.#object(data);
114+
});
115+
}
116+
117+
/**
118+
* Private method to process objects
119+
*/
120+
#object(data) {
121+
//console.log("Object: ", data);
122+
}
123+
124+
/**
125+
* Private method to process string data
126+
*/
127+
#string(data) {
128+
//console.log("String: ", data);
129+
}
130+
131+
/**
132+
* Private method to process blob data
133+
*/
134+
#blob(data) {
135+
//console.log("Blob: ", data);
136+
}
137+
}

src/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import './component/button/CloseButtonElement';
88
import './component/icon/IconElement';
99
import './component/nav/NavBarElement';
1010

11+
// Core
12+
import Provider from './core/Provider';
13+
import Event from './core/event';
1114

1215
// CSS
1316
import './css/core.css';
@@ -16,4 +19,16 @@ import './css/document.css';
1619
// Test
1720
window.addEventListener('load', () => {
1821
new EventSource('/esbuild').addEventListener('change', () => location.reload());
22+
23+
var p = new Provider("http://localhost:8000/");
24+
p.addEventListener(Event.EVENT_ERROR,(evt) => {
25+
console.log("Got error:",evt);
26+
});
27+
p.addEventListener(Event.EVENT_START,(evt) => {
28+
console.log("START",evt.detail);
29+
});
30+
p.addEventListener(Event.EVENT_DONE,(evt) => {
31+
console.log("DONE",evt.detail);
32+
});
33+
p.fetch("/", {}, 5000);
1934
});

0 commit comments

Comments
 (0)