Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

feat: use globalScope instead globalThis #104

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions packages/fetch/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createLogger, definePackage, NODE_MODE} from '@alwatr/logger';
import {createLogger, definePackage, NODE_MODE, globalScope} from '@alwatr/logger';
import {getClientId, delay} from '@alwatr/util';

import type {FetchOptions} from './type.js';
Expand All @@ -10,9 +10,8 @@ const logger = createLogger('alwatr/fetch');

definePackage('fetch', '2.x');


let alwatrCacheStorage: Cache;
const cacheSupported = 'caches' in globalThis;
const cacheSupported = 'caches' in globalScope;

const duplicateRequestStorage: Record<string, Promise<Response>> = {};

Expand Down Expand Up @@ -191,11 +190,7 @@ async function _handleCacheStrategy(options: Required<FetchOptions>): Promise<Re
case 'cache_only': {
const cachedResponse = await cacheStorage.match(request);
if (cachedResponse == null) {
logger.accident(
'_handleCacheStrategy',
'fetch_cache_not_found',
{url: request.url},
);
logger.accident('_handleCacheStrategy', 'fetch_cache_not_found', {url: request.url});
throw new Error('fetch_cache_not_found');
}
// else
Expand Down Expand Up @@ -304,7 +299,7 @@ async function _handleRetryPattern(options: Required<FetchOptions>): Promise<Res
catch (err) {
logger.accident('fetch', 'fetch_failed_retry', err);

if (globalThis.navigator?.onLine === false) {
if (globalScope.navigator?.onLine === false) {
throw new Error('offline');
}

Expand All @@ -320,7 +315,7 @@ async function _handleRetryPattern(options: Required<FetchOptions>): Promise<Res
*/
function _handleTimeout(options: FetchOptions): Promise<Response> {
if (options.timeout === 0) {
return globalThis.fetch(options.url, options);
return globalScope.fetch(options.url, options);
}
// else
logger.logMethod?.('_handleTimeout');
Expand All @@ -346,7 +341,7 @@ function _handleTimeout(options: FetchOptions): Promise<Response> {
// });
// });

globalThis
globalScope
.fetch(options.url, options)
.then((response) => resolved(response))
.catch((reason) => reject(reason))
Expand Down
8 changes: 4 additions & 4 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './global.js'
export * from './logger.js'
export * from './define-package.js'
export type * from './type.js'
export * from './global.js';
export * from './logger.js';
export * from './define-package.js';
export type * from './type.js';
8 changes: 4 additions & 4 deletions packages/math/src/math.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {definePackage} from '@alwatr/logger';
import {definePackage, globalScope} from '@alwatr/logger';

import type {TransformRangeOptions} from './type.js';
export {UnicodeDigits, type UnicodeLangKeys} from './unicode-digits.js';
Expand Down Expand Up @@ -158,7 +158,7 @@ export const random = {
shuffle: <T>(array: T[]): T[] => array.sort(() => random.number - 0.5),

getRandomValues: <T extends ArrayBufferView | null>(array: T): T => {
return globalThis.crypto.getRandomValues(array);
return globalScope.crypto.getRandomValues(array);
// TODO: check msCrypto
},

Expand All @@ -172,8 +172,8 @@ export const random = {
* ```
*/
get uuid(): `${string}-${string}-${string}-${string}-${string}` {
if (globalThis.crypto?.randomUUID) {
return globalThis.crypto.randomUUID() as `${string}-${string}-${string}-${string}-${string}`;
if (globalScope.crypto?.randomUUID) {
return globalScope.crypto.randomUUID() as `${string}-${string}-${string}-${string}-${string}`;
}
// else
const bytes = random.getRandomValues(new Uint8Array(16));
Expand Down
8 changes: 5 additions & 3 deletions packages/util/src/polyfill.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {globalScope} from '@alwatr/logger';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type IndexableWindow = Record<string, any>;

export const win = globalThis as IndexableWindow;
export const win = globalScope as IndexableWindow;

const requestAnimationFrameFallback = (callback: FrameRequestCallback): ReturnType<typeof setTimeout> =>
setTimeout(() => callback(Date.now()), 1000 / 60);

export const requestAnimationFrame: typeof globalThis.requestAnimationFrame =
export const requestAnimationFrame: typeof globalScope.requestAnimationFrame =
win.requestAnimationFrame ||
win.webkitRequestAnimationFrame ||
win.mozRequestAnimationFrame ||
Expand All @@ -17,5 +19,5 @@ const requestIdleCallbackFallback = (
options?: IdleRequestOptions,
): ReturnType<typeof setTimeout> => setTimeout(callback, options?.timeout ?? 2000);

export const requestIdleCallback: typeof globalThis.requestIdleCallback =
export const requestIdleCallback: typeof globalScope.requestIdleCallback =
win.requestIdleCallback || win.webkitRequestIdleCallback || win.mozRequestIdleCallback || requestIdleCallbackFallback;