Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 52x 52x 15x 15x 19x 52x 52x 2x 2x 2x 2x 2x 2x 2x 15x 15x 23x 23x 23x 23x 15x 15x 15x 15x 15x 15x 9x 9x 9x 9x 9x 9x 9x 15x 15x 15x 15x 2x 2x 2x 2x 2x 2x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 2x 2x 2x 2x 2x 14x 14x 14x 14x 14x 14x 14x 14x 2x 2x 2x 2x 9x 9x 9x 9x 9x 5x 5x 5x 4x 4x 4x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 9x 4x 4x 4x 2x 2x 2x 2x 2x 2x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 12x 12x 12x 11x 11x 12x 12x 12x 12x 11x 11x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11x 11x 11x 2x 11x 9x 9x 11x 2x 2x 2x 2x 2x 2x 2x 18x 18x 18x 18x 4x 18x 18x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 15x 15x 6x 6x 13x 9x 11x 1x 5x 15x 5x 1x 9x 13x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { isPlainObject } from '../../utils/object.js'
import { parseCSVResponse, isCSVResponse } from '../parses-csv-data-response/parse-csv-response.js'
import { parseJsonResponse, isJsonResponse } from '../parses-json-array-and-object-data-response/parse-json-response.js'
import { isJsonLinesResponse, parseJsonLinesResponse } from '../parses-json-lines-object-array-data-response/parse-json-lines-response.js'
/** @type {WeakMap<HTMLElement, DataLoader>} */
const dataLoaderData = new WeakMap()
/**
* @param {HTMLElement} element - target element
* @returns {DataLoader} dataloader of element
*/
export function dataLoaderOf (element) {
let dataLoader = dataLoaderData.get(element)
if (!dataLoader) {
dataLoader = createDataLoaderFor(new WeakRef(element))
dataLoaderData.set(element, dataLoader)
}
return dataLoader
}
/**
* Creates a dataloader for an element
* @param {WeakRef<HTMLElement>} elementRef - weak reference of element. We do not want to have any strong reference chain pointing to
* globally allocated `dataLoaderData`, effectively creating a memory leak
* @returns {DataLoader} - created dataloader for element
*/
function createDataLoaderFor (elementRef) {
const getElement = () => {
const element = elementRef.deref()
if (!element) { throw Error('element no longer exists') }
return element
}
/** @type {DataLoader} */
const api = {
fetchData: async () => await fetchData(getElement()),
fetchNextData: async () => await fetchNextData(getElement()),
fetchHistory: [],
getLatestSuccessResponse () {
for (let i = api.fetchHistory.length - 1; i >= 0; i--) {
const record = api.fetchHistory[i]
if (record.completed) {
const result = record.result
if (!('error' in result)) { return result }
}
}
},
}
return api
}
/**
*
* @param {HTMLElement} element - target element
* @param {DataToFetch} dataToFetch - info for data fetch
*/
function dispatchFetchDataEvent (element, dataToFetch) {
let customResponse = /** @type {any} */(null)
let respondWithCalls = 0
const event = new CustomEvent('datafetch', {
cancelable: true,
composed: true,
bubbles: true,
detail: {
dataToFetch,
/**
* @param {any} data - response data to handle
*/
respondWith (data) {
customResponse = data
respondWithCalls++
},
},
})
element.dispatchEvent(event)
return { event, customResponse, respondWithCalls }
}
/**
*
* @param {HTMLElement} element - target element to load data to
*/
async function fetchData (element) {
const dataToFetch = {
query: getQueryValue(element),
url: getUrlToFetch(element),
}
return fetchFromDataToFetch(element, dataToFetch)
}
/**
* @param {HTMLElement} element - target element to load data to
*/
async function fetchNextData (element) {
const loader = dataLoaderOf(element)
const currentData = loader.getLatestSuccessResponse()
if (currentData == null) {
return await fetchData(element)
}
if (!currentData.hasMore) {
// no need to fetch since we have all the data, return an empty result
return Object.freeze({ data: [], hasMore: false })
}
const dataToFetch = (() => {
if (currentData.navigationMode === 'link') {
return {
query: getQueryValue(element),
url: currentData.href,
}
}
if (currentData.navigationMode === 'after_value') {
const latestValue = currentData.data.at(-1)?.value
const additionalQueryParams = latestValue ? Object.freeze({ after: String(latestValue) }) : {}
return {
query: getQueryValue(element),
url: getUrlToFetch(element, additionalQueryParams),
}
}
})()
if (!dataToFetch) {
console.error('error getting next data: unreachable code detected, aborting fetch')
return currentData
}
return fetchFromDataToFetch(element, dataToFetch)
}
/**
*
* @param {HTMLElement} element - target element
* @param {DataToFetch} dataToFetch -
*/
async function fetchFromDataToFetch (element, dataToFetch) {
const fetchRecord = addToFetchHistory(element, {
dataToFetch,
loadingMode: 'sync',
status: 'dispatching event',
result: null,
completed: false,
})
const { event, customResponse, respondWithCalls } = dispatchFetchDataEvent(element, dataToFetch)
if (respondWithCalls > 0) {
const isPromiseLike = typeof customResponse?.['then'] === 'function'
const isResponse = customResponse instanceof Response
fetchRecord.loadingMode = isPromiseLike || isResponse ? 'async' : 'sync'
fetchRecord.result = await parseRespondWithCall(customResponse)
fetchRecord.completed = true
if ('error' in fetchRecord.result) {
throw Error(fetchRecord.result.error)
}
return fetchRecord.result
}
const { url } = dataToFetch
if (!event.defaultPrevented && url) {
fetchRecord.loadingMode = 'async'
fetchRecord.status = 'fetching data'
const response = await fetch(url)
fetchRecord.status = 'parsing response'
fetchRecord.result = await parseResponse(response)
fetchRecord.status = Object.hasOwn(fetchRecord.result, 'error') ? 'error' : 'completed'
fetchRecord.completed = true
if ('error' in fetchRecord.result) {
throw Error(fetchRecord.result.error)
}
return fetchRecord.result
}
if (!url) {
fetchRecord.loadingMode = 'sync'
fetchRecord.completed = true
fetchRecord.result = {
data: [],
hasMore: false,
}
return fetchRecord.result
}
fetchRecord.loadingMode = 'sync'
fetchRecord.completed = true
fetchRecord.result = {
error: 'no data loaded',
stage: 'loading data',
}
throw Error('no data to load')
}
/**
*
* @param {*} paramOfRespondWith - param sent to event.detail.respondWith()
* @returns {Promise<ParsedResponse | ParseError>} parse result
*/
async function parseRespondWithCall (paramOfRespondWith) {
const result = await Promise.resolve(paramOfRespondWith)
if (result instanceof Response) {
const parsedResponse = await parseResponse(result)
if ('error' in parsedResponse) {
parsedResponse.error = `parse event .respondWith(Response): ${parsedResponse.error}`
}
return parsedResponse
}
if (Array.isArray(result)) {
return {
data: result,
hasMore: false,
}
}
if (isPlainObject(result)) {
const { hasMore, links, records } = result
if (!Array.isArray(records)) {
return {
error: `records property must be an array, instead it is ${records}`,
stage: 'parse event .respondWith(Object)',
}
}
if (typeof links?.next === 'string') {
return {
data: records,
hasMore: true,
navigationMode: 'link',
href: links.next,
}
}
if (hasMore) {
return {
data: records,
hasMore: true,
navigationMode: 'after_value',
}
}
}
return {
error: 'invalid data on RespondsWith, param must be an array, plain object or Response',
stage: 'parse event .respondWith(...)',
}
}
/**
* @param {Response} response - response from fetch
* @returns {Promise<ParsedResponse | ParseError>} parse result
*/
async function parseResponse (response) {
if (response.ok) {
if (isCSVResponse(response)) {
return await parseCSVResponse(response)
} else if (isJsonResponse(response)) {
return await parseJsonResponse(response)
} else if (isJsonLinesResponse(response)) {
return await parseJsonLinesResponse(response)
} else {
return {
error: 'invalid response, expected JSON, JSON Lines or CSV response, guarantee that Content-type header is set correctly to "text/csv", "application/json" or "application/jsonl"',
stage: 'parse fetch response',
}
}
}
return {
error: `${response.status} HTTP status code`,
stage: 'parse fetch response',
}
}
/**
* Adds a record to fetch history of an element data loader
* Each data loader has an history to at most 128 records. Large enough to debug, small enough to not have a big memory footprint
* @param {HTMLElement} element - target element to get data loader of
* @param {FetchRecord} record - fetch record to save
*/
function addToFetchHistory (element, record) {
const { fetchHistory } = dataLoaderOf(element)
fetchHistory.push(record)
if (fetchHistory.length > 128) {
fetchHistory.shift()
}
return record
}
/**
* @param {HTMLElement} element - target element
*/
const getQueryValue = (element) => element.getAttribute('data-filter') ?? ''
/**
* @param {HTMLElement} element - target element
*/
const getDataSource = (element) => element.getAttribute('data-src') ?? ''
/**
* @param {HTMLElement} element - target element
* @param {Record<string, string>} additionalQueryParams - query parameters to add to resulting url
*/
const getUrlToFetch = (element, additionalQueryParams = {}) => {
const src = getDataSource(element)
if (!src) {
return src
}
const query = getQueryValue(element)
const srcURL = new URL(src, window.location.href)
if (query) {
srcURL.searchParams.set('q', query)
}
for (const [name, value] of Object.entries(additionalQueryParams)) {
srcURL.searchParams.set(name, value)
}
return srcURL.href
}
/**
* @typedef {object} DataLoader
* @property {() => Promise<ParsedResponse>} fetchData - fetches data and saves to current data
* @property {() => Promise<ParsedResponse>} fetchNextData - used for paginated options, fetches next page
* @property {FetchRecord[]} fetchHistory - get fetch history, history is no longer than 128 requests
* @property {()=>ParsedResponse|undefined} getLatestSuccessResponse - current data for select box
*/
/**
* @typedef {object} ParsedHasMoreResponse
* @property {true} hasMore - flag to determine if there is more data after the last element
* @property {{[prop: string]: string}[]} data - options data
*/
/**
* @typedef {object} ParsedNoMoreResponse
* @property {false} hasMore - flag to determine if there is more data after the last element
* @property {{[prop: string]: string}[]} data - options data
*/
/**
* @typedef {object} LinkPaginatedParsedResponse
* @property {"link"} navigationMode - navigation mode
* @property {string} href - link url
*/
/**
* @typedef {object} AfterLastValuePaginatedParsedResponse
* @property {"after_value"} navigationMode - navigation mode
*/
/**
* @typedef {ParsedNoMoreResponse | (ParsedHasMoreResponse & (AfterLastValuePaginatedParsedResponse | LinkPaginatedParsedResponse))} ParsedResponse
*/
/**
* @typedef {object} ParseError
* @property {string} error - error message
* @property {string} stage - stage where error happened
*/
/**
* @typedef {object} FetchRecordLoading
* @property {DataToFetch} dataToFetch - error message
* @property {"sync"|"async"} loadingMode - flag to indicate if the record is being fetched synchronously or asynchronously
* @property {string} status - fetch status
* @property {false} completed - completed flag
* @property {null} result - stage where error happened
*/
/**
* @typedef {object} FetchRecordCompletedSuccess
* @property {DataToFetch} dataToFetch - error message
* @property {"sync"|"async"} loadingMode - flag to indicate if the record is being fetched synchronously or asynchronously
* @property {"completed"} status - fetch status
* @property {true} completed - completed flag
* @property {ParsedResponse} result - stage where error happened
*/
/**
* @typedef {object} FetchRecordCompletedError
* @property {DataToFetch} dataToFetch - error message
* @property {"sync"|"async"} loadingMode - flag to indicate if the record is being fetched synchronously or asynchronously
* @property {"error"} status - fetch status
* @property {true} completed - completed flag
* @property {ParseError} result - stage where error happened
*/
/**
* @typedef {FetchRecordLoading | FetchRecordCompletedSuccess | FetchRecordCompletedError} FetchRecord
*/
/**
* @typedef {object} DataToFetch
* @property {string} query - search filter applied to search
* @property {string} url - request URL endpoint
*/
|