-
Notifications
You must be signed in to change notification settings - Fork 34
/
kitsuLibrary.tsx
157 lines (133 loc) · 3.5 KB
/
kitsuLibrary.tsx
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
import { _EventBus } from './eventBus';
/**
* Library sort options
*/
export class KitsuLibrarySort {
static get DATE_UPDATED() {
return 'updated_at';
}
static get DATE_PROGRESSED() {
return 'progressed_at';
}
static get DATE_STARTED() {
return 'started_at';
}
static get DATE_FINSIHED() {
return 'finished_at';
}
static get DATE_ADDED() {
return 'created_at';
}
static get PROGRESS() {
return 'progress';
}
static get RATING() {
return 'rating';
}
static get TITLE() {
return 'title';
}
static get LENGTH() {
return 'length';
}
static get POPULARITY() {
return 'popularity';
}
static get AVERAGE_RATING() {
return 'average';
}
}
/**
* Events of the library
*/
export class KitsuLibraryEvents {
static get LIBRARY_ENTRY_CREATE() {
return 'library-entry-create';
}
static get LIBRARY_ENTRY_UPDATE() {
return 'library-entry-update';
}
static get LIBRARY_ENTRY_DELETE() {
return 'library-entry-delete';
}
}
/**
* Sources of the library events
*/
export class KitsuLibraryEventSource {
static get STORE() {
return 'store';
}
static get QUICK_UPDATE() {
return 'quickupdate';
}
static get MEDIA_PAGE() {
return 'mediapage';
}
}
/**
* A helper class for keeping redux library state in sync
*/
class _KitsuLibrary extends _EventBus {
/**
* Publish Library entry create event.
*
* @param {any} entry The library entry that was created.
* @param {string} libraryType The type of library. `anime` or `manga`.
* @param {string} source The source of the event.
*/
onLibraryEntryCreate(entry, libraryType, source = null) {
this.publish(KitsuLibraryEvents.LIBRARY_ENTRY_CREATE, {
id: entry.id,
status: entry.status,
type: libraryType,
entry,
source,
});
}
/**
* Publish Library entry update event.
*
* @param {any} oldEntry The old library entry.
* @param {any} newEntry The updated library entry.
* @param {string} libraryType The type of library. `anime` or `manga`.
* @param {string} source The source of the update.
*/
onLibraryEntryUpdate(oldEntry, newEntry, libraryType, source = null) {
if (!oldEntry || !newEntry) return;
// Make sure the needed relationship values are not overridden
const combined = newEntry;
if (!combined.anime) combined.anime = oldEntry.anime;
if (!combined.manga) combined.manga = oldEntry.manga;
if (!combined.user) combined.user = oldEntry.user;
// Only combine unit if progress are same, otherwise they'll be different.
if (oldEntry.progress === newEntry.progress) {
if (!combined.unit) combined.unit = oldEntry.unit;
if (!combined.nextUnit) combined.nextUnit = oldEntry.nextUnit;
}
this.publish(KitsuLibraryEvents.LIBRARY_ENTRY_UPDATE, {
id: newEntry.id,
type: libraryType,
oldEntry,
newEntry: combined,
source,
});
}
/**
* Publish Library entry delete event.
*
* @param {number} id The id of the library entry that was deleted
* @param {string} libraryType The type of the library. `anime` or `manga`.
* @param {string} libraryStatus The status of the library entry.
* @param {string} source The source of the delete.
*/
onLibraryEntryDelete(id, libraryType, libraryStatus, source = null) {
this.publish(KitsuLibraryEvents.LIBRARY_ENTRY_DELETE, {
id,
type: libraryType,
status: libraryStatus,
source,
});
}
}
export const KitsuLibrary = new _KitsuLibrary();