@based/react 4.1.0 → 4.2.1

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/src/meta.ts DELETED
@@ -1,74 +0,0 @@
1
- import { useEffect, useState } from 'react'
2
-
3
- const errors = {}
4
- const errorListeners = new Set()
5
- const loadings = new Set()
6
- const loadingListeners = new Set()
7
-
8
- let isLoading = false
9
- let lastError = ''
10
- let errorCnt = 0
11
- let errorKey = errorCnt + lastError
12
-
13
- export function updateMeta(subKey, loading, error) {
14
- if (error) {
15
- lastError = error
16
- if (subKey in errors) {
17
- errors[subKey] = error
18
- } else {
19
- errors[subKey] = error
20
- errorCnt++
21
- }
22
- } else {
23
- if (subKey in errors) {
24
- errorCnt--
25
- delete errors[subKey]
26
- }
27
- }
28
-
29
- const newErrorKey = errorCnt + lastError
30
- if (newErrorKey !== errorKey) {
31
- errorKey = newErrorKey
32
- errorListeners.forEach((fn: Function) => fn(errorKey))
33
- }
34
-
35
- if (loading) {
36
- loadings.add(subKey)
37
- } else {
38
- loadings.delete(subKey)
39
- }
40
-
41
- const newLoading = !!loadings.size
42
- if (newLoading !== isLoading) {
43
- isLoading = newLoading
44
- loadingListeners.forEach((fn: Function) => fn(isLoading))
45
- }
46
- }
47
-
48
- export function useLoading() {
49
- const [, setLoading] = useState(isLoading)
50
-
51
- loadingListeners.add(setLoading)
52
-
53
- useEffect(() => {
54
- return () => {
55
- loadingListeners.delete(setLoading)
56
- }
57
- }, [])
58
-
59
- return { loading: isLoading }
60
- }
61
-
62
- export function useError() {
63
- const [, setError] = useState(errorKey)
64
-
65
- errorListeners.add(setError)
66
-
67
- useEffect(() => {
68
- return () => {
69
- errorListeners.delete(setError)
70
- }
71
- }, [])
72
-
73
- return { error: errorCnt ? lastError : null, errors: Object.values(errors) }
74
- }
package/src/reducer.ts DELETED
@@ -1,42 +0,0 @@
1
- import { Data, Loading } from './types'
2
-
3
- export function resultReducer(
4
- state: { data: Data; error?: Error; loading: Loading; checksum: number },
5
- action: {
6
- merge?: Data
7
- data?: Data
8
- error?: Error
9
- loading?: Loading
10
- checksum?: number
11
- }
12
- ) {
13
- if (action.error) {
14
- state.error = action.error
15
- }
16
- if (action.data) {
17
- state.checksum = action.checksum || 0
18
- state.data = action.data
19
- state.loading = false
20
- if (state.error) {
21
- delete state.error
22
- }
23
- }
24
-
25
- if (action.merge) {
26
- state.checksum = action.checksum || 0
27
- if (!state.data) {
28
- state.data = action.merge
29
- } else {
30
- Object.assign(state.data, action.merge)
31
- }
32
- state.loading = false
33
- if (state.error) {
34
- delete state.error
35
- }
36
- }
37
-
38
- if (action.loading) {
39
- state.loading = action.loading
40
- }
41
- return { ...state }
42
- }
package/src/types.ts DELETED
@@ -1,5 +0,0 @@
1
- import { GenericObject } from '@based/client'
2
-
3
- export type Data = GenericObject
4
-
5
- export type Loading = boolean