muya 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +1 -1
- package/esm/utils/__tests__/is.test.js +1 -1
- package/esm/utils/common.js +1 -1
- package/esm/utils/is.js +1 -1
- package/package.json +1 -1
- package/src/utils/__tests__/is.test.ts +24 -1
- package/src/utils/common.ts +5 -6
- package/src/utils/is.ts +3 -3
- package/types/utils/common.d.ts +3 -3
- package/types/utils/is.d.ts +2 -1
package/cjs/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var g=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var L=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var H=(e,t)=>{for(var r in t)g(e,r,{get:t[r],enumerable:!0})},M=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of L(t))!z.call(e,o)&&o!==r&&g(e,o,{get:()=>t[o],enumerable:!(n=R(t,o))||n.enumerable});return e};var j=e=>M(g({},"__esModule",{value:!0}),e);var Y={};H(Y,{EMPTY_SELECTOR:()=>k,create:()=>q,select:()=>h,shallow:()=>A,useValue:()=>x});module.exports=j(Y);var k=e=>e;function p(e){return e instanceof Promise}function C(e){return typeof e=="function"}function V(e){return e instanceof Map}function P(e){return e instanceof Set}function O(e){return Array.isArray(e)}function d(e,t){return e===t?!0:!!Object.is(e,t)}function I(e){return typeof e=="function"}function v(e){return e instanceof T}function G(e){return e instanceof Error}function f(e){return e===void 0}var T=class extends Error{static Error="AbortError"};function N(e,t){t&&t.abort();let r=new AbortController,{signal:n}=r;return{promise:new Promise((i,s)=>{n.addEventListener("abort",()=>{s(new T)}),e.then(i).catch(s)}),controller:r}}function b(e,t=d){if(!f(e.current)){if(!f(e.previous)&&t(e.current,e.previous))return!1;e.previous=e.current}return!0}function m(e,t,r){if(!p(r))return r;e.abortController&&e.abortController.abort();let{promise:n,controller:o}=N(r,e.abortController);return e.abortController=o,n.then(i=>{e.current=i,t()}).catch(i=>{v(i)||(e.current=i,t())})}function D(){let e=new Map,t=new Set,r=performance.now(),n=!1;function o(){let s=performance.now(),a=s-r,{size:u}=t;if(a<.2&&u>0&&u<10){r=s,i();return}n||(n=!0,Promise.resolve().then(()=>{n=!1,r=performance.now(),i()}))}function i(){if(t.size===0)return;let s=new Set;for(let a of t){if(e.has(a.id)){s.add(a.id);let{onResolveItem:u}=e.get(a.id);u&&u(a.value)}t.delete(a)}if(t.size>0){o();return}for(let a of s)e.get(a)?.onFinish()}return{add(s,a){return e.set(s,a),()=>{e.delete(s)}},schedule(s,a){t.add({value:a,id:s}),o()}}}function h(e,t,r){let n={};function o(){let c=e.map(l=>l.get());return t(...c)}function i(){if(f(n.current)){let c=o();n.current=m(n,a.emitter.emit,c)}return n.current}let s=[];for(let c of e){let l=c.emitter.subscribe(()=>{S.schedule(a.id,null)});s.push(l)}let a=y({destroy(){for(let c of s)c();u(),a.emitter.clear(),n.current=void 0},get:i}),u=S.add(a.id,{onFinish(){let c=o();n.current=m(n,a.emitter.emit,c),b(n,r)&&a.emitter.emit()}});return a}var w=require("react");function x(e,t=k){let{emitter:r}=e,n=(0,w.useSyncExternalStore)(e.emitter.subscribe,()=>t(r.getSnapshot()),()=>t(r.getInitialSnapshot?r.getInitialSnapshot():r.getSnapshot()));if((0,w.useDebugValue)(n),p(n)||G(n))throw n;return n}function U(e,t){let r=new Set,n=[];return{clear:()=>{for(let o of n)o();r.clear()},subscribe:o=>(r.add(o),()=>{r.delete(o)}),emit:(...o)=>{for(let i of r)i(...o)},contains:o=>r.has(o),getSnapshot:e,getInitialSnapshot:t,getSize:()=>r.size,subscribeToOtherEmitter(o){let i=o.subscribe(()=>{this.emit()});n.push(i)}}}var _=0;function K(){return _++}function y(e){let{get:t,destroy:r,set:n}=e,o=!!n,i=function(s){return x(i,s)};return i.isSet=o,i.id=K(),i.emitter=U(t),i.destroy=r,i.listen=function(s){return this.emitter.subscribe(()=>{s(t())})},i.withName=function(s){return this.stateName=s,this},i.select=function(s,a=d){return h([i],s,a)},i.get=t,i.set=n,i}var S=D();function q(e,t=d){let r={};function n(){try{if(f(r.current)){let u=C(e)?e():e,c=m(r,s.emitter.emit,u);return r.current=c,r.current}return r.current}catch(u){r.current=u}return r.current}async function o(u,c){await u;let l=c(r.current),E=m(r,s.emitter.emit,l);r.current=E}function i(u){let c=n(),l=I(u);if(l&&p(c)){o(c,u);return}r.abortController&&r.abortController.abort();let E=l?u(c):u,F=m(r,s.emitter.emit,E);r.current=F}let s=y({get:n,destroy(){n(),a(),s.emitter.clear(),r.current=void 0},set(u){S.schedule(s.id,u)}}),a=S.add(s.id,{onFinish(){r.current=n(),b(r,t)&&s.emitter.emit()},onResolveItem:i});return C(e)||n(),s}function A(e,t){if(e==t)return!0;if(typeof e!="object"||e==null||typeof t!="object"||t==null)return!1;if(V(e)&&V(t)){if(e.size!==t.size)return!1;for(let[o,i]of e)if(!Object.is(i,t.get(o)))return!1;return!0}if(P(e)&&P(t)){if(e.size!==t.size)return!1;for(let o of e)if(!t.has(o))return!1;return!0}if(O(e)&&O(t)){if(e.length!==t.length)return!1;for(let[o,i]of e.entries())if(!Object.is(i,t[o]))return!1;return!0}let r=Object.keys(e),n=Object.keys(t);if(r.length!==n.length)return!1;for(let o of r)if(!Object.prototype.hasOwnProperty.call(t,o)||!Object.is(e[o],t[o]))return!1;return!0}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{create as r}from"../../create";import{isPromise as o,isFunction as s,isSetValueFunction as u,isMap as
|
|
1
|
+
import{create as r}from"../../create";import{AbortError as B}from"../common";import{isPromise as o,isFunction as s,isSetValueFunction as u,isMap as n,isSet as i,isArray as a,isEqualBase as f,isUndefined as l,isState as t,isAbortError as c}from"../is";describe("isPromise",()=>{it("should return true for a Promise",()=>{expect(o(Promise.resolve())).toBe(!0)}),it("should return false for a non-Promise",()=>{expect(o(123)).toBe(!1)})}),describe("isFunction",()=>{it("should return true for a function",()=>{expect(s(()=>{})).toBe(!0)}),it("should return false for a non-function",()=>{expect(s(123)).toBe(!1)})}),describe("isSetValueFunction",()=>{it("should return true for a function",()=>{expect(u(()=>{})).toBe(!0)}),it("should return false for a non-function",()=>{expect(u(123)).toBe(!1)})}),describe("isMap",()=>{it("should return true for a Map",()=>{expect(n(new Map)).toBe(!0)}),it("should return false for a non-Map",()=>{expect(n(123)).toBe(!1)})}),describe("isSet",()=>{it("should return true for a Set",()=>{expect(i(new Set)).toBe(!0)}),it("should return false for a non-Set",()=>{expect(i(123)).toBe(!1)})}),describe("isArray",()=>{it("should return true for an array",()=>{expect(a([])).toBe(!0)}),it("should return false for a non-array",()=>{expect(a(123)).toBe(!1)})}),describe("isEqualBase",()=>{it("should return true for equal values",()=>{expect(f(1,1)).toBe(!0)}),it("should return false for non-equal values",()=>{expect(f(1,2)).toBe(!1)})}),describe("isUndefined",()=>{it("should return true for undefined",()=>{expect(l(void 0)).toBe(!0)}),it("should return false for a non-undefined",()=>{expect(l(123)).toBe(!1)})}),describe("isState",()=>{it("should return true for a State real",()=>{const e=r(1);expect(t(e)).toBe(!0)}),it("should return true for a State with derived",()=>{const d=r(1).select(p=>p);expect(t(d)).toBe(!1)}),it("should return false for a non-State",()=>{expect(t(123)).toBe(!1)})}),describe("isAbortError",()=>{it("should return true for an AbortError",()=>{const e=new B;expect(c(e)).toBe(!0)}),it("should return false for a non-AbortError",()=>{const e=new Error("asd");expect(c(e)).toBe(!1)})});
|
package/esm/utils/common.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isAbortError as a,isEqualBase as b,isPromise as u,isUndefined as
|
|
1
|
+
import{isAbortError as a,isEqualBase as b,isPromise as u,isUndefined as i}from"./is";class T extends Error{static Error="AbortError"}function p(r,o){o&&o.abort();const t=new AbortController,{signal:n}=t;return{promise:new Promise((e,s)=>{n.addEventListener("abort",()=>{s(new T)}),r.then(e).catch(s)}),controller:t}}function f(r,o=b){if(!i(r.current)){if(!i(r.previous)&&o(r.current,r.previous))return!1;r.previous=r.current}return!0}function m(r,o,t){if(!u(t))return t;r.abortController&&r.abortController.abort();const{promise:n,controller:l}=p(t,r.abortController);return r.abortController=l,n.then(e=>{r.current=e,o()}).catch(e=>{a(e)||(r.current=e,o())})}export{T as AbortError,f as canUpdate,m as handleAsyncUpdate};
|
package/esm/utils/is.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{AbortError as r}from"./common";function i(n){return n instanceof Promise}function e(n){return typeof n=="function"}function u(n){return n instanceof Map}function s(n){return n instanceof Set}function a(n){return Array.isArray(n)}function f(n,t){return n===t?!0:!!Object.is(n,t)}function c(n){return typeof n=="function"}function p(n){return n instanceof r}function k(n){return n instanceof Error}function w(n){return n===void 0}function S(n){return e(n)&&"get"in n&&"set"in n&&"isSet"in n&&n.isSet===!0}export{p as isAbortError,a as isArray,f as isEqualBase,k as isError,e as isFunction,u as isMap,i as isPromise,s as isSet,c as isSetValueFunction,S as isState,w as isUndefined};
|
package/package.json
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { create } from '../../create'
|
|
2
|
-
import {
|
|
2
|
+
import { AbortError } from '../common'
|
|
3
|
+
import {
|
|
4
|
+
isPromise,
|
|
5
|
+
isFunction,
|
|
6
|
+
isSetValueFunction,
|
|
7
|
+
isMap,
|
|
8
|
+
isSet,
|
|
9
|
+
isArray,
|
|
10
|
+
isEqualBase,
|
|
11
|
+
isUndefined,
|
|
12
|
+
isState,
|
|
13
|
+
isAbortError,
|
|
14
|
+
} from '../is'
|
|
3
15
|
|
|
4
16
|
describe('isPromise', () => {
|
|
5
17
|
it('should return true for a Promise', () => {
|
|
@@ -89,3 +101,14 @@ describe('isState', () => {
|
|
|
89
101
|
expect(isState(123)).toBe(false)
|
|
90
102
|
})
|
|
91
103
|
})
|
|
104
|
+
|
|
105
|
+
describe('isAbortError', () => {
|
|
106
|
+
it('should return true for an AbortError', () => {
|
|
107
|
+
const error = new AbortError()
|
|
108
|
+
expect(isAbortError(error)).toBe(true)
|
|
109
|
+
})
|
|
110
|
+
it('should return false for a non-AbortError', () => {
|
|
111
|
+
const error = new Error('asd')
|
|
112
|
+
expect(isAbortError(error)).toBe(false)
|
|
113
|
+
})
|
|
114
|
+
})
|
package/src/utils/common.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import type { Cache, IsEqual } from '../types'
|
|
2
2
|
import { isAbortError, isEqualBase, isPromise, isUndefined } from './is'
|
|
3
3
|
|
|
4
|
-
// eslint-disable-next-line no-shadow
|
|
5
|
-
export enum Abort {
|
|
6
|
-
Error = 'StateAbortError',
|
|
7
|
-
}
|
|
8
|
-
|
|
9
4
|
export interface CancelablePromise<T> {
|
|
10
5
|
promise: Promise<T>
|
|
11
6
|
controller?: AbortController
|
|
12
7
|
}
|
|
8
|
+
|
|
9
|
+
export class AbortError extends Error {
|
|
10
|
+
static readonly Error = 'AbortError'
|
|
11
|
+
}
|
|
13
12
|
/**
|
|
14
13
|
* Cancelable promise function, return promise and controller
|
|
15
14
|
*/
|
|
@@ -23,7 +22,7 @@ function cancelablePromise<T>(promise: Promise<T>, previousController?: AbortCon
|
|
|
23
22
|
const cancelable = new Promise<T>((resolve, reject) => {
|
|
24
23
|
// Listen for the abort event
|
|
25
24
|
signal.addEventListener('abort', () => {
|
|
26
|
-
reject(new
|
|
25
|
+
reject(new AbortError())
|
|
27
26
|
})
|
|
28
27
|
// When the original promise settles, resolve or reject accordingly
|
|
29
28
|
promise.then(resolve).catch(reject)
|
package/src/utils/is.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SetStateCb, SetValue, State } from '../types'
|
|
2
|
-
import {
|
|
2
|
+
import { AbortError } from './common'
|
|
3
3
|
|
|
4
4
|
export function isPromise<T>(value: unknown): value is Promise<T> {
|
|
5
5
|
return value instanceof Promise
|
|
@@ -30,8 +30,8 @@ export function isEqualBase<T>(valueA: T, valueB: T): boolean {
|
|
|
30
30
|
export function isSetValueFunction<T>(value: SetValue<T>): value is SetStateCb<T> {
|
|
31
31
|
return typeof value === 'function'
|
|
32
32
|
}
|
|
33
|
-
export function isAbortError(value: unknown): value is
|
|
34
|
-
return value instanceof
|
|
33
|
+
export function isAbortError(value: unknown): value is AbortError {
|
|
34
|
+
return value instanceof AbortError
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
export function isError(value: unknown): value is Error {
|
package/types/utils/common.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { Cache, IsEqual } from '../types';
|
|
2
|
-
export declare enum Abort {
|
|
3
|
-
Error = "StateAbortError"
|
|
4
|
-
}
|
|
5
2
|
export interface CancelablePromise<T> {
|
|
6
3
|
promise: Promise<T>;
|
|
7
4
|
controller?: AbortController;
|
|
8
5
|
}
|
|
6
|
+
export declare class AbortError extends Error {
|
|
7
|
+
static readonly Error = "AbortError";
|
|
8
|
+
}
|
|
9
9
|
/**
|
|
10
10
|
* Check if the cache value is different from the previous value.
|
|
11
11
|
*/
|
package/types/utils/is.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SetStateCb, SetValue, State } from '../types';
|
|
2
|
+
import { AbortError } from './common';
|
|
2
3
|
export declare function isPromise<T>(value: unknown): value is Promise<T>;
|
|
3
4
|
export declare function isFunction<T extends (...args: unknown[]) => unknown>(value: unknown): value is T;
|
|
4
5
|
export declare function isMap(value: unknown): value is Map<unknown, unknown>;
|
|
@@ -6,7 +7,7 @@ export declare function isSet(value: unknown): value is Set<unknown>;
|
|
|
6
7
|
export declare function isArray(value: unknown): value is Array<unknown>;
|
|
7
8
|
export declare function isEqualBase<T>(valueA: T, valueB: T): boolean;
|
|
8
9
|
export declare function isSetValueFunction<T>(value: SetValue<T>): value is SetStateCb<T>;
|
|
9
|
-
export declare function isAbortError(value: unknown): value is
|
|
10
|
+
export declare function isAbortError(value: unknown): value is AbortError;
|
|
10
11
|
export declare function isError(value: unknown): value is Error;
|
|
11
12
|
export declare function isUndefined(value: unknown): value is undefined;
|
|
12
13
|
export declare function isState<T>(value: unknown): value is State<T>;
|