muya 1.1.0 → 2.0.0-beta.2
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/README.md +202 -139
- package/cjs/index.js +1 -1
- package/esm/__tests__/create-async.test.js +1 -0
- package/esm/__tests__/test-utils.js +1 -0
- package/esm/create.js +1 -1
- package/esm/debug/development-tools.js +1 -0
- package/esm/index.js +1 -1
- package/esm/subscriber.js +1 -0
- package/esm/types.js +1 -1
- package/esm/use.js +1 -0
- package/esm/utils/__tests__/context.test.js +1 -0
- package/esm/utils/__tests__/is.test.js +1 -0
- package/esm/utils/__tests__/shallow.test.js +1 -0
- package/esm/utils/__tests__/sub-memo.test.js +1 -0
- package/esm/utils/common.js +1 -0
- package/esm/utils/create-context.js +1 -0
- package/esm/utils/create-emitter.js +1 -0
- package/esm/utils/global-scheduler.js +1 -0
- package/esm/utils/is.js +1 -0
- package/esm/utils/scheduler.js +1 -0
- package/esm/utils/shallow.js +1 -0
- package/esm/utils/sub-memo.js +1 -0
- package/package.json +1 -1
- package/packages/core/__tests__/bench.test.tsx +266 -0
- package/packages/core/__tests__/create-async.test.ts +88 -0
- package/packages/core/__tests__/create.test.tsx +107 -0
- package/packages/core/__tests__/subscriber.test.tsx +89 -0
- package/packages/core/__tests__/test-utils.ts +40 -0
- package/packages/core/__tests__/use-async.test.tsx +45 -0
- package/packages/core/__tests__/use.test.tsx +125 -0
- package/packages/core/create.ts +98 -0
- package/packages/core/debug/development-tools.ts +37 -0
- package/packages/core/index.ts +4 -0
- package/packages/core/subscriber.ts +165 -0
- package/packages/core/types.ts +15 -0
- package/packages/core/use.ts +57 -0
- package/packages/core/utils/__tests__/context.test.ts +198 -0
- package/packages/core/utils/__tests__/is.test.ts +74 -0
- package/packages/core/utils/__tests__/shallow.test.ts +418 -0
- package/packages/core/utils/__tests__/sub-memo.test.ts +13 -0
- package/packages/core/utils/common.ts +48 -0
- package/packages/core/utils/create-context.ts +60 -0
- package/packages/core/utils/create-emitter.ts +55 -0
- package/packages/core/utils/global-scheduler.ts +75 -0
- package/packages/core/utils/is.ts +50 -0
- package/packages/core/utils/scheduler.ts +59 -0
- package/{src → packages/core/utils}/shallow.ts +3 -6
- package/packages/core/utils/sub-memo.ts +49 -0
- package/types/__tests__/test-utils.d.ts +20 -0
- package/types/create.d.ts +21 -21
- package/types/debug/development-tools.d.ts +10 -0
- package/types/index.d.ts +2 -4
- package/types/subscriber.d.ts +25 -0
- package/types/types.d.ts +9 -65
- package/types/use.d.ts +2 -0
- package/types/utils/common.d.ts +15 -0
- package/types/utils/create-context.d.ts +5 -0
- package/types/utils/create-emitter.d.ts +20 -0
- package/types/utils/global-scheduler.d.ts +5 -0
- package/types/utils/is.d.ts +13 -0
- package/types/utils/scheduler.d.ts +8 -0
- package/types/utils/sub-memo.d.ts +7 -0
- package/esm/common.js +0 -1
- package/esm/create-base-state.js +0 -1
- package/esm/create-emitter.js +0 -1
- package/esm/create-getter-state.js +0 -1
- package/esm/is.js +0 -1
- package/esm/merge.js +0 -1
- package/esm/select.js +0 -1
- package/esm/shallow.js +0 -1
- package/esm/use-state-value.js +0 -1
- package/src/common.ts +0 -28
- package/src/create-base-state.ts +0 -35
- package/src/create-emitter.ts +0 -24
- package/src/create-getter-state.ts +0 -19
- package/src/create.ts +0 -102
- package/src/index.ts +0 -6
- package/src/is.ts +0 -36
- package/src/merge.ts +0 -41
- package/src/select.ts +0 -33
- package/src/state.test.tsx +0 -647
- package/src/types.ts +0 -94
- package/src/use-state-value.ts +0 -29
- package/types/common.d.ts +0 -7
- package/types/create-base-state.d.ts +0 -10
- package/types/create-emitter.d.ts +0 -7
- package/types/create-getter-state.d.ts +0 -6
- package/types/is.d.ts +0 -10
- package/types/merge.d.ts +0 -2
- package/types/select.d.ts +0 -2
- package/types/use-state-value.d.ts +0 -10
- /package/types/{shallow.d.ts → utils/shallow.d.ts} +0 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { SchedulerOptions } from './scheduler'
|
|
2
|
+
import { RESCHEDULE_COUNT, THRESHOLD, THRESHOLD_ITEMS } from './scheduler'
|
|
3
|
+
|
|
4
|
+
interface GlobalSchedulerItem<T> {
|
|
5
|
+
value: T
|
|
6
|
+
id: number
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function createGlobalScheduler() {
|
|
10
|
+
const listeners = new Map<number, SchedulerOptions<unknown>>()
|
|
11
|
+
const batches = new Set<GlobalSchedulerItem<unknown>>()
|
|
12
|
+
|
|
13
|
+
let frame = performance.now()
|
|
14
|
+
let scheduled = false
|
|
15
|
+
|
|
16
|
+
function schedule() {
|
|
17
|
+
const startFrame = performance.now()
|
|
18
|
+
const frameSizeDiffIn = startFrame - frame
|
|
19
|
+
const { size } = batches
|
|
20
|
+
if (frameSizeDiffIn < THRESHOLD && size > 0 && size < THRESHOLD_ITEMS) {
|
|
21
|
+
frame = startFrame
|
|
22
|
+
flush()
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!scheduled) {
|
|
27
|
+
scheduled = true
|
|
28
|
+
Promise.resolve().then(() => {
|
|
29
|
+
scheduled = false
|
|
30
|
+
frame = performance.now()
|
|
31
|
+
flush()
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function flush() {
|
|
37
|
+
if (batches.size === 0) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const effectedListeners = new Set<number>()
|
|
42
|
+
for (const value of batches) {
|
|
43
|
+
if (listeners.has(value.id)) {
|
|
44
|
+
effectedListeners.add(value.id)
|
|
45
|
+
const { onResolveItem } = listeners.get(value.id)!
|
|
46
|
+
if (onResolveItem) {
|
|
47
|
+
onResolveItem(value.value)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
batches.delete(value)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (batches.size > RESCHEDULE_COUNT) {
|
|
54
|
+
schedule()
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (const id of effectedListeners) {
|
|
59
|
+
listeners.get(id)?.onFinish()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
add<T>(id: number, option: SchedulerOptions<T>) {
|
|
65
|
+
listeners.set(id, option as SchedulerOptions<unknown>)
|
|
66
|
+
return () => {
|
|
67
|
+
listeners.delete(id)
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
schedule<T>(id: number, value: T) {
|
|
71
|
+
batches.add({ value, id })
|
|
72
|
+
schedule()
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Abort } from './common'
|
|
2
|
+
import type { SetStateCb, SetValue } from '../types'
|
|
3
|
+
import type { State } from '../create'
|
|
4
|
+
|
|
5
|
+
export function isPromise<T>(value: unknown): value is Promise<T> {
|
|
6
|
+
return value instanceof Promise
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function isFunction<T extends (...args: unknown[]) => unknown>(value: unknown): value is T {
|
|
10
|
+
return typeof value === 'function'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isMap(value: unknown): value is Map<unknown, unknown> {
|
|
14
|
+
return value instanceof Map
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isSet(value: unknown): value is Set<unknown> {
|
|
18
|
+
return value instanceof Set
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isArray(value: unknown): value is Array<unknown> {
|
|
22
|
+
return Array.isArray(value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function isEqualBase<T>(valueA: T, valueB: T): boolean {
|
|
26
|
+
if (valueA === valueB) {
|
|
27
|
+
return true
|
|
28
|
+
}
|
|
29
|
+
return !!Object.is(valueA, valueB)
|
|
30
|
+
}
|
|
31
|
+
export function isSetValueFunction<T>(value: SetValue<T>): value is SetStateCb<T> {
|
|
32
|
+
return typeof value === 'function'
|
|
33
|
+
}
|
|
34
|
+
export function isAbortError(value: unknown): value is DOMException {
|
|
35
|
+
return value instanceof DOMException && value.name === Abort.Error
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isAnyOtherError(value: unknown): value is Error {
|
|
39
|
+
return value instanceof Error && value.name !== Abort.Error
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function isUndefined(value: unknown): value is undefined {
|
|
43
|
+
return value === undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function isCreate(value: unknown): value is State<unknown> {
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
48
|
+
// @ts-expect-error
|
|
49
|
+
return isFunction(value) && value.set !== undefined
|
|
50
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export const THRESHOLD = 0.2
|
|
2
|
+
export const THRESHOLD_ITEMS = 10
|
|
3
|
+
export const RESCHEDULE_COUNT = 0
|
|
4
|
+
|
|
5
|
+
export interface SchedulerOptions<T> {
|
|
6
|
+
readonly onResolveItem?: (item: T) => void
|
|
7
|
+
readonly onFinish: () => void
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function createScheduler<T>(options: SchedulerOptions<T>) {
|
|
11
|
+
const batches = new Set<T>()
|
|
12
|
+
const { onResolveItem, onFinish } = options
|
|
13
|
+
let frame = performance.now()
|
|
14
|
+
let scheduled = false
|
|
15
|
+
|
|
16
|
+
function schedule() {
|
|
17
|
+
const startFrame = performance.now()
|
|
18
|
+
const frameSizeDiffIn = startFrame - frame
|
|
19
|
+
const { size } = batches
|
|
20
|
+
if (frameSizeDiffIn < THRESHOLD && size > 0 && size < THRESHOLD_ITEMS) {
|
|
21
|
+
frame = startFrame
|
|
22
|
+
flush()
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!scheduled) {
|
|
27
|
+
scheduled = true
|
|
28
|
+
Promise.resolve().then(() => {
|
|
29
|
+
scheduled = false
|
|
30
|
+
frame = performance.now()
|
|
31
|
+
flush()
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function flush() {
|
|
37
|
+
if (batches.size === 0) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
for (const value of batches) {
|
|
41
|
+
if (onResolveItem) {
|
|
42
|
+
onResolveItem(value)
|
|
43
|
+
}
|
|
44
|
+
batches.delete(value)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (batches.size > RESCHEDULE_COUNT) {
|
|
48
|
+
schedule()
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
onFinish()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function addValue(value: T) {
|
|
55
|
+
batches.add(value)
|
|
56
|
+
schedule()
|
|
57
|
+
}
|
|
58
|
+
return addValue
|
|
59
|
+
}
|
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
/* eslint-disable sonarjs/cognitive-complexity */
|
|
1
2
|
import { isArray, isMap, isSet } from './is'
|
|
2
3
|
|
|
3
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
4
4
|
export function shallow<T>(valueA: T, valueB: T): boolean {
|
|
5
5
|
if (valueA == valueB) {
|
|
6
6
|
return true
|
|
7
7
|
}
|
|
8
|
-
if (Object.is(valueA, valueB)) {
|
|
9
|
-
return true
|
|
10
|
-
}
|
|
11
8
|
|
|
12
9
|
if (typeof valueA !== 'object' || valueA == null || typeof valueB !== 'object' || valueB == null) {
|
|
13
10
|
return false
|
|
@@ -43,8 +40,8 @@ export function shallow<T>(valueA: T, valueB: T): boolean {
|
|
|
43
40
|
return true
|
|
44
41
|
}
|
|
45
42
|
|
|
46
|
-
const keysA = Object.keys(valueA
|
|
47
|
-
const keysB = Object.keys(valueB
|
|
43
|
+
const keysA = Object.keys(valueA)
|
|
44
|
+
const keysB = Object.keys(valueB)
|
|
48
45
|
if (keysA.length !== keysB.length) return false
|
|
49
46
|
for (const key of keysA) {
|
|
50
47
|
if (
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { Subscribe } from '../subscriber'
|
|
2
|
+
import { subscriber } from '../subscriber'
|
|
3
|
+
import type { AnyFunction } from '../types'
|
|
4
|
+
|
|
5
|
+
interface CacheItem<T extends AnyFunction> {
|
|
6
|
+
count: number
|
|
7
|
+
returnType: Subscribe<T>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const cache = new WeakMap<AnyFunction, CacheItem<AnyFunction>>()
|
|
11
|
+
let cacheCount = 0
|
|
12
|
+
|
|
13
|
+
export function getDebugCacheCreation() {
|
|
14
|
+
return cacheCount
|
|
15
|
+
}
|
|
16
|
+
function incrementDebugFunctionCreationCount() {
|
|
17
|
+
cacheCount++
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function subMemo<F extends AnyFunction>(anyFunction: F) {
|
|
21
|
+
cacheCount = 0
|
|
22
|
+
return {
|
|
23
|
+
call(): Subscribe<F> {
|
|
24
|
+
const item = cache.get(anyFunction)
|
|
25
|
+
if (item) {
|
|
26
|
+
item.count++
|
|
27
|
+
|
|
28
|
+
return item.returnType
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
incrementDebugFunctionCreationCount()
|
|
32
|
+
const returnType = subscriber(anyFunction)
|
|
33
|
+
const newItem = { count: 1, returnType }
|
|
34
|
+
cache.set(anyFunction, newItem)
|
|
35
|
+
return newItem.returnType
|
|
36
|
+
},
|
|
37
|
+
destroy() {
|
|
38
|
+
const item = cache.get(anyFunction)
|
|
39
|
+
|
|
40
|
+
if (item) {
|
|
41
|
+
item.count--
|
|
42
|
+
if (item.count === 0) {
|
|
43
|
+
item.returnType.destroy()
|
|
44
|
+
cache.delete(anyFunction)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Component } from 'react';
|
|
2
|
+
export declare function longPromise(time?: number): Promise<number>;
|
|
3
|
+
export declare class ErrorBoundary extends Component<{
|
|
4
|
+
fallback: React.ReactNode;
|
|
5
|
+
children: React.ReactNode;
|
|
6
|
+
}, {
|
|
7
|
+
hasError: boolean;
|
|
8
|
+
error: Error | null;
|
|
9
|
+
}> {
|
|
10
|
+
constructor(props: {
|
|
11
|
+
fallback: React.ReactNode;
|
|
12
|
+
children: React.ReactNode;
|
|
13
|
+
});
|
|
14
|
+
static getDerivedStateFromError(error: Error): {
|
|
15
|
+
hasError: boolean;
|
|
16
|
+
error: Error;
|
|
17
|
+
};
|
|
18
|
+
componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void;
|
|
19
|
+
render(): import("react").ReactNode;
|
|
20
|
+
}
|
package/types/create.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export
|
|
1
|
+
import type { Emitter } from './utils/create-emitter';
|
|
2
|
+
import type { Callable, DefaultValue, IsEqual, Listener, SetValue } from './types';
|
|
3
|
+
export declare const createScheduler: {
|
|
4
|
+
add<T>(id: number, option: import("./utils/scheduler").SchedulerOptions<T>): () => void;
|
|
5
|
+
schedule<T>(id: number, value: T): void;
|
|
6
|
+
};
|
|
7
|
+
interface RawState<T> {
|
|
8
|
+
(): T;
|
|
9
|
+
id: number;
|
|
10
|
+
set: (value: SetValue<T>) => void;
|
|
11
|
+
emitter: Emitter<T>;
|
|
12
|
+
listen: Listener<T>;
|
|
13
|
+
destroy: () => void;
|
|
14
|
+
withName: (name: string) => RawState<T>;
|
|
15
|
+
stateName?: string;
|
|
16
|
+
}
|
|
17
|
+
export type State<T> = {
|
|
18
|
+
readonly [K in keyof RawState<T>]: RawState<T>[K];
|
|
19
|
+
} & Callable<T>;
|
|
20
|
+
export declare function create<T>(initialValue: DefaultValue<T>, isEqual?: IsEqual<T>): State<T>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type StateType = 'state' | 'derived';
|
|
2
|
+
interface SendOptions {
|
|
3
|
+
message?: string;
|
|
4
|
+
type: StateType;
|
|
5
|
+
value: unknown;
|
|
6
|
+
name: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function sendToDevelopmentTools(options: SendOptions): void;
|
|
9
|
+
export declare function developmentToolsListener(name: string, type: StateType): (value: unknown) => void;
|
|
10
|
+
export {};
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { create } from './create';
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export { useStateValue } from './use-state-value';
|
|
6
|
-
export { shallow } from './shallow';
|
|
3
|
+
export { use } from './use';
|
|
4
|
+
export { shallow } from './utils/shallow';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AnyFunction, Callable, Listener } from './types';
|
|
2
|
+
import type { Emitter } from './utils/create-emitter';
|
|
3
|
+
interface SubscribeContext<T = unknown> {
|
|
4
|
+
addEmitter: (emitter: Emitter<T>) => void;
|
|
5
|
+
id: number;
|
|
6
|
+
sub: () => void;
|
|
7
|
+
}
|
|
8
|
+
interface SubscribeRaw<F extends AnyFunction, T extends ReturnType<F> = ReturnType<F>> {
|
|
9
|
+
(): T;
|
|
10
|
+
emitter: Emitter<T | undefined>;
|
|
11
|
+
destroy: () => void;
|
|
12
|
+
id: number;
|
|
13
|
+
listen: Listener<T>;
|
|
14
|
+
abort: () => void;
|
|
15
|
+
}
|
|
16
|
+
export type Subscribe<F extends AnyFunction, T extends ReturnType<F> = ReturnType<F>> = {
|
|
17
|
+
readonly [K in keyof SubscribeRaw<F, T>]: SubscribeRaw<F, T>[K];
|
|
18
|
+
} & Callable<T>;
|
|
19
|
+
export declare const context: {
|
|
20
|
+
run: <R>(ctxValue: SubscribeContext<unknown> | undefined, cb: () => R | Promise<R>) => R;
|
|
21
|
+
use: () => SubscribeContext<unknown> | undefined;
|
|
22
|
+
wrap: <X>(cb: () => X | Promise<X>) => () => X | Promise<X>;
|
|
23
|
+
};
|
|
24
|
+
export declare function subscriber<F extends AnyFunction, T extends ReturnType<F> = ReturnType<F>>(anyFunction: () => T): Subscribe<F, T>;
|
|
25
|
+
export {};
|
package/types/types.d.ts
CHANGED
|
@@ -1,68 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Equality check function.
|
|
4
|
-
*/
|
|
1
|
+
export type AnyFunction = (...args: any[]) => any;
|
|
5
2
|
export type IsEqual<T = unknown> = (a: T, b: T) => boolean;
|
|
6
|
-
export type
|
|
7
|
-
|
|
8
|
-
* Set new state value function.
|
|
9
|
-
*/
|
|
10
|
-
export type SetValue<T> = T | Setter<T>;
|
|
11
|
-
export type UpdateValue<T> = T extends object ? Partial<T> : T;
|
|
12
|
-
/**
|
|
13
|
-
* Set new state function
|
|
14
|
-
*/
|
|
15
|
-
export type StateValue<T, S> = undefined extends S ? T : S;
|
|
16
|
-
export type Set<T> = (value: SetValue<T>) => void;
|
|
17
|
-
export type Update<T> = (value: UpdateValue<T>) => void;
|
|
18
|
-
/**
|
|
19
|
-
* Getting state value function.
|
|
20
|
-
*/
|
|
21
|
-
export type GetState<T> = () => T;
|
|
22
|
-
export interface StateDataInternal<T = unknown> {
|
|
23
|
-
value?: T;
|
|
24
|
-
updateVersion: number;
|
|
25
|
-
}
|
|
26
|
-
export declare enum StateKeys {
|
|
27
|
-
IS_STATE = "isState",
|
|
28
|
-
IS_SLICE = "isSlice"
|
|
29
|
-
}
|
|
30
|
-
export interface BaseState<T> {
|
|
31
|
-
/**
|
|
32
|
-
* Reset state to default value if it's basic atom - if it's family - it will clear all family members
|
|
33
|
-
*/
|
|
34
|
-
reset: () => void;
|
|
35
|
-
/**
|
|
36
|
-
* Get current state value
|
|
37
|
-
*/
|
|
38
|
-
getState: GetState<T>;
|
|
39
|
-
select: <S>(selector: (value: T) => S, isEqual?: IsEqual<S>) => GetterState<S>;
|
|
40
|
-
merge: <T2, S>(state2: GetterState<T2>, selector: (value1: T, value2: T2) => S, isEqual?: IsEqual<S>) => GetterState<S>;
|
|
41
|
-
/**
|
|
42
|
-
* Internal state data
|
|
43
|
-
*/
|
|
44
|
-
__internal: {
|
|
45
|
-
emitter: Emitter<T>;
|
|
46
|
-
};
|
|
47
|
-
subscribe: (listener: (value: T) => void) => () => void;
|
|
48
|
-
}
|
|
49
|
-
export interface GetterState<T> extends BaseState<T> {
|
|
50
|
-
<S>(selector?: (state: T) => S, isEqual?: IsEqual<S>): StateValue<T, S>;
|
|
51
|
-
}
|
|
52
|
-
export interface SetterState<T> extends GetterState<T> {
|
|
53
|
-
/**
|
|
54
|
-
* Set new state value
|
|
55
|
-
*/
|
|
56
|
-
setState: Set<T>;
|
|
57
|
-
/**
|
|
58
|
-
* Set new state value
|
|
59
|
-
*/
|
|
60
|
-
updateState: Update<T>;
|
|
61
|
-
}
|
|
62
|
-
export type State<T> = SetterState<T> | GetterState<T>;
|
|
3
|
+
export type SetStateCb<T> = (value: T) => Awaited<T>;
|
|
4
|
+
export type SetValue<T> = SetStateCb<T> | Awaited<T>;
|
|
63
5
|
export type DefaultValue<T> = T | (() => T);
|
|
64
|
-
export
|
|
65
|
-
export interface
|
|
66
|
-
current
|
|
67
|
-
|
|
6
|
+
export type Listener<T> = (listener: (value: T) => void) => () => void;
|
|
7
|
+
export interface Cache<T> {
|
|
8
|
+
current?: T;
|
|
9
|
+
previous?: T;
|
|
68
10
|
}
|
|
11
|
+
export type Callable<T> = () => T;
|
|
12
|
+
export declare const EMPTY_SELECTOR: <T>(stateValue: T) => T;
|
package/types/use.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Cache, IsEqual } from '../types';
|
|
2
|
+
export declare enum Abort {
|
|
3
|
+
Error = "StateAbortError"
|
|
4
|
+
}
|
|
5
|
+
export interface CancelablePromise<T> {
|
|
6
|
+
promise?: Promise<T>;
|
|
7
|
+
controller?: AbortController;
|
|
8
|
+
resolveInitialPromise?: (value: T) => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Cancelable promise function, return promise and controller
|
|
12
|
+
*/
|
|
13
|
+
export declare function cancelablePromise<T>(promise: Promise<T>, previousController?: AbortController): CancelablePromise<T>;
|
|
14
|
+
export declare function generateId(): number;
|
|
15
|
+
export declare function canUpdate<T>(cache: Cache<T>, isEqual?: IsEqual<T>): boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type EmitterSubscribe<P = undefined> = (listener: (...params: P[]) => void) => () => void;
|
|
2
|
+
export interface Emitter<T, P = undefined> {
|
|
3
|
+
subscribe: EmitterSubscribe<P>;
|
|
4
|
+
subscribeToOtherEmitter: (emitter: Emitter<unknown>) => void;
|
|
5
|
+
getSnapshot: () => T;
|
|
6
|
+
getInitialSnapshot?: () => T;
|
|
7
|
+
emit: (...params: P[]) => void;
|
|
8
|
+
getSize: () => number;
|
|
9
|
+
clear: () => void;
|
|
10
|
+
contains: (listener: (...params: P[]) => void) => boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Generics parameters are:
|
|
14
|
+
* T: Type of the state
|
|
15
|
+
* R: Type of the snapshot
|
|
16
|
+
* P: Type of the parameters
|
|
17
|
+
* @param getSnapshot
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export declare function createEmitter<T, P = undefined>(getSnapshot: () => T, getInitialSnapshot?: () => T): Emitter<T, P>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SetStateCb, SetValue } from '../types';
|
|
2
|
+
import type { State } from '../create';
|
|
3
|
+
export declare function isPromise<T>(value: unknown): value is Promise<T>;
|
|
4
|
+
export declare function isFunction<T extends (...args: unknown[]) => unknown>(value: unknown): value is T;
|
|
5
|
+
export declare function isMap(value: unknown): value is Map<unknown, unknown>;
|
|
6
|
+
export declare function isSet(value: unknown): value is Set<unknown>;
|
|
7
|
+
export declare function isArray(value: unknown): value is Array<unknown>;
|
|
8
|
+
export declare function isEqualBase<T>(valueA: T, valueB: T): boolean;
|
|
9
|
+
export declare function isSetValueFunction<T>(value: SetValue<T>): value is SetStateCb<T>;
|
|
10
|
+
export declare function isAbortError(value: unknown): value is DOMException;
|
|
11
|
+
export declare function isAnyOtherError(value: unknown): value is Error;
|
|
12
|
+
export declare function isUndefined(value: unknown): value is undefined;
|
|
13
|
+
export declare function isCreate(value: unknown): value is State<unknown>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const THRESHOLD = 0.2;
|
|
2
|
+
export declare const THRESHOLD_ITEMS = 10;
|
|
3
|
+
export declare const RESCHEDULE_COUNT = 0;
|
|
4
|
+
export interface SchedulerOptions<T> {
|
|
5
|
+
readonly onResolveItem?: (item: T) => void;
|
|
6
|
+
readonly onFinish: () => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function createScheduler<T>(options: SchedulerOptions<T>): (value: T) => void;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Subscribe } from '../subscriber';
|
|
2
|
+
import type { AnyFunction } from '../types';
|
|
3
|
+
export declare function getDebugCacheCreation(): number;
|
|
4
|
+
export declare function subMemo<F extends AnyFunction>(anyFunction: F): {
|
|
5
|
+
call(): Subscribe<F>;
|
|
6
|
+
destroy(): void;
|
|
7
|
+
};
|
package/esm/common.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{useSyncExternalStoreWithSelector as u}from"use-sync-external-store/shim/with-selector";import{useDebugValue as s}from"react";function S(e){return e}function i(e,t,r){const n=u(e.subscribe,e.getSnapshot,e.getSnapshot,t?o=>t(o):S,r);return s(n),n}export{S as toType,i as useSyncExternalStore};
|
package/esm/create-base-state.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{merge as c}from"./merge";import{select as p}from"./select";function y(n){const{emitter:o,getGetterState:s,reset:i,getState:e}=n;return{getState:e,reset:i,select(t,r){const a=s();return p(a,t,r)},merge(t,r,a){const m=s();return c(m,t,r,a)},__internal:{emitter:o},subscribe(t){return t(e()),o.subscribe(()=>{t(e())})}}}export{y as createBaseState};
|
package/esm/create-emitter.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function n(r){const t=new Set;return{subscribe:e=>(t.add(e),()=>{t.delete(e)}),emit:(...e)=>{for(const i of t)i(...e)},getSnapshot:r}}export{n as createEmitter};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{useStateValue as n}from"./use-state-value";function i(r){const{baseState:t}=r,e=(a,s)=>n(e,a,s);return e.__internal=t.__internal,e.getState=t.getState,e.reset=t.reset,e.select=t.select,e.merge=t.merge,e.subscribe=t.subscribe,e}export{i as createGetterState};
|
package/esm/is.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function o(n){return n instanceof Promise}function r(n){return typeof n=="function"}function u(n){return typeof n=="function"}function t(n){return typeof n=="object"&&n!==null}function i(n){return t(n)&&n.isRef===!0}function s(n){return n instanceof Map}function f(n){return n instanceof Set}function a(n){return Array.isArray(n)}function c(n,e){return n===e?!0:!!Object.is(n,e)}export{a as isArray,c as isEqualBase,r as isFunction,s as isMap,t as isObject,o as isPromise,i as isRef,f as isSet,u as isSetValueFunction};
|
package/esm/merge.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{createBaseState as u}from"./create-base-state";import{createEmitter as s}from"./create-emitter";import{createGetterState as c}from"./create-getter-state";import{isEqualBase as f}from"./is";function T(t,e,i,n=f){let r;const a=s(()=>{const S=i(t.getState(),e.getState());return r!==void 0&&n(r,S)?r:(r=S,S)});t.__internal.emitter.subscribe(()=>{a.emit()}),e.__internal.emitter.subscribe(()=>{a.emit()});const o=u({emitter:a,getGetterState:()=>m,getState:()=>i(t.getState(),e.getState()),reset(){t.reset(),e.reset()}}),m=c({baseState:o});return m}export{T as merge};
|
package/esm/select.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{createBaseState as m}from"./create-base-state";import{createEmitter as s}from"./create-emitter";import{createGetterState as u}from"./create-getter-state";import{isEqualBase as c}from"./is";function d(t,a,o=c){let e;const S=s(()=>{const r=a(t.getState());return e!==void 0&&o(e,r)?e:(e=r,r)});t.__internal.emitter.subscribe(()=>{S.emit()});const n=m({emitter:S,getGetterState:()=>i,getState:()=>a(t.getState()),reset:t.reset}),i=u({baseState:n});return i}export{d as select};
|
package/esm/shallow.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{isArray as o,isMap as f,isSet as i}from"./is";function b(t,r){if(t==r||Object.is(t,r))return!0;if(typeof t!="object"||t==null||typeof r!="object"||r==null)return!1;if(f(t)&&f(r)){if(t.size!==r.size)return!1;for(const[n,e]of t)if(!Object.is(e,r.get(n)))return!1;return!0}if(i(t)&&i(r)){if(t.size!==r.size)return!1;for(const n of t)if(!r.has(n))return!1;return!0}if(o(t)&&o(r)){if(t.length!==r.length)return!1;for(const[n,e]of t.entries())if(!Object.is(e,r[n]))return!1;return!0}const s=Object.keys(t),c=Object.keys(r);if(s.length!==c.length)return!1;for(const n of s)if(!Object.prototype.hasOwnProperty.call(r,n)||!Object.is(t[n],r[n]))return!1;return!0}export{b as shallow};
|
package/esm/use-state-value.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{useSyncExternalStore as n,toType as S}from"./common";import{isPromise as i}from"./is";function m(e,r=t=>S(t),o){const t=n(e.__internal.emitter,a=>r(a),o);if(i(t))throw t;return t}export{m as useStateValue};
|
package/src/common.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { useSyncExternalStoreWithSelector as useSync } from 'use-sync-external-store/shim/with-selector'
|
|
2
|
-
import type { Emitter } from './create-emitter'
|
|
3
|
-
import type { IsEqual } from './types'
|
|
4
|
-
import { useDebugValue } from 'react'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Todo need to remove this
|
|
8
|
-
*/
|
|
9
|
-
export function toType<T>(object?: unknown): T {
|
|
10
|
-
return object as T
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function useSyncExternalStore<T, S>(
|
|
14
|
-
emitter: Emitter<T>,
|
|
15
|
-
selector: (stateValue: T) => S,
|
|
16
|
-
isEqual?: IsEqual<S>,
|
|
17
|
-
): undefined extends S ? T : S {
|
|
18
|
-
const value = useSync<T, S>(
|
|
19
|
-
emitter.subscribe,
|
|
20
|
-
emitter.getSnapshot,
|
|
21
|
-
emitter.getSnapshot,
|
|
22
|
-
selector ? (stateValue) => selector(stateValue) : toType,
|
|
23
|
-
isEqual,
|
|
24
|
-
) as undefined extends S ? T : S
|
|
25
|
-
|
|
26
|
-
useDebugValue(value)
|
|
27
|
-
return value
|
|
28
|
-
}
|
package/src/create-base-state.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { Emitter } from './create-emitter'
|
|
2
|
-
import { merge } from './merge'
|
|
3
|
-
import { select } from './select'
|
|
4
|
-
import type { BaseState, GetterState } from './types'
|
|
5
|
-
|
|
6
|
-
interface Options<T> {
|
|
7
|
-
readonly emitter: Emitter<T>
|
|
8
|
-
readonly reset: () => void
|
|
9
|
-
readonly getState: () => T
|
|
10
|
-
readonly getGetterState: () => GetterState<T>
|
|
11
|
-
}
|
|
12
|
-
export function createBaseState<T>(options: Options<T>): BaseState<T> {
|
|
13
|
-
const { emitter, getGetterState, reset, getState } = options
|
|
14
|
-
return {
|
|
15
|
-
getState,
|
|
16
|
-
reset,
|
|
17
|
-
select(selector, isSame) {
|
|
18
|
-
const state = getGetterState()
|
|
19
|
-
return select(state, selector, isSame)
|
|
20
|
-
},
|
|
21
|
-
merge(state2, selector, isEqualHook) {
|
|
22
|
-
const state = getGetterState()
|
|
23
|
-
return merge(state, state2, selector, isEqualHook)
|
|
24
|
-
},
|
|
25
|
-
__internal: {
|
|
26
|
-
emitter,
|
|
27
|
-
},
|
|
28
|
-
subscribe(listener) {
|
|
29
|
-
listener(getState())
|
|
30
|
-
return emitter.subscribe(() => {
|
|
31
|
-
listener(getState())
|
|
32
|
-
})
|
|
33
|
-
},
|
|
34
|
-
}
|
|
35
|
-
}
|