-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathtodo.service.ts
More file actions
102 lines (85 loc) · 3.02 KB
/
todo.service.ts
File metadata and controls
102 lines (85 loc) · 3.02 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import {environment} from '../environments/environment';
import cettia from 'cettia-client/cettia-bundler';
import {ChangeEvent, ChangeType, decodeChangeEvent, decodeTodos, encodeChangeEvent, Todo} from "./protos/changeevent";
@Injectable({
providedIn: 'root'
})
export class TodoService {
private todos: Map<string, Todo> = new Map();
private todosSubject = new BehaviorSubject<Todo[]>([]);
public todosObservable = this.todosSubject.asObservable();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private socket: any = null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private cache: any = [];
constructor() {
this.socket = cettia.open(environment.SERVER_URL);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.socket.on('initial', (msg: any) => {
const initialTodos = decodeTodos(msg);
if (!initialTodos.todos) {
return;
}
for (const todo of initialTodos.todos) {
if (todo.id) {
this.todos.set(todo.id, todo);
}
}
this.todosSubject.next([...this.todos.values()]);
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.socket.on('update', (msg: any) => {
const changeEvent = decodeChangeEvent(msg);
const todo = changeEvent.todo;
if (todo?.id) {
if (changeEvent.change === ChangeType.DELETE) {
this.todos.delete(todo.id);
} else if (changeEvent.change === ChangeType.INSERT) {
this.todos.set(todo.id, todo);
} else if (changeEvent.change === ChangeType.UPDATE) {
this.todos.set(todo.id, todo);
}
this.todosSubject.next([...this.todos.values()]);
}
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.socket.on('cache', (args: any) => this.cache.push(args));
this.socket.on('open', () => {
while (this.socket.state() === 'opened' && this.cache.length > 0) {
const args = this.cache.shift();
this.socket.send(...args);
}
});
}
getTodo(id: string): Todo | undefined {
return this.todos.get(id);
}
deleteTodo(todo: Todo): void {
if (todo.id) {
const deleted = this.todos.delete(todo.id);
if (deleted) {
this.todosSubject.next([...this.todos.values()]);
const changeEvent: ChangeEvent = {change: ChangeType.DELETE, todo};
const buffer = encodeChangeEvent(changeEvent);
this.socket.send('update', buffer);
}
}
}
save(todo: Todo): void {
let changeType;
if (todo.id) {
if (this.todos.has(todo.id)) {
changeType = ChangeType.UPDATE;
} else {
changeType = ChangeType.INSERT;
}
const changeEvent: ChangeEvent = {change: changeType, todo};
const buffer = encodeChangeEvent(changeEvent);
this.socket.send('update', buffer);
this.todos.set(todo.id, todo);
this.todosSubject.next([...this.todos.values()]);
}
}
}