-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
65 lines (54 loc) · 2.83 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//demo file for the package
let clockAbortHandler = function( ) {console.log('==============>>>>>> clock stopped')};
let productAbortHandler = function( ) {console.log('==============>>>>>> products lookup stopped')};
let clockPauseHandler = function( ) {console.log('==============>>>>>> clock paused')};
let productPauseHandler = function( ) {console.log('==============>>>>>> products lookup paused')};
let clockResumeHandler = function( ) {
return new Promise( r => setTimeout(
function(){
console.log('==============>>>>>>++++ clock resumed');
r('resolved');
},3000));
};
let productResumeHandler = function( ) {console.log('==============>>>>>> products lookup resumed')};
let clockNoObserversHandler = function( ) {console.log('==============>>>>>> There are no observers'); /*clock_sse.abortCycle()*/};
let clock_sse = new AlternateSSE({path:''}, { stopCount:Infinity, intervalSeconds: 1,
onBeforeAbort: clockAbortHandler,
onBeforePause: clockPauseHandler,
onBeforeResume: clockResumeHandler,
onNoObservers: clockNoObserversHandler
}
);
let products_sse = new AlternateSSE({path:''}, { stopCount:5, intervalSeconds: 4,
onBeforeAbort: productAbortHandler,
onBeforePause: productPauseHandler,
onBeforeResume: productResumeHandler
}
);
let counter = 0;
jsonpath = 'https://reqres.in/api/products/';
//***************************************** */
adjustFetchOptions = ( ) =>{
if ( counter > 5 )
return products_sse.abortCycle( );
counter++;
console.log(`${jsonpath}${counter}`);
return {path:`${jsonpath}${counter}`, headers:{originator: 'macleen'}}
}
function display_products(response) {
console.log('response =>>>:', response);
let id = response.response?.data?.id;
if ( id ) document.querySelector('#prod-'+id).innerHTML = 'name of prod-'+id+': '+response.response.data.name+' and its color: '+response.response.data.color;
else throw new Error('ID is not defined');
}
function display_clock( response ) {
let date = (new Date(Date.now( ))).toString( ).split(' ');;
document.querySelector('#clock').innerHTML = date[4];
};
function products( ) {
products_sse.setFetchOptions(adjustFetchOptions)
.subscribe(display_products);
}
function clock( ) {
clock_sse.subscribe(display_clock);
}