muya 2.0.0-beta.2 → 2.0.0

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.
Files changed (71) hide show
  1. package/README.md +124 -195
  2. package/cjs/index.js +1 -1
  3. package/esm/create-state.js +1 -0
  4. package/esm/create.js +1 -1
  5. package/esm/debug/development-tools.js +1 -1
  6. package/esm/index.js +1 -1
  7. package/esm/scheduler.js +1 -0
  8. package/esm/select.js +1 -0
  9. package/esm/use-value.js +1 -0
  10. package/esm/utils/__tests__/is.test.js +1 -1
  11. package/esm/utils/common.js +1 -1
  12. package/esm/utils/is.js +1 -1
  13. package/package.json +12 -12
  14. package/{packages/core → src}/__tests__/bench.test.tsx +3 -108
  15. package/src/__tests__/create.test.tsx +159 -0
  16. package/src/__tests__/scheduler.test.tsx +52 -0
  17. package/src/__tests__/select.test.tsx +127 -0
  18. package/src/__tests__/use-value.test.tsx +78 -0
  19. package/src/create-state.ts +50 -0
  20. package/src/create.ts +67 -0
  21. package/{packages/core → src}/debug/development-tools.ts +18 -3
  22. package/{packages/core → src}/index.ts +2 -1
  23. package/{packages/core/utils/global-scheduler.ts → src/scheduler.ts} +9 -3
  24. package/src/select.ts +69 -0
  25. package/src/types.ts +66 -0
  26. package/src/use-value.ts +22 -0
  27. package/{packages/core → src}/utils/__tests__/is.test.ts +24 -7
  28. package/{packages/core → src}/utils/common.ts +35 -10
  29. package/{packages/core → src}/utils/is.ts +5 -8
  30. package/types/create-state.d.ts +12 -0
  31. package/types/create.d.ts +6 -18
  32. package/types/debug/development-tools.d.ts +2 -9
  33. package/types/index.d.ts +2 -1
  34. package/types/{utils/scheduler.d.ts → scheduler.d.ts} +4 -1
  35. package/types/select.d.ts +10 -0
  36. package/types/types.d.ts +55 -5
  37. package/types/use-value.d.ts +2 -0
  38. package/types/utils/common.d.ts +6 -5
  39. package/types/utils/is.d.ts +3 -4
  40. package/esm/__tests__/create-async.test.js +0 -1
  41. package/esm/subscriber.js +0 -1
  42. package/esm/use.js +0 -1
  43. package/esm/utils/__tests__/context.test.js +0 -1
  44. package/esm/utils/__tests__/sub-memo.test.js +0 -1
  45. package/esm/utils/create-context.js +0 -1
  46. package/esm/utils/global-scheduler.js +0 -1
  47. package/esm/utils/scheduler.js +0 -1
  48. package/esm/utils/sub-memo.js +0 -1
  49. package/packages/core/__tests__/create-async.test.ts +0 -88
  50. package/packages/core/__tests__/create.test.tsx +0 -107
  51. package/packages/core/__tests__/subscriber.test.tsx +0 -89
  52. package/packages/core/__tests__/use-async.test.tsx +0 -45
  53. package/packages/core/__tests__/use.test.tsx +0 -125
  54. package/packages/core/create.ts +0 -98
  55. package/packages/core/subscriber.ts +0 -165
  56. package/packages/core/types.ts +0 -15
  57. package/packages/core/use.ts +0 -57
  58. package/packages/core/utils/__tests__/context.test.ts +0 -198
  59. package/packages/core/utils/__tests__/sub-memo.test.ts +0 -13
  60. package/packages/core/utils/create-context.ts +0 -60
  61. package/packages/core/utils/scheduler.ts +0 -59
  62. package/packages/core/utils/sub-memo.ts +0 -49
  63. package/types/subscriber.d.ts +0 -25
  64. package/types/use.d.ts +0 -2
  65. package/types/utils/create-context.d.ts +0 -5
  66. package/types/utils/global-scheduler.d.ts +0 -5
  67. package/types/utils/sub-memo.d.ts +0 -7
  68. /package/{packages/core → src}/__tests__/test-utils.ts +0 -0
  69. /package/{packages/core → src}/utils/__tests__/shallow.test.ts +0 -0
  70. /package/{packages/core → src}/utils/create-emitter.ts +0 -0
  71. /package/{packages/core → src}/utils/shallow.ts +0 -0
