-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
45 lines (36 loc) · 1.25 KB
/
index.ts
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
import {AsklessServer} from "../../src";
const server = new AsklessServer();
server.init({
wsOptions: { port : 3000, },
debugLogs: true,
});
const newRandomNumber = () => parseInt(Math.random() * 1001 as any);
// optionally create a type
type DataEntity = { currentRandomNumber: number, generatedAt:Date };
const dataEntity:DataEntity = {
generatedAt: new Date(),
currentRandomNumber: newRandomNumber(),
};
const randomsRoute = server.addRoute.forAllUsers.read<DataEntity>({
route: 'generated-random-number',
handleRead: async (context) => {
console.log("handleRead");
context.successCallback(dataEntity);
},
toOutput: (dataEntity) => {
return {
"generatedAtTimestamp": dataEntity.generatedAt.getTime(),
"currentRandomNumber": dataEntity.currentRandomNumber,
};
}
});
const interval = setInterval(() => {
dataEntity.currentRandomNumber = newRandomNumber();
console.log("new random: "+dataEntity.currentRandomNumber);
dataEntity.generatedAt = new Date();
// because we are not passing handleReadOverride(..),
// handleRead(..) will be called again
randomsRoute.notifyChanges();
}, 1000);
server.start();
console.log('my local server url: '+server.localUrl);