muya 2.4.7 → 2.4.9
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/esm/sqlite/use-sqlite.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useCallback as f,useLayoutEffect as P,useReducer as R,useRef as D}from"react";import{DEFAULT_PAGE_SIZE as O}from"./table";const T=1e4;function K(i,w={},k=[]){const{select:m,pageSize:q=O}=w,e=D(),[,I]=R(c=>c+1,0),n=D(new Map),x=D(),L=f(()=>{const{select:c,...
|
|
1
|
+
import{useCallback as f,useLayoutEffect as P,useReducer as R,useRef as D}from"react";import{DEFAULT_PAGE_SIZE as O}from"./table";const T=1e4;function K(i,w={},k=[]){const{select:m,pageSize:q=O}=w,e=D(null),[,I]=R(c=>c+1,0),n=D(new Map),x=D(null),L=f(()=>{const{select:c,...l}=w;x.current=i.search({select:(o,a)=>({doc:o,meta:a}),...l})},[i,...k]),d=f(()=>{e.current=[],n.current.clear(),L()},[L]),y=f(async c=>{e.current===null&&(e.current=[]),c===!0&&d();const{current:l}=x;if(!l)return!0;let o=!1;for(let a=0;a<q;a++){const s=await l.next();if(s.done){x.current=null,o=!0;break}if(n.current.has(s.value.meta.key)){a+=-1;continue}e.current.push(m?m(s.value.doc):s.value.doc),n.current.set(s.value.meta.key,e.current.length-1)}return e.current=[...e.current],o},[]),p=f(async()=>{const c=await y(!1);return I(),c},[y]);P(()=>{const c=i.subscribe(async l=>{const{mutations:o,removedAll:a}=l;if(a&&d(),!o)return;const s=e.current?.length??0;let g=s,h=!1;const S=new Set;for(const u of o){const{key:r,op:b}=u;switch(b){case"insert":{g+=1;break}case"delete":{if(e.current&&e.current.length>0&&n.current.has(r)){const t=n.current.get(r);if(t===void 0)break;S.add(t),h=!0}break}case"update":{if(n.current.has(r)){const t=n.current.get(r);t!==void 0&&e.current&&(e.current[t]=await i.get(r,m),e.current=[...e.current],h=!0)}else{const t=await i.get(r,m);t&&(e.current=[...e.current??[],t],n.current.set(r,e.current.length-1),h=!0)}break}}}if(S.size>0&&e.current&&e.current.length>0){const u=new Map;e.current=e.current?.filter((b,t)=>!S.has(t));let r=0;for(const[b,t]of n.current)S.has(t)||(u.set(b,r),r++);n.current=u}const A=s!==g;if(A||h){if(A){await y(!0);let u=0;for(;(e.current?.length??0)<g&&u<T;)await y(!1),u++;u===T&&console.warn("Reached maximum iterations in fillNextPage loop. Possible duplicate or data issue.")}I()}});return()=>{c()}},[i]),P(()=>{d(),p()},k);const v=f(async()=>{d(),await p()},[p,d]);return[e.current,{nextPage:p,reset:v,keysIndex:n.current}]}export{K as useSqliteValue};
|
package/package.json
CHANGED
package/src/sqlite/use-sqlite.ts
CHANGED
|
@@ -39,13 +39,13 @@ export function useSqliteValue<Document extends DocType, Selected = Document>(
|
|
|
39
39
|
state: SyncTable<Document>,
|
|
40
40
|
options: UseSearchOptions<Document, Selected> = {},
|
|
41
41
|
deps: DependencyList = [],
|
|
42
|
-
): [(undefined extends Selected ? Document[] : Selected[]) |
|
|
42
|
+
): [(undefined extends Selected ? Document[] : Selected[]) | null, SqLiteActions] {
|
|
43
43
|
const { select, pageSize = DEFAULT_PAGE_SIZE } = options
|
|
44
44
|
|
|
45
|
-
const itemsRef = useRef<
|
|
45
|
+
const itemsRef = useRef<null | (Document | Selected)[]>(null)
|
|
46
46
|
const [, rerender] = useReducer((c: number) => c + 1, 0)
|
|
47
47
|
const keysIndex = useRef(new Map<Key, number>())
|
|
48
|
-
const iteratorRef = useRef<AsyncIterableIterator<{ doc: Document; meta: { key: Key } }
|
|
48
|
+
const iteratorRef = useRef<AsyncIterableIterator<{ doc: Document; meta: { key: Key } }> | null>(null)
|
|
49
49
|
|
|
50
50
|
const updateIterator = useCallback(() => {
|
|
51
51
|
// eslint-disable-next-line sonarjs/no-unused-vars
|
|
@@ -61,7 +61,7 @@ export function useSqliteValue<Document extends DocType, Selected = Document>(
|
|
|
61
61
|
}, [updateIterator])
|
|
62
62
|
|
|
63
63
|
const fillNextPage = useCallback(async (shouldReset: boolean) => {
|
|
64
|
-
if (itemsRef.current ===
|
|
64
|
+
if (itemsRef.current === null) {
|
|
65
65
|
itemsRef.current = []
|
|
66
66
|
}
|
|
67
67
|
if (shouldReset === true) {
|
|
@@ -77,7 +77,7 @@ export function useSqliteValue<Document extends DocType, Selected = Document>(
|
|
|
77
77
|
for (let index = 0; index < pageSize; index++) {
|
|
78
78
|
const result = await iterator.next()
|
|
79
79
|
if (result.done) {
|
|
80
|
-
iteratorRef.current =
|
|
80
|
+
iteratorRef.current = null
|
|
81
81
|
isDone = true
|
|
82
82
|
break
|
|
83
83
|
}
|
|
@@ -204,7 +204,7 @@ export function useSqliteValue<Document extends DocType, Selected = Document>(
|
|
|
204
204
|
}, [nextPage, reset])
|
|
205
205
|
|
|
206
206
|
return [itemsRef.current, { nextPage, reset: resetCb, keysIndex: keysIndex.current }] as [
|
|
207
|
-
(undefined extends Selected ? Document[] : Selected[]) |
|
|
207
|
+
(undefined extends Selected ? Document[] : Selected[]) | null,
|
|
208
208
|
SqLiteActions,
|
|
209
209
|
]
|
|
210
210
|
}
|
|
@@ -29,4 +29,4 @@ export interface UseSearchOptions<Document extends DocType, Selected = Document>
|
|
|
29
29
|
* @param deps Dependency list to control when to re-run the search and reset the iterator.
|
|
30
30
|
* @returns A tuple containing the current list of results and an object with actions to manage pagination and resetting.
|
|
31
31
|
*/
|
|
32
|
-
export declare function useSqliteValue<Document extends DocType, Selected = Document>(state: SyncTable<Document>, options?: UseSearchOptions<Document, Selected>, deps?: DependencyList): [(undefined extends Selected ? Document[] : Selected[]) |
|
|
32
|
+
export declare function useSqliteValue<Document extends DocType, Selected = Document>(state: SyncTable<Document>, options?: UseSearchOptions<Document, Selected>, deps?: DependencyList): [(undefined extends Selected ? Document[] : Selected[]) | null, SqLiteActions];
|