This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.ts
60 lines (56 loc) · 1.72 KB
/
sync.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Tempo, isoServerTime } from './tempo';
const createRoom = (): Promise<string> => {
console.log('creating room');
return fetch('https://jsonstorage.net/api/items', {
method: 'POST',
body: JSON.stringify(Tempo.defaultTempo()),
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
})
.then((response) => {
return response.json();
})
.then((json) => {
return json.uri;
})
.catch((error) => {
console.error(error);
});
};
const updateRoomTempo = (roomEndpoint: string, tempo: Tempo): void => {
console.log('updating room', roomEndpoint, tempo);
fetch(roomEndpoint, {
method: 'PUT',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(tempo),
})
.catch((error) => {
console.error(error);
});
};
const getRoomTempo = (roomEndpoint: string): Promise<Tempo> => {
return new Promise((resolve, reject) => {
fetch(roomEndpoint)
.then((response) => {
return response.json();
})
.then((tempo: {bpm: number, startTime: string} ) => {
const { bpm, startTime } = tempo;
if (!(bpm && startTime)) {
reject('invalid room contents');
}
resolve(new Tempo(
bpm,
isoServerTime.wrap(new Date(startTime))
));
})
.catch((error) => {
console.error(error);
reject();
});
});
};
export { createRoom, updateRoomTempo, getRoomTempo };