-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataSync.re
More file actions
192 lines (147 loc) · 5.87 KB
/
DataSync.re
File metadata and controls
192 lines (147 loc) · 5.87 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
type changeValue =
| ContentBlockCreated(Data.contentBlock)
| ContentBlockUpdated(Data.contentBlock)
| NoteCreated(Data.note)
| NoteUpdated(Data.note)
| NotebookCreated(Data.notebook)
| NotebookUpdated(Data.notebook)
| NotebookDeleted(string)
| NoteDeleted(string);
type change = {
id: string,
change: changeValue,
};
let pendingChanges: ref(list(change)) = ref([]);
let retryQueue: ref(list(change)) = ref([]);
type syncedListener('a) = 'a => Promise.t(unit);
let noteSyncedListener: ref(option(syncedListener(Data.note))) = ref(None);
let notebookSyncedListener: ref(option(syncedListener(Data.notebook))) = ref(None);
let contentBlockSyncedListener: ref(option(syncedListener(Data.contentBlock))) = ref(None);
let setNoteSyncedListener = listener => noteSyncedListener := Some(listener);
let setNotebookSyncedListener = listener => notebookSyncedListener := Some(listener);
let setContentBlockSyncedListener = listener => contentBlockSyncedListener := Some(listener);
let notifyListener = (listener, resource) =>
(
switch (listener^) {
| None => Promise.resolved()
| Some(listener) => listener(resource)
}
)
->Promise.map(v => Belt.Result.Ok(v));
let notifyNoteSyncedListener = notifyListener(noteSyncedListener);
let notifyNotebookSyncedListener = notifyListener(notebookSyncedListener);
let notifyContentBlockSyncedListener = notifyListener(contentBlockSyncedListener);
type pendingChangesListener = int => unit;
let pendingChangesListener: ref(option(pendingChangesListener)) = ref(None);
let setPendingChangesListener = listener => pendingChangesListener := Some(listener);
let removePendingChangesListener = () => pendingChangesListener := None;
let notifyPendingChangesListener = pendingChanges => {
/* FIXME: pendingChanges can currently contain duplicates because it consists of pending changes
+ the retry queue. */
let uniqueCount =
Belt.Set.String.fromArray(Belt.List.toArray(pendingChanges))->Belt.Set.String.size;
switch (pendingChangesListener^) {
| None => ()
| Some(listener) => listener(uniqueCount)
};
};
let syncChange = change =>
switch (change.change) {
| NoteCreated(note) => Api.createNote(note)->Promise.flatMapOk(notifyNoteSyncedListener)
| NoteUpdated(note) => Api.updateNote(note)->Promise.flatMapOk(notifyNoteSyncedListener)
| ContentBlockCreated(contentBlock) =>
Api.createContentBlock(contentBlock)->Promise.flatMapOk(notifyContentBlockSyncedListener)
| ContentBlockUpdated(contentBlock) =>
Api.updateContentBlock(contentBlock)->Promise.flatMapOk(notifyContentBlockSyncedListener)
| NotebookCreated(notebook) =>
Api.createNotebook(notebook)->Promise.flatMapOk(notifyNotebookSyncedListener)
| NotebookUpdated(notebook) =>
Api.updateNotebook(notebook)->Promise.flatMapOk(notifyNotebookSyncedListener)
| NotebookDeleted(notebookId) => Api.deleteNotebook(notebookId)->Promise.mapOk(ignore)
| NoteDeleted(noteId) => Api.deleteNote(noteId)->Promise.mapOk(ignore)
};
let storePendingChanges = () => {
let pendingChangeIds =
(pendingChanges^)->Belt.List.concat(retryQueue^)->Belt.List.map(change => change.id);
DataSyncPersistence.store(pendingChangeIds);
notifyPendingChangesListener(pendingChangeIds);
};
let removePendingChange = change => {
pendingChanges := Belt.List.keep(pendingChanges^, pendingChange => pendingChange !== change);
storePendingChanges();
};
let pushChangeToQueue = (queue, change) => {
queue :=
(queue^)
->Belt.List.keep(pendingChange => pendingChange.id != change.id)
->Belt.List.concat([change]);
storePendingChanges();
};
let pushChange = change => pushChangeToQueue(pendingChanges, change);
let pushContentBlock = (contentBlock: Data.contentBlock) => {
let id = "contentBlock:updated:" ++ contentBlock.id;
let change = {id, change: ContentBlockUpdated(contentBlock)};
pushChange(change);
};
let pushNewContentBlock = (contentBlock: Data.contentBlock) => {
let id = "contentBlock:created:" ++ contentBlock.id;
let change = {id, change: ContentBlockCreated(contentBlock)};
pushChange(change);
};
let pushNewNote = (note: Data.note) => {
let id = "note:created:" ++ note.id;
let change = {id, change: NoteCreated(note)};
pushChange(change);
};
let pushNoteChange = (note: Data.note) => {
let id = "note:updated:" ++ note.id;
let change = {id, change: NoteUpdated(note)};
pushChange(change);
};
let pushNewNotebook = (notebook: Data.notebook) => {
let id = "notebook:created:" ++ notebook.id;
let change = {id, change: NotebookCreated(notebook)};
pushChange(change);
};
let pushNotebookChange = (notebook: Data.notebook) => {
let id = "notebook:updated:" ++ notebook.id;
let change = {id, change: NotebookUpdated(notebook)};
pushChange(change);
};
let pushNotebookDelete = (notebookId: string) => {
let id = "notebook:deleted:" ++ notebookId;
let change = {id, change: NotebookDeleted(notebookId)};
pushChange(change);
};
let pushNoteDelete = (noteId: string) => {
let id = "note:deleted:" ++ noteId;
let change = {id, change: NoteDeleted(noteId)};
pushChange(change);
};
let rec syncPendingChanges = onComplete => {
let nextChange = Belt.List.take(pendingChanges^, 1);
switch (nextChange) {
| Some([change]) =>
syncChange(change)
->Promise.get(result => {
if (Belt.Result.isError(result)) {
Js.Console.error2("Error syncing: ", result);
pushChangeToQueue(retryQueue, change);
};
removePendingChange(change);
syncPendingChanges(onComplete);
})
| _ => onComplete()
};
};
let retryFailed = () => {
Belt.List.forEach(retryQueue^, pushChange);
retryQueue := [];
};
let start = persistedChanges => {
Belt.List.forEach(persistedChanges, pushChange);
let rec onComplete = () =>
Js.Global.setTimeout(() => syncPendingChanges(onComplete), 3_000) |> ignore;
syncPendingChanges(onComplete);
Js.Global.setInterval(retryFailed, 10_000) |> ignore;
};