UNPKG

@krai-tech/web-api

Version:

A set of common tokens for consuming Web API with Angular.

159 lines (145 loc) 6.53 kB
import { DOCUMENT } from '@angular/common'; import { InjectionToken, inject } from '@angular/core'; import { Observable, share, fromEvent, startWith, map, distinctUntilChanged, shareReplay } from 'rxjs'; /** * Injection token for the global window object. * * This token provides an abstraction over the global window object, allowing it to be injected * into Angular services or components. It uses the Angular DOCUMENT token to access the defaultView, * which represents the window object. * * Throws an error if the window object is not available. */ const WINDOW = new InjectionToken('An abstraction over global window object', { factory: () => { const { defaultView } = inject(DOCUMENT); if (!defaultView) { throw new Error('Window is not available'); } return defaultView; }, }); /** * Injection token for the global localStorage object. * * This token provides an abstraction over the window.localStorage object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the localStorage * property of the global window object. */ const LOCAL_STORAGE = new InjectionToken('An abstraction over window.localStorage object', { factory: () => inject(WINDOW).localStorage, }); /** * Injection token for the global location object. * * This token provides an abstraction over the window.location object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the location * property of the global window object. */ const LOCATION = new InjectionToken('An abstraction over window.location object', { factory: () => inject(WINDOW).location, }); /** * Injection token for the global navigator object. * * This token provides an abstraction over the window.navigator object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the navigator * property of the global window object. */ const NAVIGATOR = new InjectionToken('An abstraction over window.navigator object', { factory: () => inject(WINDOW).navigator, }); /** * Injection token for the global userAgent string. * * This token provides an abstraction over the window.navigator.userAgent property, allowing it to be injected * into Angular services or components. It utilizes the NAVIGATOR injection token to access the userAgent * property of the global navigator object. */ const USER_AGENT = new InjectionToken('An abstraction over window.navigator.userAgent object', { factory: () => inject(NAVIGATOR).userAgent, }); /** * Injection token for the global sessionStorage object. * * This token provides an abstraction over the window.sessionStorage object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the sessionStorage * property of the global window object. */ const SESSION_STORAGE = new InjectionToken('An abstraction over window.sessionStorage object', { factory: () => inject(WINDOW).sessionStorage, }); /** * Injection token for the global history object. * * This token provides an abstraction over the window.history object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the history * property of the global window object. */ const HISTORY = new InjectionToken('An abstraction over window.history object', { factory: () => inject(WINDOW).history, }); /** * Injection token for an Observable based on `window.requestAnimationFrame`. * * This token provides an Observable that emits timestamps on each animation frame. * It utilizes the WINDOW injection token to access the requestAnimationFrame and cancelAnimationFrame * methods of the global window object. The Observable is shared to ensure multiple subscribers * receive the same animation frame updates. */ const ANIMATION_FRAME = new InjectionToken('Shared Observable based on `window.requestAnimationFrame`', { factory: () => { const { requestAnimationFrame, cancelAnimationFrame } = inject(WINDOW); const animationFrame$ = new Observable(subscriber => { let id = NaN; const callback = (timestamp) => { subscriber.next(timestamp); id = requestAnimationFrame(callback); }; id = requestAnimationFrame(callback); return () => { cancelAnimationFrame(id); }; }); return animationFrame$.pipe(share()); }, }); /** * Injection token for an Observable that emits the visibility state of the document. * * This token provides an Observable that emits a boolean value indicating whether the document * is visible or hidden. It uses the `visibilitychange` event of the document to detect changes * in visibility state. The Observable is shared and replayed, ensuring that subscribers receive * the latest visibility state. */ const PAGE_VISIBILITY = new InjectionToken('Shared Observable based on `document visibility changed`', { factory: () => { const documentRef = inject(DOCUMENT); return fromEvent(documentRef, 'visibilitychange').pipe(startWith(0), map(() => documentRef.visibilityState !== 'hidden'), distinctUntilChanged(), shareReplay({ refCount: false, bufferSize: 1 })); }, }); /** * Injection token for the global Crypto object. * * This token provides an abstraction over the window.crypto object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the crypto * property of the global window object. */ const CRYPTO = new InjectionToken('An abstraction over window.crypto object', { factory: () => inject(WINDOW).crypto, }); /** * Injection token for the global Screen object. * * This token provides an abstraction over the window.screen object, allowing it to be injected * into Angular services or components. It utilizes the WINDOW injection token to access the screen * property of the global window object. */ const SCREEN = new InjectionToken('An abstraction over window.screen object', { factory: () => inject(WINDOW).screen, }); /** * Generated bundle index. Do not edit. */ export { ANIMATION_FRAME, CRYPTO, HISTORY, LOCAL_STORAGE, LOCATION, NAVIGATOR, PAGE_VISIBILITY, SCREEN, SESSION_STORAGE, USER_AGENT, WINDOW }; //# sourceMappingURL=krai-tech-web-api.mjs.map