Skip to content

Commit b8cbaf4

Browse files
committed
Tidy up
1 parent 0730628 commit b8cbaf4

File tree

13 files changed

+57
-51
lines changed

13 files changed

+57
-51
lines changed

.github/workflows/.gh-action.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ jobs:
2222
node-version: '22.x'
2323
- run: (cd 2-3-4-Exercises; cp config-default.js config.js)
2424
- run: (cd 2-3-4-Exercises; npm ci)
25-
- run: (cd 2-3-4-Exercises; npm test)
26-
- run: (cd 2-3-4-Exercises; npm run coverage)
25+
- run: (cd 2-3-4-Exercises; npm run coverage)
26+
27+
# check plicate service, also the remote data double use loading spinner,
28+
# check all code quality

1-Introductory-Exercises/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Include required modules
21
const {HttpServer, WebSocket, WebSocketMessage, LogManager} = require('@aliceo2/web-ui');
32

43
const logger = LogManager.getLogger('Exercise1');
@@ -12,7 +11,7 @@ const http = new HttpServer({
1211
port: 8080,
1312
}, jwtConfig);
1413

15-
// Server `public` folder
14+
// Serve the frontend
1615
http.addStaticPath('public');
1716

1817
http.get('/helloWorld', (req, res) => {
@@ -25,7 +24,7 @@ http.get('/testJWT', (req, res) => {
2524
});
2625

2726
http.get('/addDefaultUser', (req, res) => {
28-
http.addDefaultUserData(req, res); // adds /api/userData endpoint
27+
http.addDefaultUserData(req, res);
2928
}, { public: true});
3029

3130
// curl cmd to test it

1-Introductory-Exercises/public/index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
<title>Hello World</title>
88
<link rel="shortcut icon" href="o2-favicon.png" />
99
<script type="module" src="./webSocket.js"></script>
10-
11-
1210
</head>
1311

1412
<body>

1-Introductory-Exercises/public/webSocket.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {h, mount, Observable, WebSocketClient, sessionService} from '/js/src/index.js';
22

3-
3+
// Simple Model View setup
44
const model = new Observable();
5-
const view = (model) => h('h1.title', `${model.title}`);
6-
mount(document.body, view, model);
75
model.title = 'Websocket Example';
86
model.notify();
97

10-
sessionService.loadAndHideParameters();
8+
const view = (model) => h('h1.title', `${model.title}`);
9+
10+
mount(document.body, view, model);
1111

12+
sessionService.loadAndHideParameters();
1213

1314
const ws = new WebSocketClient();
1415

2-3-4-Exercises/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path');
22
const config = require('./config.js');
33

4-
const {HttpServer, LogManager} = require('@aliceo2/web-ui');
4+
const {HttpServer, LogManager, WebSocket, WebSocketMessage} = require('@aliceo2/web-ui');
55

66
const logger = LogManager.getLogger('Exercise2');
77

@@ -15,3 +15,14 @@ http.get('/applicationData', (req, res) => {
1515
});
1616
});
1717

18+
const ws = new WebSocket(http);
19+
20+
ws.bind('random-number', (message) => message);
21+
22+
setInterval(() => {
23+
const randomNum = Math.floor(Math.random() * 100);
24+
const msg = new WebSocketMessage(200).setCommand('random-number-update')
25+
.setPayload({number: randomNum});
26+
ws.broadcast(msg);
27+
logger.infoMessage(`Broadcasted random number: ${randomNum}`);
28+
}, 5000);
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
class ApplicationService {
2-
constructor() {
3-
this.details = {
4-
appName: '2-3-4 Exercises Application',
5-
};
2+
constructor(appDetails) {
3+
this.details = appDetails
64

75
}
86

97
getDetails() {
108
return this.details;
119
}
12-
1310
}
1411

1512
export default ApplicationService;

2-3-4-Exercises/public/Model.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Observable, QueryRouter, Loader, sessionService} from '/js/src/index.js';
1+
import {Observable, QueryRouter, Loader, sessionService, WebSocketClient} from '/js/src/index.js';
22
import Home from './home/Home.js';
33
import About from './about/About.js';
44

