@@ -39,6 +39,8 @@ const events = [
39
39
'team_add' , 'member_add'
40
40
]
41
41
42
+ let listOfFollowees
43
+
42
44
init ( )
43
45
updateClasses ( )
44
46
if ( context === 'user' ) specifyTimelineEvents ( )
@@ -139,46 +141,50 @@ function specifyTimelineEvents() {
139
141
observer . observe ( dashboard , { subtree : true , childList : true } )
140
142
}
141
143
142
- async function getFollowingList ( ) {
144
+ async function getFolloweeList ( ) {
145
+ if ( listOfFollowees ) return listOfFollowees
146
+
143
147
console . log ( 'Dashboard extension: getting list of people you follow from localStorage' )
144
- const following = localStorage . getItem ( 'dashboard:following' )
145
- if ( ! following || ( following && ( new Date ( ) . getTime ( ) - new Date ( JSON . parse ( following ) . updatedAt ) ) / 1000 > 24 * 60 * 60 ) ) {
146
- const results = await fetchFollowing ( )
147
- const following = {
148
+ const followees = localStorage . getItem ( 'dashboard:following' )
149
+ if ( ! followees || ( followees && ( new Date ( ) . getTime ( ) - new Date ( JSON . parse ( followees ) . updatedAt ) ) / 1000 > 24 * 60 * 60 ) ) {
150
+ const results = await fetchFollowees ( )
151
+ const followees = {
148
152
updatedAt : ( new Date ( ) ) . getTime ( ) ,
149
153
following : results
150
154
}
151
- localStorage . setItem ( 'dashboard:following' , JSON . stringify ( following ) )
152
- return results
155
+ localStorage . setItem ( 'dashboard:following' , JSON . stringify ( followees ) )
156
+ listOfFollowees = results
153
157
} else {
154
- return JSON . parse ( following ) . following
158
+ listOfFollowees = JSON . parse ( followees ) . following
155
159
}
160
+
161
+ return listOfFollowees
156
162
}
157
163
158
- async function fetchFollowing ( ) {
164
+ async function fetchFollowees ( ) {
159
165
console . log ( 'Dashboard extension: updating list of people you follow from GitHub API (once every 24h)' )
160
166
return new Promise ( async function ( resolve ) {
161
- let following = [ ]
167
+ let followees = [ ]
162
168
const user = document . querySelector ( '.HeaderNavlink.name img' ) . alt . slice ( 1 )
163
169
const endpoint = `https://api.github.com/users/${ user } /following`
164
170
let page = 1
165
171
while ( page > 0 ) {
166
172
const res = await fetch ( `${ endpoint } ?page=${ page } ` )
167
173
const people = await res . json ( )
168
- following = following . concat ( people )
174
+ followees = followees . concat ( people )
169
175
if ( people . length === 30 ) {
170
176
page ++
171
177
} else {
172
178
page = 0
173
- resolve ( following . map ( o => o . login ) )
179
+ resolve ( followees . map ( o => o . login ) )
174
180
}
175
181
}
176
182
} )
177
183
}
178
184
179
185
// Could break if GitHub changes its markup
180
186
async function addMoreSpecificIdentifiers ( list ) {
181
- const following = await getFollowingList ( )
187
+ const followees = await getFolloweeList ( )
182
188
for ( const record of list ) {
183
189
if ( ! record . target . classList . contains ( 'news' ) ) continue
184
190
for ( const eventItem of record . addedNodes ) {
@@ -188,7 +194,7 @@ async function addMoreSpecificIdentifiers(list) {
188
194
189
195
// Check if any links are to one of the followed people
190
196
const fromFollowedPeople = Array . from ( eventItem . querySelectorAll ( 'a' ) ) . some ( function ( maybeActor ) {
191
- return following . indexOf ( maybeActor . pathname . slice ( 1 ) ) >= 0
197
+ return followees . indexOf ( maybeActor . pathname . slice ( 1 ) ) >= 0
192
198
} )
193
199
eventItem . classList . add ( fromFollowedPeople ? 'by_followed_people' : 'by_internet' )
194
200
}
0 commit comments