Refactored instant loading and some other components

This commit is contained in:
squidfunk
2021-02-12 12:12:49 +01:00
parent 884330da3b
commit 9c9ea8a64d
33 changed files with 463 additions and 150 deletions

View File

@@ -22,10 +22,10 @@
export * from "./document"
export * from "./element"
export * from "./fetch"
export * from "./keyboard"
export * from "./location"
export * from "./media"
export * from "./request"
export * from "./toggle"
export * from "./viewport"
export * from "./worker"

View File

@@ -20,8 +20,14 @@
* IN THE SOFTWARE.
*/
import { Observable, fromEvent, merge } from "rxjs"
import { filter, map, mapTo, startWith } from "rxjs/operators"
import { NEVER, Observable, fromEvent, merge } from "rxjs"
import {
filter,
map,
mapTo,
startWith,
switchMap
} from "rxjs/operators"
/* ----------------------------------------------------------------------------
* Functions
@@ -57,3 +63,24 @@ export function watchPrint(): Observable<void> {
mapTo(undefined)
)
}
/* ------------------------------------------------------------------------- */
/**
* Toggle an observable with another one
*
* @template T - Data type
*
* @param toggle$ - Toggle observable
* @param factory - Observable factory
*
* @returns Toggled observable
*/
export function at<T>(
toggle$: Observable<boolean>, factory: () => Observable<T>
): Observable<T> {
return toggle$
.pipe(
switchMap(active => active ? factory() : NEVER)
)
}

View File

@@ -28,21 +28,29 @@ import {
switchMap
} from "rxjs/operators"
/* ----------------------------------------------------------------------------
* Data
* ------------------------------------------------------------------------- */
/**
* XML parser
*/
const dom = new DOMParser()
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Fetch given URL as JSON
* Fetch the given URL
*
* @param url - Request URL
* @param options - Request options
*
* @returns Response observable
*/
export function request(
url: string, options: RequestInit = { credentials: "same-origin" }
): Observable<Response> {
return from(fetch(url, options))
.pipe(
filter(res => res.status === 200),
)
}
/**
* Fetch JSON from the given URL
*
* @template T - Data type
*
@@ -51,32 +59,31 @@ const dom = new DOMParser()
*
* @returns Data observable
*/
export function fetchJSON<T>(
url: string, options: RequestInit = { credentials: "same-origin" }
export function requestJSON<T>(
url: string, options?: RequestInit
): Observable<T> {
return from(fetch(url, options))
return request(url, options)
.pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
shareReplay(1)
)
}
/**
* Fetch given URL as XML
* Fetch XML from the given URL
*
* @param url - Request URL
* @param options - Request options
*
* @returns Data observable
*/
export function fetchXML(
url: string, options: RequestInit = { credentials: "same-origin" }
export function requestXML(
url: string, options?: RequestInit
): Observable<Document> {
return from(fetch(url, options))
const dom = new DOMParser()
return request(url, options)
.pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
switchMap(res => res.text()),
map(res => dom.parseFromString(res, "text/xml")),
shareReplay(1)
)