From dc91981a157a2ba9f3f51e804f5fa4b02ed155ef Mon Sep 17 00:00:00 2001 From: "samuel.gjabel" Date: Fri, 22 Nov 2024 18:30:45 +0700 Subject: [PATCH] fix: react native support - remove dom error --- package.json | 2 +- packages/core/utils/__tests__/is.test.ts | 25 +++++++++++++++++++++++- packages/core/utils/common.ts | 11 +++++------ packages/core/utils/is.ts | 6 +++--- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index d0422d2..dc9dc1e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "muya", - "version": "2.0.2", + "version": "2.0.3", "author": "samuel.gjabel@gmail.com", "repository": "https://github.com/samuelgjabel/muya", "main": "cjs/index.js", diff --git a/packages/core/utils/__tests__/is.test.ts b/packages/core/utils/__tests__/is.test.ts index b5e784b..766e823 100644 --- a/packages/core/utils/__tests__/is.test.ts +++ b/packages/core/utils/__tests__/is.test.ts @@ -1,5 +1,17 @@ import { create } from '../../create' -import { isPromise, isFunction, isSetValueFunction, isMap, isSet, isArray, isEqualBase, isUndefined, isState } from '../is' +import { AbortError } from '../common' +import { + isPromise, + isFunction, + isSetValueFunction, + isMap, + isSet, + isArray, + isEqualBase, + isUndefined, + isState, + isAbortError, +} from '../is' describe('isPromise', () => { it('should return true for a Promise', () => { @@ -89,3 +101,14 @@ describe('isState', () => { expect(isState(123)).toBe(false) }) }) + +describe('isAbortError', () => { + it('should return true for an AbortError', () => { + const error = new AbortError() + expect(isAbortError(error)).toBe(true) + }) + it('should return false for a non-AbortError', () => { + const error = new Error('asd') + expect(isAbortError(error)).toBe(false) + }) +}) diff --git a/packages/core/utils/common.ts b/packages/core/utils/common.ts index 76b298f..af00d06 100644 --- a/packages/core/utils/common.ts +++ b/packages/core/utils/common.ts @@ -1,15 +1,14 @@ import type { Cache, IsEqual } from '../types' import { isAbortError, isEqualBase, isPromise, isUndefined } from './is' -// eslint-disable-next-line no-shadow -export enum Abort { - Error = 'StateAbortError', -} - export interface CancelablePromise { promise: Promise controller?: AbortController } + +export class AbortError extends Error { + static readonly Error = 'AbortError' +} /** * Cancelable promise function, return promise and controller */ @@ -23,7 +22,7 @@ function cancelablePromise(promise: Promise, previousController?: AbortCon const cancelable = new Promise((resolve, reject) => { // Listen for the abort event signal.addEventListener('abort', () => { - reject(new DOMException('Promise was aborted', Abort.Error)) + reject(new AbortError()) }) // When the original promise settles, resolve or reject accordingly promise.then(resolve).catch(reject) diff --git a/packages/core/utils/is.ts b/packages/core/utils/is.ts index 7f28229..eb53e8c 100644 --- a/packages/core/utils/is.ts +++ b/packages/core/utils/is.ts @@ -1,5 +1,5 @@ import type { SetStateCb, SetValue, State } from '../types' -import { Abort } from './common' +import { AbortError } from './common' export function isPromise(value: unknown): value is Promise { return value instanceof Promise @@ -30,8 +30,8 @@ export function isEqualBase(valueA: T, valueB: T): boolean { export function isSetValueFunction(value: SetValue): value is SetStateCb { return typeof value === 'function' } -export function isAbortError(value: unknown): value is DOMException { - return value instanceof DOMException && value.name === Abort.Error +export function isAbortError(value: unknown): value is AbortError { + return value instanceof AbortError } export function isError(value: unknown): value is Error {