Skip to content

Commit

Permalink
refactor: cleanup multiple JS runtime support implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Sep 28, 2024
1 parent 62bf56a commit ca64e63
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Please consult our [general guidelines](#general-guidelines).
- Performance
[benchmarks](https://github.com/poolifier/poolifier-web-worker/blob/master/benchmarks/README.md)
- Zero cost abstraction for multiple JS runtime support
- Multiple JS runtime support at zero cost abstraction
- No runtime dependencies ✔
- Support for ESM and TypeScript ✔
- Support for
Expand Down
7 changes: 1 addition & 6 deletions src/pools/selection-strategies/selection-strategies-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
availableParallelism,
runtime,
unsupportedJsRuntime,
} from '../../utils.ts'
import { availableParallelism, runtime } from '../../utils.ts'
import type { IPool } from '../pool.ts'
import type { IWorker } from '../worker.ts'
import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy.ts'
Expand Down Expand Up @@ -67,7 +63,6 @@ const cpusCycleTimeWeight = (cpus: { speed: number }[]): number => {
const computedDefaultWorkerWeight: number =
await (async (): Promise<number> => {
return await {
unknown: unsupportedJsRuntime,
browser: () => {
const estCpuSpeed = estimatedCpuSpeed()
return cpusCycleTimeWeight(
Expand Down
17 changes: 3 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,6 @@ const isDeno: boolean = !!(globalThis as any).Deno
// deno-lint-ignore no-explicit-any
const isBrowser: boolean = !!(globalThis as any).navigator

/**
* Throws an error indicating that the current JavaScript runtime environment is unsupported.
*
* @internal
*/
export const unsupportedJsRuntime = () => {
throw new Error('Unsupported JavaScript runtime environment')
}

/**
* JavaScript runtime environments enumeration.
*
Expand All @@ -279,22 +270,21 @@ export enum JavaScriptRuntimes {
}

/**
* JavaScript runtime environments.
* JavaScript runtime environment.
*
* @internal
*/
export const runtime: JavaScriptRuntimes | 'unknown' = (() => {
export const runtime: JavaScriptRuntimes = (() => {
if (isBun) return JavaScriptRuntimes.bun
if (isDeno) return JavaScriptRuntimes.deno
if (isBrowser) return JavaScriptRuntimes.browser
return 'unknown'
throw new Error('Unsupported JavaScript runtime environment')
})()

const isMainThread: boolean | undefined = await (async (): Promise<
boolean | undefined
> => {
return await {
unknown: unsupportedJsRuntime,
browser: () => undefined,
deno: () => undefined,
bun: async () => {
Expand All @@ -312,7 +302,6 @@ const isMainThread: boolean | undefined = await (async (): Promise<
*/
export const environment: string = await (async (): Promise<string> => {
return await {
unknown: unsupportedJsRuntime,
browser: () => 'production',
deno: () => {
// deno-lint-ignore ban-ts-comment
Expand Down

0 comments on commit ca64e63

Please sign in to comment.