Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: react native support - remove dom error #4

Merged
merged 1 commit into from
Nov 22, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "muya",
"version": "2.0.2",
"version": "2.0.3",
"author": "[email protected]",
"repository": "https://github.com/samuelgjabel/muya",
"main": "cjs/index.js",
Expand Down
25 changes: 24 additions & 1 deletion packages/core/utils/__tests__/is.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
11 changes: 5 additions & 6 deletions packages/core/utils/common.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
promise: Promise<T>
controller?: AbortController
}

export class AbortError extends Error {
static readonly Error = 'AbortError'
}
/**
* Cancelable promise function, return promise and controller
*/
Expand All @@ -23,7 +22,7 @@ function cancelablePromise<T>(promise: Promise<T>, previousController?: AbortCon
const cancelable = new Promise<T>((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)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/utils/is.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SetStateCb, SetValue, State } from '../types'
import { Abort } from './common'
import { AbortError } from './common'

export function isPromise<T>(value: unknown): value is Promise<T> {
return value instanceof Promise
Expand Down Expand Up @@ -30,8 +30,8 @@ export function isEqualBase<T>(valueA: T, valueB: T): boolean {
export function isSetValueFunction<T>(value: SetValue<T>): value is SetStateCb<T> {
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 {
Expand Down