2020-01-20 12:23:00 +01:00
|
|
|
import Observable from 'core-js-pure/features/observable'
|
2020-01-20 03:26:18 +01:00
|
|
|
import { observableToAsyncIterator } from '/services/misc.js'
|
|
|
|
|
|
|
|
const LINK_RE = /<(.+?)>; rel="(\w+)"/gi
|
|
|
|
|
|
|
|
function parseLinkHeader(link) {
|
|
|
|
const links = {}
|
|
|
|
|
|
|
|
for (const [ , url, name ] of link.matchAll(LINK_RE)) {
|
|
|
|
links[name] = url
|
|
|
|
}
|
|
|
|
|
|
|
|
return links
|
|
|
|
}
|
|
|
|
|
|
|
|
export const hashtagStreamingObservable = (domain, hashtag) => {
|
|
|
|
return new Observable(observer => {
|
2020-02-04 20:07:34 +01:00
|
|
|
const onOpen = () => {
|
|
|
|
console.log(`Streaming ${domain} #${hashtag} : open`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onStatus = event => {
|
|
|
|
const status = JSON.parse(event.data)
|
|
|
|
console.log(`Streaming ${domain} #${hashtag} : status ${status.id}`)
|
|
|
|
observer.next(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
const onError = error => {
|
|
|
|
console.error(`Streaming ${domain} #${hashtag} : error`)
|
|
|
|
console.error(error)
|
|
|
|
observer.error(error)
|
|
|
|
}
|
2020-01-20 03:26:18 +01:00
|
|
|
|
|
|
|
const eventSource = new EventSource(`https://${domain}/api/v1/streaming/hashtag?tag=${hashtag}`)
|
2020-02-04 20:07:34 +01:00
|
|
|
eventSource.addEventListener('open', onOpen)
|
2020-01-20 03:26:18 +01:00
|
|
|
eventSource.addEventListener('update', onStatus)
|
|
|
|
eventSource.addEventListener('error', onError)
|
|
|
|
|
|
|
|
return () => {
|
2020-02-04 20:07:34 +01:00
|
|
|
eventSource.removeEventListener('open', onOpen)
|
2020-01-20 03:26:18 +01:00
|
|
|
eventSource.removeEventListener('update', onStatus)
|
|
|
|
eventSource.removeEventListener('error', onError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function* hashtagTimelineIterator (domain, hashtag) {
|
|
|
|
let nextLink = `https://${domain}/api/v1/timelines/tag/${hashtag}?limit=40`
|
|
|
|
|
|
|
|
while (nextLink) {
|
|
|
|
const response = await fetch(nextLink)
|
|
|
|
|
|
|
|
nextLink = response.headers.has('link')
|
|
|
|
? parseLinkHeader(response.headers.get('link')).next
|
|
|
|
: null
|
|
|
|
|
2020-02-04 20:07:34 +01:00
|
|
|
const statuses = await response.json()
|
|
|
|
|
|
|
|
console.log(`Timeline ${domain} #${hashtag} : fetched ${statuses.length} statuses`)
|
|
|
|
|
|
|
|
yield* statuses
|
2020-01-20 03:26:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function* hashtagIterator(domain, hashtag) {
|
|
|
|
const newerIterator = observableToAsyncIterator(hashtagStreamingObservable(domain, hashtag))
|
|
|
|
const olderIterator = hashtagTimelineIterator(domain, hashtag)
|
|
|
|
|
2020-02-04 20:07:34 +01:00
|
|
|
const iterators = [newerIterator, olderIterator]
|
|
|
|
const values = iterators.map(iterator => iterator.next())
|
2020-01-20 03:26:18 +01:00
|
|
|
|
|
|
|
while (true) {
|
2020-02-04 20:07:34 +01:00
|
|
|
const promises = values.map((promise, index) => promise.then(result => ({ index, result })))
|
2020-01-20 03:26:18 +01:00
|
|
|
const { index, result: { done, value } } = await Promise.race(promises)
|
|
|
|
|
2020-02-04 20:07:34 +01:00
|
|
|
values[index] = iterators[index].next()
|
2020-01-20 03:26:18 +01:00
|
|
|
|
2020-02-04 20:07:34 +01:00
|
|
|
console.log(`Resolver ${domain} #${hashtag} : resolved with iterator ${index} status ${value.id}`)
|
2020-01-20 03:26:18 +01:00
|
|
|
yield value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|