@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/dist/meta.js DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useError = exports.useLoading = exports.updateMeta = void 0;
4
- const react_1 = require("react");
5
- const errors = {};
6
- const errorListeners = new Set();
7
- const loadings = new Set();
8
- const loadingListeners = new Set();
9
- let isLoading = false;
10
- let lastError = '';
11
- let errorCnt = 0;
12
- let errorKey = errorCnt + lastError;
13
- function updateMeta(subKey, loading, error) {
14
- if (error) {
15
- lastError = error;
16
- if (subKey in errors) {
17
- errors[subKey] = error;
18
- }
19
- else {
20
- errors[subKey] = error;
21
- errorCnt++;
22
- }
23
- }
24
- else {
25
- if (subKey in errors) {
26
- errorCnt--;
27
- delete errors[subKey];
28
- }
29
- }
30
- const newErrorKey = errorCnt + lastError;
31
- if (newErrorKey !== errorKey) {
32
- errorKey = newErrorKey;
33
- errorListeners.forEach((fn) => fn(errorKey));
34
- }
35
- if (loading) {
36
- loadings.add(subKey);
37
- }
38
- else {
39
- loadings.delete(subKey);
40
- }
41
- const newLoading = !!loadings.size;
42
- if (newLoading !== isLoading) {
43
- isLoading = newLoading;
44
- loadingListeners.forEach((fn) => fn(isLoading));
45
- }
46
- }
47
- exports.updateMeta = updateMeta;
48
- function useLoading() {
49
- const [, setLoading] = (0, react_1.useState)(isLoading);
50
- loadingListeners.add(setLoading);
51
- (0, react_1.useEffect)(() => {
52
- return () => {
53
- loadingListeners.delete(setLoading);
54
- };
55
- }, []);
56
- return { loading: isLoading };
57
- }
58
- exports.useLoading = useLoading;
59
- function useError() {
60
- const [, setError] = (0, react_1.useState)(errorKey);
61
- errorListeners.add(setError);
62
- (0, react_1.useEffect)(() => {
63
- return () => {
64
- errorListeners.delete(setError);
65
- };
66
- }, []);
67
- return { error: errorCnt ? lastError : null, errors: Object.values(errors) };
68
- }
69
- exports.useError = useError;
70
- //# sourceMappingURL=meta.js.map
package/dist/meta.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"meta.js","sourceRoot":"","sources":["../src/meta.ts"],"names":[],"mappings":";;;AAAA,iCAA2C;AAE3C,MAAM,MAAM,GAAG,EAAE,CAAA;AACjB,MAAM,cAAc,GAAG,IAAI,GAAG,EAAE,CAAA;AAChC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAE,CAAA;AAC1B,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAE,CAAA;AAElC,IAAI,SAAS,GAAG,KAAK,CAAA;AACrB,IAAI,SAAS,GAAG,EAAE,CAAA;AAClB,IAAI,QAAQ,GAAG,CAAC,CAAA;AAChB,IAAI,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAA;AAEnC,SAAgB,UAAU,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK;IAC/C,IAAI,KAAK,EAAE;QACT,SAAS,GAAG,KAAK,CAAA;QACjB,IAAI,MAAM,IAAI,MAAM,EAAE;YACpB,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAA;SACvB;aAAM;YACL,MAAM,CAAC,MAAM,CAAC,GAAG,KAAK,CAAA;YACtB,QAAQ,EAAE,CAAA;SACX;KACF;SAAM;QACL,IAAI,MAAM,IAAI,MAAM,EAAE;YACpB,QAAQ,EAAE,CAAA;YACV,OAAO,MAAM,CAAC,MAAM,CAAC,CAAA;SACtB;KACF;IAED,MAAM,WAAW,GAAG,QAAQ,GAAG,SAAS,CAAA;IACxC,IAAI,WAAW,KAAK,QAAQ,EAAE;QAC5B,QAAQ,GAAG,WAAW,CAAA;QACtB,cAAc,CAAC,OAAO,CAAC,CAAC,EAAY,EAAE,EAAE,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAA;KACvD;IAED,IAAI,OAAO,EAAE;QACX,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;KACrB;SAAM;QACL,QAAQ,C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Data, Loading } from './types';
2
- export declare function resultReducer(state: {
3
- data: Data;
4
- error?: Error;
5
- loading: Loading;
6
- checksum: number;
7
- }, action: {
8
- merge?: Data;
9
- data?: Data;
10
- error?: Error;
11
- loading?: Loading;
12
- checksum?: number;
13
- }): {
14
- data: Data;
15
- error?: Error;
16
- loading: Loading;
17
- checksum: number;
18
- };
package/dist/reducer.js DELETED
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resultReducer = void 0;
4
- function resultReducer(state, action) {
5
- if (action.error) {
6
- state.error = action.error;
7
- }
8
- if (action.data) {
9
- state.checksum = action.checksum || 0;
10
- state.data = action.data;
11
- state.loading = false;
12
- if (state.error) {
13
- delete state.error;
14
- }
15
- }
16
- if (action.merge) {
17
- state.checksum = action.checksum || 0;
18
- if (!state.data) {
19
- state.data = action.merge;
20
- }
21
- else {
22
- Object.assign(state.data, action.merge);
23
- }
24
- state.loading = false;
25
- if (state.error) {
26
- delete state.error;
27
- }
28
- }
29
- if (action.loading) {
30
- state.loading = action.loading;
31
- }
32
- return { ...state };
33
- }
34
- exports.resultReducer = resultReducer;
35
- //# sourceMappingURL=reducer.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"reducer.js","sourceRoot":"","sources":["../src/reducer.ts"],"names":[],"mappings":";;;AAEA,SAAgB,aAAa,CAC3B,KAAwE,EACxE,MAMC;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAA;KAC3B;IACD,IAAI,MAAM,CAAC,IAAI,EAAE;QACf,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAA;QACrC,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAA;QACxB,KAAK,CAAC,OAAO,GAAG,KAAK,CAAA;QACrB,IAAI,KAAK,CAAC,KAAK,EAAE;YACf,OAAO,KAAK,CAAC,KAAK,CAAA;SACnB;KACF;IAED,IAAI,MAAM,CAAC,KAAK,EAAE;QAChB,KAAK,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,CAAC,CAAA;QACrC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE;YACf,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAA;SAC1B;aAAM;YACL,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;SACxC;QACD,KAAK,CAAC,OAAO,GAAG,KAAK,CAAA;QACrB,IAAI,KAAK,CAAC,KAAK,EAAE;YACf,OAAO,KAAK,CAAC,KAAK,CAAA;SACnB;KACF;IAED,IAAI,MAAM,CAAC,OAAO,EAAE;QAClB,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAA;KAC/B;IACD,OAAO,EAAE,GAAG,KAAK,EAAE,CAAA;AACrB,CAAC;AAvCD,sCAuCC"}
package/dist/types.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { GenericObject } from '@based/client';
2
- export declare type Data = GenericObject;
3
- export declare type Loading = boolean;
package/dist/types.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/src/clients.ts DELETED
@@ -1,161 +0,0 @@
1
- import React, {
2
- createContext,
3
- FunctionComponent,
4
- ReactNode,
5
- useContext,
6
- useEffect,
7
- useReducer,
8
- } from 'react'
9
-
10
- import based, { Based, BasedOpts } from '@based/client'
11
-
12
- import { genOptsId } from './genOptsId'
13
-
14
- export type CreateClient = (
15
- selector: string | (BasedOpts & { key?: string })
16
- ) => Based
17
-
18
- const newClientListeners: Set<() => void> = new Set()
19
-
20
- interface BasedContextType {
21
- clients: { [key: string]: Based }
22
- createClient?: CreateClient
23
- removeClient: (
24
- selector: string | (BasedOpts & { key?: string }) | Based
25
- ) => void
26
- }
27
-
28
- export const BasedContext = createContext<BasedContextType>({
29
- clients: {},
30
- // eslint-disable-next-line
31
- removeClient: (...args: any) => {},
32
- })
33
-
34
- export const defaultCreateClient: CreateClient = (selector) => {
35
- if (typeof selector === 'object') {
36
- if (
37
- typeof process !== 'undefined' &&
38
- process.env.CLUSTER &&
39
- process.env.CLUSTER.startsWith('local') &&
40
- !selector.cluster
41
- ) {
42
- selector.cluster = process.env.CLUSTER
43
- }
44
- return based(selector)
45
- } else {
46
- // default
47
- console.error('Cannot create client from ' + selector)
48
- }
49
- }
50
-
51
- export const Provider: FunctionComponent<{
52
- client?: Based
53
- clients?: { [key: string]: Based }
54
- children: ReactNode
55
- createClient?: CreateClient
56
- }> = ({ client, children, clients, createClient }) => {
57
- if (!clients && client) {
58
- clients = {
59
- default: client,
60
- }
61
- } else if (clients && client) {
62
- clients.default = client
63
- }
64
-
65
- const ctx = React.createElement(
66
- BasedContext.Provider,
67
- {
68
- value: {
69
- clients,
70
- createClient: createClient || defaultCreateClient,
71
- removeClient: (selector) => {
72
- if (selector instanceof Based) {
73
- for (const cl in clients) {
74
- if (clients[cl] === selector) {
75
- selector = cl
76
- break
77
- }
78
- }
79
- if (typeof selector !== 'string') {
80
- console.error('Cannot find client to remove from ctx', selector)
81
- return
82
- }
83
- } else if (typeof selector !== 'string') {
84
- selector = genOptsId(selector)
85
- }
86
- // @ts-ignore
87
- if (clients[selector]) {
88
- // @ts-ignore
89
- clients[selector].disconnect()
90
- // @ts-ignore
91
- delete clients[selector]
92
- newClientListeners.forEach((fn) => fn())
93
- }
94
- },
95
- },
96
- },
97
- children
98
- )
99
-
100
- return ctx
101
- }
102
-
103
- export const useBasedContext = () => {
104
- return useContext(BasedContext)
105
- }
106
-
107
- function forceUpdate(state: number) {
108
- return state + 1
109
- }
110
-
111
- export const useClients = (): Based[] => {
112
- const ctx = useBasedContext()
113
- const [, update] = useReducer(forceUpdate, 0)
114
-
115
- useEffect(() => {
116
- let timer
117
- const fn = () => {
118
- timer = setTimeout(update, 0)
119
- }
120
- newClientListeners.add(fn)
121
- return () => {
122
- newClientListeners.delete(fn)
123
- clearTimeout(timer)
124
- }
125
- }, [])
126
-
127
- return Object.values(ctx.clients)
128
- }
129
-
130
- export const useClient = (
131
- selector: string | (BasedOpts & { key?: string }) = 'default'
132
- ) => {
133
- const basedCtx = useContext(BasedContext)
134
-
135
- if (typeof selector === 'object') {
136
- if (!(selector.env && selector.project && selector.org)) {
137
- return
138
- }
139
- }
140
-
141
- let key: string
142
-
143
- if (typeof selector === 'string') {
144
- key = selector
145
- } else {
146
- key = selector.key || genOptsId(selector)
147
- }
148
-
149
- let client: Based = basedCtx.clients[key]
150
-
151
- if (!client && basedCtx.createClient) {
152
- client = basedCtx.createClient(selector)
153
-
154
- if (client) {
155
- basedCtx.clients[key] = client
156
- newClientListeners.forEach((fn) => fn())
157
- }
158
- }
159
-
160
- return client
161
- }
@@ -1,182 +0,0 @@
1
- import {
2
- addSubscriber,
3
- removeSubscriber,
4
- generateSubscriptionId,
5
- } from '@based/client'
6
- import React, { useRef, useState, useEffect } from 'react'
7
- import { FixedSizeList as List } from 'react-window'
8
- import { useData, useClient } from '../..'
9
-
10
- const addScrollSub = (client, subId, payload, offset, current, setChecksum) => {
11
- const [, subscriberId] = addSubscriber(
12
- client.client,
13
- payload,
14
- ({ items }, checksum) => {
15
- for (let i = 0; i < items.length; i++) {
16
- current.items[i + offset] = items[i]
17
- }
18
- setChecksum(`${offset}-${checksum}`)
19
- },
20
- (err) => err && console.error(err),
21
- console.error,
22
- subId
23
- )
24
- return subscriberId
25
- }
26
-
27
- export function InfiniteList({
28
- query,
29
- delay = 100,
30
- itemSize = 50,
31
- height = 400,
32
- limit = Math.ceil(height / itemSize),
33
- treshold = 0,
34
- target = 'root',
35
- ...props
36
- }) {
37
- const blockHeight = itemSize * limit
38
- const client = useClient()
39
- const [offset, setOffset] = useState(0)
40
- const [blocks, setBlocks] = useState(() => {
41
- let blocks = Math.ceil(height / blockHeight)
42
- if (treshold) {
43
- blocks += Math.ceil(
44
- (height / itemSize + treshold - blocks * limit) / limit
45
- )
46
- }
47
- return blocks
48
- })
49
-
50
- const [, setChecksum] = useState()
51
- const { current } = useRef({
52
- offset,
53
- blocks,
54
- scroll: 0,
55
- items: [],
56
- timer: null,
57
- subs: {},
58
- })
59
- const {
60
- data: { itemCount },
61
- } = useData({
62
- itemCount: {
63
- $aggregate: {
64
- $function: 'count',
65
- ...query().$list.$find,
66
- },
67
- },
68
- })
69
-
70
- useEffect(() => {
71
- if (client) {
72
- return () => {
73
- const { subs } = current
74
- current.subs = {}
75
- setTimeout(() => {
76
- for (const subId in subs) {
77
- const subscriberId = current.subs[subId]
78
- removeSubscriber(client.client, Number(subId), subscriberId)
79
- }
80
- })
81
- }
82
- }
83
- }, [client, current])
84
-
85
- useEffect(() => {
86
- if (client) {
87
- const subs = {}
88
- let i = blocks
89
-
90
- while (i--) {
91
- const start = offset + limit * i
92
- const payload = {
93
- $id: target,
94
- items: query(start, limit),
95
- }
96
- const subId = generateSubscriptionId(payload)
97
- subs[subId] =
98
- current.subs[subId] ||
99
- addScrollSub(client, subId, payload, start, current, setChecksum)
100
- }
101
-
102
- for (const subId in current.subs) {
103
- if (!(subId in subs)) {
104
- const subscriberId = current.subs[subId]
105
- removeSubscriber(client.client, Number(subId), subscriberId)
106
- }
107
- }
108
-
109
- current.subs = subs
110
- }
111
- }, [target, client, offset, blocks, query, current]) // dont include limit
112
-
113
- useEffect(update, [
114
- blockHeight,
115
- delay,
116
- height,
117
- itemSize,
118
- current,
119
- limit,
120
- treshold,
121
- ])
122
-
123
- if (!itemCount || !current.items.length) {
124
- return null
125
- }
126
-
127
- return (
128
- <List
129
- {...props}
130
- height={height}
131
- itemSize={itemSize}
132
- itemData={current.items}
133
- itemCount={itemCount}
134
- onScroll={({ scrollOffset }) => {
135
- current.scroll = scrollOffset
136
- update()
137
- }}
138
- />
139
- )
140
-
141
- function update() {
142
- const start = Math.max(0, current.scroll / itemSize - treshold)
143
- const end = (current.scroll + height) / itemSize
144
- const newOffset = start - (start % limit)
145
- let newBlocks = Math.ceil(
146
- height / blockHeight + (current.scroll % blockHeight) / blockHeight
147
- )
148
-
149
- if (treshold) {
150
- const newLength = newOffset + limit * newBlocks
151
- newBlocks += Math.max(0, Math.ceil((end + treshold - newLength) / limit))
152
- }
153
-
154
- if (newOffset !== current.offset || newBlocks !== current.blocks) {
155
- current.offset = newOffset
156
- current.blocks = newBlocks
157
-
158
- const set = () => {
159
- setOffset(current.offset)
160
- setBlocks(current.blocks)
161
- }
162
-
163
- if (current.timer) {
164
- clearTimeout(current.timer)
165
- } else {
166
- set()
167
- }
168
-
169
- current.timer = setTimeout(() => {
170
- current.timer = null
171
- set()
172
- }, delay)
173
- } else if (current.timer) {
174
- clearTimeout(current.timer)
175
- current.timer = setTimeout(() => {
176
- current.timer = null
177
- setOffset(current.offset)
178
- setBlocks(current.blocks)
179
- }, delay)
180
- }
181
- }
182
- }
package/src/genOptsId.ts DELETED
@@ -1,13 +0,0 @@
1
- import { BasedOpts } from '@based/client'
2
-
3
- export const genOptsId = (opts: BasedOpts & { key?: string }): string => {
4
- if (!opts) {
5
- return
6
- }
7
- if (opts.key) {
8
- return opts.key
9
- }
10
- return `${opts.env}_${opts.project}_${opts.org}_${opts.cluster || ''}_${
11
- opts.name || ''
12
- }`
13
- }