@@ -21,7 +21,7 @@ export default class Model extends Observable {
2121
this.loader = new Loader(this);
2222
this.loader.bubbleTo(this);
2323

24-
// Sub-models
24+
// Setup sub-models
2525
this.home = new Home();
2626
this.home.bubbleTo(this);
2727

@@ -33,6 +33,18 @@ export default class Model extends Observable {
3333
this.router.observe(this.handleLocationChange.bind(this));
3434
this.router.bubbleTo(this);
3535

36+
this.randomNumber = null;
37+
38+
this.ws = new WebSocketClient();
39+
40+
this.ws.addListener('command', (message) => {
41+
if (message.command === 'random-number-update') {
42+
console.log('Received message from server:', message.payload);
43+
this.randomNumber = message.payload.number;
44+
this.notify();
45+
}
46+
});
47+
3648
this.handleLocationChange(); // Init first page
3749
}
3850

2-3-4-Exercises/public/about/About.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class About extends Observable {
3030
this.details = details;
3131
this.notify();
3232
}
33-
3433
}
3534

3635
export default About;

2-3-4-Exercises/public/about/aboutPage.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import {h, fetchClient, RemoteData} from '/js/src/index.js';
1+
import {h} from '/js/src/index.js';
22
import {iconLaptop, iconHome} from '/js/src/icons.js'
33

44
const content = (model) => {
55

66
const getStatusMessage = (remoteData) => remoteData.match({
7-
NotAsked: () => "Data has not been fetched from the server", // Display a message to the user saying that data has not been fetched
8-
Loading: () => "Loading, please wait", // A request has probably been sent to the server but we did not receive any response yet
9-
Success: () => "Data has been successfully loaded", // The server response has been stored in the remote data payload, and it is passed as parameter to the Success callback
10-
Failure: (error) => `An error has occurred: ${error.message}`, // An error has occurred, displays its message
7+
NotAsked: () => "Data has not been fetched from the server",
8+
Loading: () => "Loading, please wait",
9+
Success: () => "Data has been successfully loaded",
10+
Failure: (error) => `An error has occurred: ${error.message}`,
1111
});
1212

1313
const getTableData = (remoteData) => remoteData.match({
14-
NotAsked: () => ({}), // No data to display
15-
Loading: () => ({}), // No data to display
16-
Success: (data) => data, // The server response has been stored in the remote data payload, and it is passed as parameter to the Success callback
17-
Failure: (error) => ({}), // No data to display
14+
NotAsked: () => ({}),
15+
Loading: () => ({}),
16+
Success: (data) => data,
17+
Failure: (error) => ({}),
1818
});
1919

2020
return h('div.m2', [
2121
h('p', 'Welcome to the about page of our application.'),
2222
h('div.flex.flex-column.w-33', [
2323
h('.btn.btn-primary.mv2' ,{
24-
// on load print to console its purpose
2524
oninit: () => {
2625
console.log('This button takes you to the Home page');
2726
},
@@ -30,17 +29,17 @@ const content = (model) => {
3029
},
3130
id: 'home-button'
3231
}, [h('div.flex.flex-row.justify-center.items-center', [h('p', 'Home'), h('span.mh1', iconHome())])]),
32+
3333
h('.btn.btn-primary.mv2' ,{
34-
// on load print to console its purpose
3534
oninit: () => {
3635
console.log('This button tells you application data');
3736
},
3837
onclick: () => {
3938
model.about.requestData()
40-
4139
},
4240
id: 'request-application-data-button'
4341
}, [h('div.flex.flex-row.justify-center.items-center', [h('p', 'Request Application Data'), h('span.mh1', iconLaptop())])]),
42+
4443
]),
4544
h('p.mt2', {id: 'application-data-status-display'} ,`Your app data status: ${getStatusMessage(model.about.getDetails())}`),
4645
h('table.table', [

2-3-4-Exercises/public/home/homePage.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const content = (model) => {
77
h('p', 'Welcome to the home page of our application.'),
88
h('div.flex.flex-column.w-33', [
99
h('.btn.btn-primary.mv2', {
10-
// on load print to console its purpose
1110
oninit: () => {
1211
console.log('This button takes you to the About page');
1312
},
@@ -16,8 +15,8 @@ const content = (model) => {
1615
},
1716
id: 'about-button'
1817
}, [h('div.flex.flex-row.justify-center.items-center', [h('p', 'About'), h('span.mh1', info())])]),
18+
1919
h('.btn.btn-primary.mv2', {
20-
// on load print to console its purpose
2120
oninit: () => {
2221
console.log('This button tells you your username');
2322
},

0 commit comments

Comments
 (0)