@@ -1,165 +0,0 @@
1
- import type { AnyFunction, Cache, Callable, Listener } from './types'
2
- import type { CancelablePromise } from './utils/common'
3
- import { cancelablePromise, canUpdate, generateId } from './utils/common'
4
- import { createContext } from './utils/create-context'
5
- import type { Emitter } from './utils/create-emitter'
6
- import { createEmitter } from './utils/create-emitter'
7
- import type { StateType } from './debug/development-tools'
8
- import { developmentToolsListener, sendToDevelopmentTools } from './debug/development-tools'
9
- import { createGlobalScheduler } from './utils/global-scheduler'
10
- import { isAbortError, isCreate, isEqualBase, isPromise, isUndefined } from './utils/is'
11
-
12
- const subscriberScheduler = createGlobalScheduler()
13
- interface SubscribeContext<T = unknown> {
14
- addEmitter: (emitter: Emitter<T>) => void
15
- id: number
16
- sub: () => void
17
- }
18
- interface SubscribeRaw<F extends AnyFunction, T extends ReturnType<F> = ReturnType<F>> {
19
- (): T
20
- emitter: Emitter<T | undefined>
21
- destroy: () => void
22
- id: number
23
- listen: Listener<T>
24
- abort: () => void
25
- }
26
-
27
- export type Subscribe<F extends AnyFunction, T extends ReturnType<F> = ReturnType<F>> = {
28
- readonly [K in keyof SubscribeRaw<F, T>]: SubscribeRaw<F, T>[K]
29
- } & Callable<T>
30
-
31
- export const context = createContext<SubscribeContext | undefined>(undefined)
32
-
33
- export function subscriber<F extends AnyFunction, T extends ReturnType<F> = ReturnType<F>>(
34
- anyFunction: () => T,
35
- ): Subscribe<F, T> {
36
- const cache: Cache<T> = {}
37
- let isInitialized = false
38
- const cleaners: Array<() => void> = []
39
- const promiseData: CancelablePromise<T> = {}
40
-
41
- const emitter = createEmitter(
42
- () => {
43
- if (!isInitialized) {
44
- isInitialized = true
45
- return subscribe()
46
- }
47
- return cache.current
48
- },
49
- () => {
50
- isInitialized = true
51
- return subscribe()
52
- },
53
- )
54
-
55
- async function sub() {
56
- if (!canUpdate(cache, isEqualBase)) {
57
- return
58
- }
59
- if (promiseData.controller) {
60
- promiseData.controller.abort()
61
- }
62
-
63
- subscriberScheduler.schedule(id, null)
64
- }
65
-
66
- const id = generateId()
67
-
68
- const clearScheduler = subscriberScheduler.add(id, {
69
- onFinish() {
70
- cache.current = subscribe()
71
- emitter.emit()
72
- },
73
- })
74
- const ctx: SubscribeContext = {
75
- addEmitter(stateEmitter) {
76
- const clean = stateEmitter.subscribe(sub)
77
- cleaners.push(clean)
78
- },
79
- id,
80
- sub,
81
- }
82
-
83
- function asyncSub(resultValue: Promise<T>): Promise<T> | undefined {
84
- const cancel = cancelablePromise<T>(resultValue, promiseData.controller)
85
- promiseData.controller = cancel.controller
86
- cancel.promise
87
- ?.then((value) => {
88
- cache.current = value
89
- emitter.emit()
90
- })
91
- .catch((error) => {
92
- if (isAbortError(error)) {
93
- return
94
- }
95
- cache.current = error
96
- emitter.emit()
97
- })
98
- return cancel.promise
99
- }
100
-
101
- const subscribe = function (): T {
102
- const resultValue = context.run(ctx, anyFunction)
103
-
104
- if (!isPromise(resultValue)) {
105
- cache.current = resultValue
106
- return resultValue
107
- }
108
-
109
- const promise = context.wrap(() => asyncSub(resultValue))()
110
- if (isPromise(promise)) {
111
- // we do not do anything with the promise, because it is already handled in asyncSub
112
- promise.catch(() => null)
113
- }
114
- const promiseAsT = promise as T
115
- cache.current = promiseAsT
116
- return promiseAsT
117
- }
118
-
119
- subscribe.emitter = emitter
120
- subscribe.destroy = function () {
121
- for (const cleaner of cleaners) {
122
- cleaner()
123
- }
124
- emitter.clear()
125
- clearScheduler()
126
- }
127
- subscribe.id = id
128
- subscribe.listen = function (listener: (value: T) => void) {
129
- return emitter.subscribe(() => {
130
- const final = cache.current
131
- if (isUndefined(final)) {
132
- throw new Error('The value is undefined')
133
- }
134
- listener(final)
135
- })
136
- }
137
-
138
- if (process.env.NODE_ENV === 'development') {
139
- let name: string | undefined
140
- let type: StateType = 'derived'
141
- if (isCreate(anyFunction)) {
142
- type = 'state'
143
- name = anyFunction.stateName
144
- }
145
-
146
- if (!name) {
147
- name = anyFunction.name.length > 0 ? anyFunction.name : anyFunction.toString()
148
- }
149
-
150
- sendToDevelopmentTools({
151
- name,
152
- type,
153
- value: subscribe(),
154
- message: 'init',
155
- })
156
- const listener = developmentToolsListener(name, type)
157
- subscribe.listen(listener)
158
- }
159
- subscribe.abort = function () {
160
- if (promiseData.controller) {
161
- promiseData.controller.abort()
162
- }
163
- }
164
- return subscribe
165
- }
@@ -1,15 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
- export type AnyFunction = (...args: any[]) => any
3
-
4
- export type IsEqual<T = unknown> = (a: T, b: T) => boolean
5
- export type SetStateCb<T> = (value: T) => Awaited<T>
6
- export type SetValue<T> = SetStateCb<T> | Awaited<T>
7
- export type DefaultValue<T> = T | (() => T)
8
- export type Listener<T> = (listener: (value: T) => void) => () => void
9
- export interface Cache<T> {
10
- current?: T
11
- previous?: T
12
- }
13
- export type Callable<T> = () => T
14
-
15
- export const EMPTY_SELECTOR = <T>(stateValue: T) => stateValue
@@ -1,57 +0,0 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
- /* eslint-disable sonarjs/rules-of-hooks */
3
- import { useDebugValue, useEffect, useRef } from 'react'
4
- import { EMPTY_SELECTOR, type AnyFunction } from './types'
5
- import { isAnyOtherError, isPromise } from './utils/is'
6
- import { useSyncExternalStore } from 'react'
7
- import { subMemo } from './utils/sub-memo'
8
-
9
- const PROMOTE_DEBUG_AFTER_REACH_TIMES = 10
10
- const PROMOTE_DEBUG_AFTER_REACH_COUNT = 3
11
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
12
- function useDebugFunction<F extends AnyFunction>(function_: F) {
13
- const renderCount = useRef({ renders: 0, startTime: performance.now() })
14
- useEffect(() => {
15
- renderCount.current.renders++
16
- const passedTime = performance.now() - renderCount.current.startTime
17
- if (passedTime < PROMOTE_DEBUG_AFTER_REACH_TIMES) {
18
- return
19
- }
20
- if (renderCount.current.renders < PROMOTE_DEBUG_AFTER_REACH_COUNT) {
21
- return
22
- }
23
- renderCount.current.startTime = performance.now()
24
- renderCount.current.renders = 0
25
- // eslint-disable-next-line no-console
26
- console.warn(
27
- `Function ${function_.name.length > 0 ? function_.name : function_} seems to be not memoized, wrap the function to the useCallback or use global defined functions.`,
28
- )
29
- }, [function_])
30
- }
31
-
32
- export function use<F extends AnyFunction, T extends ReturnType<F>, S extends ReturnType<F>>(
33
- anyFunction: () => T,
34
- selector: (stateValue: T) => S = EMPTY_SELECTOR,
35
- ): undefined extends S ? T : S {
36
- const memo = subMemo(anyFunction)
37
- const sub = memo.call()
38
- const initialSnapshot = sub.emitter.getInitialSnapshot ?? sub.emitter.getSnapshot
39
- useEffect(() => {
40
- return memo.destroy
41
- }, [anyFunction, memo.destroy])
42
-
43
- const value = useSyncExternalStore<S>(
44
- sub.emitter.subscribe,
45
- () => selector(sub.emitter.getSnapshot() as T),
46
- () => selector(initialSnapshot() as T),
47
- )
48
- useDebugValue(value)
49
- if (isPromise(value)) {
50
- throw value
51
- }
52
- if (isAnyOtherError(value)) {
53
- memo.destroy()
54
- throw value
55
- }
56
- return value
57
- }
@@ -1,198 +0,0 @@
1
- /* eslint-disable sonarjs/pseudo-random */
2
- /* eslint-disable sonarjs/no-nested-functions */
3
- import { createContext } from '../create-context'
4
- import { longPromise } from '../../__tests__/test-utils'
5
-
6
- describe('context', () => {
7
- it('should check context', () => {
8
- const context = createContext({ name: 'John Doe' })
9
-
10
- const main = () => {
11
- context.run({ name: 'Jane Doe' }, () => {
12
- expect(context.use()).toEqual({ name: 'Jane Doe' })
13
- })
14
- }
15
- expect(context.use()).toEqual({ name: 'John Doe' })
16
- main()
17
- expect(context.use()).toEqual({ name: 'John Doe' })
18
- })
19
-
20
- it('should test async context', (done) => {
21
- const context = createContext<string>('empty')
22
- // eslint-disable-next-line unicorn/consistent-function-scoping
23
- const awaiter = async () => new Promise((resolve) => setTimeout(resolve, 10))
24
- context.run('outer', () => {
25
- expect(context.use()).toEqual('outer')
26
-
27
- // Wrap the asynchronous callback to preserve 'outer' context
28
- setTimeout(
29
- context.wrap(async () => {
30
- try {
31
- await awaiter()
32
- expect(context.use()).toEqual('outer')
33
- innerDone()
34
- } catch (error) {
35
- done(error)
36
- }
37
- }),
38
- 10,
39
- )
40
-
41
- context.run('inner', () => {
42
- expect(context.use()).toEqual('inner')
43
-
44
- // Wrap the asynchronous callback to preserve 'inner' context
45
- setTimeout(
46
- context.wrap(() => {
47
- try {
48
- expect(context.use()).toEqual('inner')
49
- innerDone()
50
- } catch (error) {
51
- done(error)
52
- }
53
- }),
54
- 10,
55
- )
56
- })
57
-
58
- expect(context.use()).toEqual('outer')
59
- })
60
-
61
- let completed = 0
62
- function innerDone() {
63
- completed += 1
64
- if (completed === 2) {
65
- done()
66
- }
67
- }
68
- })
69
- it('should test async nested context', (done) => {
70
- const context = createContext(0)
71
- context.run(1, () => {
72
- expect(context.use()).toEqual(1)
73
- context.run(2, () => {
74
- context.run(3, () => {
75
- expect(context.use()).toEqual(3)
76
- setTimeout(
77
- context.wrap(() => {
78
- expect(context.use()).toEqual(3)
79
- }),
80
- 10,
81
- )
82
- expect(context.use()).toEqual(3)
83
- })
84
- setTimeout(
85
- context.wrap(() => {
86
- expect(context.use()).toEqual(2)
87
- }),
88
- 10,
89
- )
90
- expect(context.use()).toEqual(2)
91
- context.run(3, () => {
92
- expect(context.use()).toEqual(3)
93
- setTimeout(
94
- context.wrap(() => {
95
- expect(context.use()).toEqual(3)
96
- context.run(4, () => {
97
- expect(context.use()).toEqual(4)
98
- setTimeout(
99
- context.wrap(() => {
100
- expect(context.use()).toEqual(4)
101
- done()
102
- }),
103
- 10,
104
- )
105
- expect(context.use()).toEqual(4)
106
- })
107
- }),
108
- 10,
109
- )
110
- expect(context.use()).toEqual(3)
111
- })
112
- // check back to 2
113
- expect(context.use()).toEqual(2)
114
- })
115
- // check back to 1
116
- expect(context.use()).toEqual(1)
117
- })
118
- // check back to 0
119
- expect(context.use()).toEqual(0)
120
- })
121
- it('should stress test context with async random code', async () => {
122
- const stressCount = 10_000
123
- const context = createContext(0)
124
- for (let index = 0; index < stressCount; index++) {
125
- context.run(index, () => {
126
- expect(context.use()).toEqual(index)
127
- })
128
- }
129
-
130
- const promises: Promise<unknown>[] = []
131
- for (let index = 0; index < stressCount; index++) {
132
- context.run(index, () => {
133
- expect(context.use()).toEqual(index)
134
- const promise = new Promise((resolve) => {
135
- setTimeout(
136
- context.wrap(() => {
137
- expect(context.use()).toEqual(index)
138
- resolve(index)
139
- }),
140
- Math.random() * 100,
141
- )
142
- })
143
- promises.push(promise)
144
- })
145
- }
146
- await Promise.all(promises)
147
- })
148
-
149
- it('should-test-default-value-with-ctx', async () => {
150
- const ctx = createContext({ counter: 1 })
151
- ctx.run({ counter: 10 }, async () => {
152
- await longPromise(10)
153
- expect(ctx.use()?.counter).toBe(10)
154
- })
155
- ctx.run({ counter: 12 }, () => {
156
- expect(ctx.use()?.counter).toBe(12)
157
- })
158
- })
159
- it('should test nested context', () => {
160
- const currentCount = 0
161
- const context = createContext({ count: 0 })
162
- function container() {
163
- const isIn = context.use()
164
- expect(isIn?.count).toBe(currentCount)
165
- context.run({ count: currentCount + 1 }, () => {
166
- const inner = context.use()
167
- expect(inner?.count).toBe(currentCount + 1)
168
- context.run({ count: currentCount + 2 }, () => {
169
- const innerInner = context.use()
170
- expect(innerInner?.count).toBe(currentCount + 2)
171
- })
172
- })
173
- }
174
-
175
- container()
176
- container()
177
- })
178
-
179
- it('should test nested context with async when promise is returned, but not waited', async () => {
180
- const context = createContext({ count: 0 })
181
-
182
- async function insideContextNestedAwaited() {
183
- await longPromise(10)
184
- const ctx = context.use()
185
- expect(ctx?.count).toBe(1)
186
- }
187
- async function insideContext() {
188
- await insideContextNestedAwaited()
189
- // HERE THIS IS NOT WAITED, SO CONTEXT IS LOST.
190
- // insideContextNestedAwaited() // this will not work
191
- context.wrap(insideContextNestedAwaited) // this work
192
- const ctx = context.use()
193
- expect(ctx?.count).toBe(1)
194
- }
195
-
196
- context.run({ count: 1 }, insideContext)
197
- })
198
- })
@@ -1,13 +0,0 @@
1
- /* eslint-disable unicorn/consistent-function-scoping */
2
- import { subMemo } from '../sub-memo'
3
-
4
- describe('memo-fn', () => {
5
- it('should create memo fn', () => {
6
- function toBeMemoized(): boolean {
7
- return true
8
- }
9
-
10
- const memoized = subMemo(toBeMemoized)
11
- expect(memoized.call().emitter).toBeDefined()
12
- })
13
- })
@@ -1,60 +0,0 @@
1
- import { isPromise } from './is'
2
-
3
- const EMPTY_CONTEXT = Symbol('_')
4
-
5
- export function createContext<T>(defaultContextValue: T) {
6
- const contextStack: Array<T | typeof EMPTY_CONTEXT> = []
7
-
8
- function use(): T | undefined {
9
- if (contextStack.length === 0) {
10
- return defaultContextValue
11
- }
12
- const currentContext = contextStack.at(-1)
13
- return currentContext === EMPTY_CONTEXT ? defaultContextValue : currentContext
14
- }
15
-
16
- function run<R>(ctxValue: T, cb: () => R | Promise<R>): R {
17
- contextStack.push(ctxValue)
18
- const result = cb()
19
- const isResultPromise = isPromise(result)
20
- if (isResultPromise) {
21
- return (async () => {
22
- try {
23
- return await result
24
- } finally {
25
- contextStack.pop()
26
- }
27
- })() as R
28
- } else {
29
- contextStack.pop()
30
- return result
31
- }
32
- }
33
-
34
- function wrap<X>(cb: () => X | Promise<X>): () => X | Promise<X> {
35
- const capturedContext = use()
36
- return () => {
37
- contextStack.push(capturedContext!)
38
- const result = cb()
39
- const isResultPromise = isPromise(result)
40
- if (isResultPromise) {
41
- return (async () => {
42
- try {
43
- return await result
44
- } finally {
45
- contextStack.pop()
46
- }
47
- })()
48
- } else {
49
- contextStack.pop()
50
- return result
51
- }
52
- }
53
- }
54
-
55
- return {
56
- run,
57
- use,
58
- wrap,
59
- }
60
- }
@@ -1,59 +0,0 @@
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,49 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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/use.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { type AnyFunction } from './types';
2
- export declare function use<F extends AnyFunction, T extends ReturnType<F>, S extends ReturnType<F>>(anyFunction: () => T, selector?: (stateValue: T) => S): undefined extends S ? T : S;
@@ -1,5 +0,0 @@
1
- export declare function createContext<T>(defaultContextValue: T): {
2
- run: <R>(ctxValue: T, cb: () => R | Promise<R>) => R;
3
- use: () => T | undefined;
4
- wrap: <X>(cb: () => X | Promise<X>) => () => X | Promise<X>;
5
- };
@@ -1,5 +0,0 @@
1
- import type { SchedulerOptions } from './scheduler';
2
- export declare function createGlobalScheduler(): {
3
- add<T>(id: number, option: SchedulerOptions<T>): () => void;
4
- schedule<T>(id: number, value: T): void;
5
- };
@@ -1,7 +0,0 @@
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
- };
File without changes
File without changes
File without changes