-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew Text Document.txt
195 lines (177 loc) · 6.48 KB
/
New Text Document.txt
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
193
194
195
import { DEFAULT_ACTIVE_LIST_URLS } from './../../constants/lists';
import { createReducer } from '@reduxjs/toolkit';
import { getVersionUpgrade, VersionUpgrade } from '@uniswap/token-lists';
import { TokenList } from '@uniswap/token-lists/dist/types';
import { DEFAULT_LIST_OF_LISTS } from '../../constants/lists';
import { updateVersion } from '../global/actions';
import { acceptListUpdate, addList, fetchTokenList, removeList, enableList, disableList } from './actions';
export interface ListsState {
readonly byUrl: {
readonly [url: string]: {
readonly current: TokenList | null;
readonly pendingUpdate: TokenList | null;
readonly loadingRequestId: string | null;
readonly error: string | null;
};
};
// this contains the default list of lists from the last time the updateVersion was called, i.e. the app was reloaded
readonly lastInitializedDefaultListOfLists?: string[];
// currently active lists
readonly activeListUrls: string[] | undefined;
}
type ListState = ListsState['byUrl'][string];
const NEW_LIST_STATE: ListState = {
error: null,
current: null,
loadingRequestId: null,
pendingUpdate: null,
};
type Mutable<T> = { -readonly [P in keyof T]: T[P] extends ReadonlyArray<infer U> ? U[] : T[P] };
const initialState: ListsState = {
lastInitializedDefaultListOfLists: DEFAULT_LIST_OF_LISTS,
byUrl: {
...DEFAULT_LIST_OF_LISTS.reduce<Mutable<ListsState['byUrl']>>((memo, listUrl) => {
memo[listUrl] = NEW_LIST_STATE;
return memo;
}, {}),
},
activeListUrls: DEFAULT_ACTIVE_LIST_URLS,
};
export default createReducer(initialState, (builder) =>
builder
.addCase(fetchTokenList.pending, (state, { payload: { requestId, url } }) => {
/*state.byUrl[url] = {
current: null,
pendingUpdate: null,
...state.byUrl[url],
loadingRequestId: requestId,
error: null,*/
const current = state.byUrl[url]?.current ? state.byUrl[url]?.current : null
const pendingUpdate = state.byUrl[url]?.pendingUpdate ? state.byUrl[url]?.pendingUpdate : null
state.byUrl[url] = {
...state.byUrl[url],
current: current,
pendingUpdate: pendingUpdate,
loadingRequestId: requestId,
error: null,
};
})
.addCase(fetchTokenList.fulfilled, (state, { payload: { requestId, tokenList, url } }) => {
const current = state.byUrl[url]?.current;
const loadingRequestId = state.byUrl[url]?.loadingRequestId;
// no-op if update does nothing
if (current) {
const upgradeType = getVersionUpgrade(current.version, tokenList.version);
if (upgradeType === VersionUpgrade.NONE) return;
if (loadingRequestId === null || loadingRequestId === requestId) {
state.byUrl[url] = {
...state.byUrl[url],
loadingRequestId: null,
error: null,
current: current,
pendingUpdate: tokenList,
};
}
} else {
// activate if on default active
if (DEFAULT_ACTIVE_LIST_URLS.includes(url)) {
state.activeListUrls?.push(url);
}
state.byUrl[url] = {
...state.byUrl[url],
loadingRequestId: null,
error: null,
current: tokenList,
pendingUpdate: null,
};
}
})
.addCase(fetchTokenList.rejected, (state, { payload: { url, requestId, errorMessage } }) => {
if (state.byUrl[url]?.loadingRequestId !== requestId) {
// no-op since it's not the latest request
return;
}
state.byUrl[url] = {
...state.byUrl[url],
loadingRequestId: null,
error: errorMessage,
current: null,
pendingUpdate: null,
};
})
.addCase(addList, (state, { payload: url }) => {
if (!state.byUrl[url]) {
state.byUrl[url] = NEW_LIST_STATE;
}
})
.addCase(removeList, (state, { payload: url }) => {
if (state.byUrl[url]) {
delete state.byUrl[url];
}
// remove list from active urls if needed
if (state.activeListUrls && state.activeListUrls.includes(url)) {
state.activeListUrls = state.activeListUrls.filter((u) => u !== url);
}
})
.addCase(enableList, (state, { payload: url }) => {
if (!state.byUrl[url]) {
state.byUrl[url] = NEW_LIST_STATE;
}
if (state.activeListUrls && !state.activeListUrls.includes(url)) {
state.activeListUrls.push(url);
}
if (!state.activeListUrls) {
state.activeListUrls = [url];
}
})
.addCase(disableList, (state, { payload: url }) => {
if (state.activeListUrls && state.activeListUrls.includes(url)) {
state.activeListUrls = state.activeListUrls.filter((u) => u !== url);
}
})
.addCase(acceptListUpdate, (state, { payload: url }) => {
if (!state.byUrl[url]?.pendingUpdate) {
throw new Error('accept list update called without pending update');
}
state.byUrl[url] = {
...state.byUrl[url],
pendingUpdate: null,
current: state.byUrl[url].pendingUpdate,
};
})
.addCase(updateVersion, (state) => {
// state loaded from localStorage, but new lists have never been initialized
if (!state.lastInitializedDefaultListOfLists) {
state.byUrl = initialState.byUrl;
state.activeListUrls = initialState.activeListUrls;
} else if (state.lastInitializedDefaultListOfLists) {
const lastInitializedSet = state.lastInitializedDefaultListOfLists.reduce<Set<string>>(
(s, l) => s.add(l),
new Set()
);
const newListOfListsSet = DEFAULT_LIST_OF_LISTS.reduce<Set<string>>((s, l) => s.add(l), new Set());
DEFAULT_LIST_OF_LISTS.forEach((listUrl) => {
if (!lastInitializedSet.has(listUrl)) {
state.byUrl[listUrl] = NEW_LIST_STATE;
}
});
state.lastInitializedDefaultListOfLists.forEach((listUrl) => {
if (!newListOfListsSet.has(listUrl)) {
delete state.byUrl[listUrl];
}
});
}
state.lastInitializedDefaultListOfLists = DEFAULT_LIST_OF_LISTS;
// if no active lists, activate defaults
if (!state.activeListUrls) {
state.activeListUrls = DEFAULT_ACTIVE_LIST_URLS;
// for each list on default list, initialize if needed
DEFAULT_ACTIVE_LIST_URLS.map((listUrl: string) => {
if (!state.byUrl[listUrl]) {
state.byUrl[listUrl] = NEW_LIST_STATE;
}
return true;
});
}
})
);