Skip to content

Commit

Permalink
refactor: prevent side-effects
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadhonarvar committed Oct 11, 2024
1 parent 092d448 commit 01e00e1
Show file tree
Hide file tree
Showing 18 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/async-queue/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {newFlatomise} from '@alwatr/flatomise';
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* A queue that executes async tasks in order like mutex and semaphore methodology
Expand Down
3 changes: 3 additions & 0 deletions packages/dedupe/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {getGlobalThis} from '@alwatr/global-this';
import {packageTracer} from '@alwatr/package-tracer';
import '@alwatr/polyfill-has-own';

__dev_mode__: packageTracer.add(__package_name__, __package_version__);

const globalThis_ = getGlobalThis<{__alwatr_dedupe__: string | true}>();

if (typeof globalThis_.__alwatr_dedupe__ === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion packages/deep-clone/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* Clone deeply nested objects and arrays.
Expand Down
2 changes: 1 addition & 1 deletion packages/delay/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {packageTracer} from '@alwatr/package-tracer';
import {parseDuration, type Duration} from '@alwatr/parse-duration';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

import {requestAnimationFrame, requestIdleCallback} from './polyfill.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/exit-hook/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {deduplicate} from '@alwatr/dedupe';
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

deduplicate({name: __package_name__});

Expand Down
16 changes: 9 additions & 7 deletions packages/fetch/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import {delay} from '@alwatr/delay';
import {globalScope} from '@alwatr/global-this';
import {getGlobalThis} from '@alwatr/global-this';
import {createLogger} from '@alwatr/logger';
import {packageTracer} from '@alwatr/package-tracer';
import {parseDuration} from '@alwatr/parse-duration';

import type {FetchOptions} from './type.js';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

export const logger_ = createLogger('@alwatr/fetch');
export const logger_ = /* #__PURE__ */ createLogger('@alwatr/fetch');

const globalThis_ = /* #__PURE__ */ getGlobalThis();

let cacheStorage_: Cache;
export const cacheSupported = Object.hasOwn(globalScope, 'caches');
export const cacheSupported = /* #__PURE__ */ Object.hasOwn(globalThis_, 'caches');

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

Expand Down Expand Up @@ -215,7 +217,7 @@ export async function handleRetryPattern_(options: Required<FetchOptions>): Prom
catch (err) {
logger_.accident('fetch', 'fetch_failed_retry', err);

if (globalScope.navigator?.onLine === false) {
if (globalThis_.navigator?.onLine === false) {
logger_.accident('handleRetryPattern_', 'offline', 'Skip retry because offline');
throw err;
}
Expand All @@ -232,7 +234,7 @@ export async function handleRetryPattern_(options: Required<FetchOptions>): Prom
*/
export function handleTimeout_(options: FetchOptions): Promise<Response> {
if (options.timeout === 0) {
return globalScope.fetch(options.url, options);
return globalThis_.fetch(options.url, options);
}
// else
logger_.logMethod?.('handleTimeout_');
Expand All @@ -257,7 +259,7 @@ export function handleTimeout_(options: FetchOptions): Promise<Response> {
// });
// });

globalScope
globalThis_
.fetch(options.url, options)
.then((response) => resolved(response))
.catch((reason) => reject(reason))
Expand Down
2 changes: 1 addition & 1 deletion packages/flat-string/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* This function simplifies the complex C structures that are part of a combined JavaScript string.
Expand Down
2 changes: 1 addition & 1 deletion packages/flatomise/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* Flat promise that can be resolved or rejected from outside.
Expand Down
2 changes: 1 addition & 1 deletion packages/is-number/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123
Expand Down
2 changes: 1 addition & 1 deletion packages/local-storage/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* Parse json string without throwing error.
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {platformInfo} from '@alwatr/platform-info';

import type {AlwatrLogger} from './type.js';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

const defaultDebugMode =
platformInfo.development ||
Expand Down
6 changes: 3 additions & 3 deletions packages/node-fs/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {AsyncQueue} from '@alwatr/async-queue';
import {createLogger} from '@alwatr/logger';
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

export const logger = createLogger('@alwatr/node-fs');
export const logger = /* #__PURE__ */ createLogger('@alwatr/node-fs');

export const asyncQueue = new AsyncQueue();
export const asyncQueue = /* #__PURE__ */ new AsyncQueue();
2 changes: 2 additions & 0 deletions packages/package-tracer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ export const packageTracer = {
return this.list[packageName];
},
} as const;

__dev_mode__: packageTracer.add(__package_name__, __package_version__);
2 changes: 1 addition & 1 deletion packages/parse-duration/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber} from '@alwatr/is-number';
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* Unit conversion table
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-info/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/**
* Represents information about the current platform.
Expand Down
9 changes: 5 additions & 4 deletions packages/playground/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {createLogger} from '@alwatr/nanolib';
// import {exitHook} from '@alwatr/nanolib/node';
import {exitHook} from '@alwatr/nanolib/exit-hook';
import '@alwatr/nanolib/node-fs'

createLogger('playground');

// exitHook(() => {
// console.log('Exiting...');
// });
exitHook(() => {
console.log('Exiting...');
});
2 changes: 1 addition & 1 deletion packages/polyfill-has-own/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {packageTracer} from '@alwatr/package-tracer';

packageTracer.add(__package_name__, __package_version__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

/* eslint-disable no-prototype-builtins */

Expand Down
4 changes: 2 additions & 2 deletions packages/redner-state/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {createLogger} from '@alwatr/logger';
import {packageTracer} from '@alwatr/package-tracer';

const logger = createLogger(__package_name__);
__dev_mode__: packageTracer.add(__package_name__, __package_version__);

packageTracer.add(__package_name__, __package_version__);
const logger = /* #__PURE__ */ createLogger(__package_name__);

export const renderState = <R, T extends string>(
state: T,
Expand Down

0 comments on commit 01e00e1

Please sign in to comment.