@based/react 4.1.0 → 4.2.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.
package/src/gql.ts DELETED
@@ -1,214 +0,0 @@
1
- import { useEffect, useMemo, useReducer, useRef, useState } from 'react'
2
- import {
3
- addSubscriber,
4
- removeSubscriber,
5
- handleGraphqlVariables,
6
- BasedGraphQL,
7
- generateSubscriptionId,
8
- BasedOpts,
9
- } from '@based/client'
10
- import { Loading, Data } from './types'
11
- import { resultReducer } from './reducer'
12
- import { useClient } from './clients'
13
- import { genOptsId } from './genOptsId'
14
- import { updateMeta } from './meta'
15
- import { hashObjectIgnoreKeyOrder } from '@saulx/hash'
16
-
17
- const schemaSubId = generateSubscriptionId({ $subscribe_schema: 'default' })
18
-
19
- // step one make 1 useEffect
20
- // - and the nessecary if
21
-
22
- export function useQuery(
23
- query?: string | BasedGraphQL,
24
- variables: Record<string, any> = {},
25
- clientSelector?: string | (BasedOpts & { key?: string })
26
- ): {
27
- data: Data
28
- error?: Error
29
- loading: Loading
30
- } {
31
- const [result, dispatch] = useReducer(resultReducer, {
32
- loading: true,
33
- data: {},
34
- checksum: 0,
35
- })
36
-
37
- const r = useRef({ checksum: 0, fns: {} })
38
- const selector = clientSelector || 'default'
39
- const client = useClient(selector)
40
-
41
- if (query) {
42
- const [configState, updateConfigState] = useState(0)
43
-
44
- useEffect(() => {
45
- const [, subscriberId] = addSubscriber(
46
- client.client,
47
- // FIXME dont want too many updates plz this has to become 1 use effect
48
- { $subscribe_schema: 'default' },
49
- (d, checksum) => {
50
- if (!client.client.configuration) {
51
- client.client.configuration = {
52
- dbs: [],
53
- schema: {},
54
- functions: {},
55
- } as any // TODO: FIX
56
- }
57
- client.client.configuration.schema.default = d
58
- updateConfigState(checksum)
59
- },
60
- (err) => {
61
- if (err) {
62
- console.error(err)
63
- }
64
- },
65
- (err) => {
66
- console.error(err)
67
- },
68
- schemaSubId
69
- )
70
- return () => {
71
- removeSubscriber(client.client, schemaSubId, subscriberId)
72
- }
73
- return () => {}
74
- }, [])
75
-
76
- if (configState) {
77
- let op: BasedGraphQL
78
- if (typeof query === 'string') {
79
- op = client.gql(query)
80
- } else {
81
- op = query
82
- }
83
-
84
- op = handleGraphqlVariables(op, op, variables)
85
-
86
- const fns: {
87
- [key: string]: { name: string; payload: any; key: string }
88
- } = {}
89
- const queryObj: any = {}
90
- for (const key in op.ops) {
91
- if (op.ops[key].fnObserve) {
92
- const { name, payload } = op.ops[key].fnObserve
93
- fns[key] = { name: <string>name, payload, key }
94
- continue
95
- }
96
- queryObj[key] = op.ops[key].get
97
- }
98
-
99
- const fnHash = useMemo(() => {
100
- return hashObjectIgnoreKeyOrder(fns)
101
- }, [fns])
102
-
103
- const subId = useMemo(() => {
104
- // fn?
105
- return generateSubscriptionId(queryObj)
106
- }, [queryObj])
107
-
108
- const clientKey =
109
- typeof selector === 'string' ? selector : genOptsId(selector)
110
-
111
- if (client) {
112
- const subKey = clientKey + subId
113
-
114
- useEffect(() => {
115
- const subs = []
116
-
117
- for (const key in fns) {
118
- subs.push(
119
- addSubscriber(
120
- client.client,
121
- fns[key].payload,
122
- (d, checksum) => {
123
- updateMeta(subKey, false, false)
124
- if (r.current.fns[key] !== checksum) {
125
- r.current.fns[key] = checksum
126
- dispatch({
127
- merge: { [key]: d },
128
- checksum: hashObjectIgnoreKeyOrder(r.current),
129
- })
130
- }
131
- },
132
- (err) => {
133
- if (err) {
134
- console.error(err)
135
- dispatch({ error: err, loading: false })
136
- }
137
- },
138
- (err) => {
139
- console.error(err)
140
- updateMeta(subKey, false, err)
141
- dispatch({ error: err })
142
- },
143
- undefined,
144
- fns[key].name
145
- )
146
- )
147
- }
148
-
149
- return () => {
150
- for (const [subId, subscriberId] of subs) {
151
- removeSubscriber(client.client, subId, subscriberId)
152
- }
153
- }
154
- }, [fnHash])
155
-
156
- useEffect(() => {
157
- if (!configState) {
158
- return
159
- }
160
- updateMeta(subKey, true, false)
161
- const [, subscriberId] = addSubscriber(
162
- client.client,
163
- queryObj,
164
- (d, checksum) => {
165
- updateMeta(subKey, false, false)
166
- if (r.current.checksum !== checksum) {
167
- r.current.checksum = checksum
168
- dispatch({
169
- merge: d,
170
- checksum: hashObjectIgnoreKeyOrder(r.current),
171
- })
172
- }
173
- },
174
- (err) => {
175
- if (err) {
176
- console.error(err)
177
- updateMeta(subKey, false, err)
178
- dispatch({ error: err, loading: false })
179
- }
180
- },
181
- (err) => {
182
- console.error(err)
183
- updateMeta(subKey, false, err)
184
- dispatch({ error: err })
185
- },
186
- subId
187
- )
188
- return () => {
189
- updateMeta(subKey, false, false)
190
- removeSubscriber(client.client, subId, subscriberId)
191
- }
192
- }, [subId, clientKey, configState])
193
- } else {
194
- useEffect(stubFn, [null, null])
195
- }
196
- } else {
197
- useMemo(stubFn, [null])
198
- useMemo(stubFn, [null])
199
- useEffect(stubFn, [null])
200
- useEffect(stubFn, [null, null, null])
201
- }
202
- } else {
203
- useState(null)
204
- useEffect(stubFn, [null])
205
- useMemo(stubFn, [null])
206
- useMemo(stubFn, [null])
207
- useEffect(stubFn, [null])
208
- useEffect(stubFn, [null, null, null])
209
- }
210
-
211
- return result
212
- }
213
-
214
- function stubFn() {}
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