muya 2.2.3 → 2.2.4
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createScheduler as P}from"../scheduler";import{shallow as
|
|
1
|
+
import{createScheduler as P}from"../scheduler";import{shallow as O}from"../utils/shallow";import{selectSql as R}from"./select-sql";import{createTable as v,DEFAULT_STEP_SIZE as M}from"./table/table";const f=P();let C=0;function B(){return C++}function L(g){const k=B();function l(e){return`state-${k}-search-${e}`}let m;async function o(){if(!m){const{backend:e,...n}=g,t=e instanceof Promise?await e:e;m=await v({backend:t,...n})}return m}const c=new Map,i=new Map,h=new Map;async function p(e,n){const t=h.get(e),{options:a={}}=n,{stepSize:s=M}=a;if(!t)return!1;const r=[];for(let u=0;u<s;u++){const y=await t.next();if(y.done){h.delete(e);break}r.push(y.value.document),n.keys.add(String(y.value.rowId))}return r.length===0||O(n.items,r)?!1:(n.items=[...n.items,...r],!0)}function b(e){const n=i.get(e);n&&n()}async function x(e){const n=await o(),t=c.get(e);if(!t)return;const{options:a}=t,s=n.search({...a,select:(r,{rowId:u})=>({document:r,rowId:u})});h.set(e,s),t.keys=new Set,t.items=[],await p(e,t)}async function S(e){await x(e),b(e)}function T(e){const{key:n,op:t}=e,a=new Set;for(const[s,{keys:r}]of c)switch(t){case"delete":case"update":{r.has(String(n))&&a.add(s);break}case"insert":{a.add(s);break}}return a}async function d(e){const n=new Set;for(const t of e){const a=T(t);for(const s of a)n.add(s)}for(const t of n){const a=l(t);f.schedule(a,{searchId:t})}}const D=new Set;function w(e,n){c.has(e)||(c.set(e,{items:[],options:n,keys:new Set}),n&&S(e));const t=c.get(e);return n&&(t.options=n),t}const I={async set(e){const t=await(await o()).set(e);return await d([t]),t},async batchSet(e){const t=await(await o()).batchSet(e);return await d(t),t},async delete(e){const t=await(await o()).delete(e);return t&&await d([t]),t},async deleteBy(e){const t=await(await o()).deleteBy(e);return await d(t),t},async get(e,n){return(await o()).get(e,n)},async*search(e={}){const n=await o();for await(const t of n.search(e))yield t},async count(e){return await(await o()).count(e)},updateSearchOptions(e,n){const t=w(e,n);t.options=n;const a=l(e);f.schedule(a,{searchId:e})},subscribe(e,n){const t=l(e),a=f.add(t,{onScheduleDone(){S(e)}});return D.add(a),i.has(e)||i.set(e,n),()=>{i.delete(e),a(),c.delete(e)}},getSnapshot(e){return w(e).items},refresh:S,destroy(){for(const e of D)e();c.clear(),i.clear()},async next(e){const n=c.get(e);if(n){const t=await p(e,n);return t&&b(e),t}return!1},select(e){return R(I,e)}};return I}export{L as createSqliteState};
|
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { createScheduler } from '../scheduler'
|
|
5
5
|
import { shallow } from '../utils/shallow'
|
|
6
6
|
import { selectSql, type CreateState } from './select-sql'
|
|
7
|
+
import type { Backend } from './table'
|
|
7
8
|
import { createTable, DEFAULT_STEP_SIZE } from './table/table'
|
|
8
9
|
import type { DbOptions, DocType, Key, MutationResult, SearchOptions, Table } from './table/table.types'
|
|
9
10
|
import type { Where } from './table/where'
|
|
@@ -16,6 +17,10 @@ function getStateId() {
|
|
|
16
17
|
return stateId++
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
export interface CreateSqliteOptions<Document extends DocType> extends Omit<DbOptions<Document>, 'backend'> {
|
|
21
|
+
readonly backend: Backend | Promise<Backend>
|
|
22
|
+
}
|
|
23
|
+
|
|
19
24
|
export interface SyncTable<Document extends DocType> {
|
|
20
25
|
// readonly registerSearch: <Selected = Document>(searchId: SearchId, options: SearchOptions<Document, Selected>) => () => void
|
|
21
26
|
readonly updateSearchOptions: <Selected = Document>(searchId: SearchId, options: SearchOptions<Document, Selected>) => void
|
|
@@ -45,9 +50,7 @@ interface DataItems<Document extends DocType> {
|
|
|
45
50
|
options?: SearchOptions<Document, unknown>
|
|
46
51
|
}
|
|
47
52
|
|
|
48
|
-
export function createSqliteState<Document extends DocType>(options:
|
|
49
|
-
// const table = await createTable<Document>(options)
|
|
50
|
-
|
|
53
|
+
export function createSqliteState<Document extends DocType>(options: CreateSqliteOptions<Document>): SyncTable<Document> {
|
|
51
54
|
const id = getStateId()
|
|
52
55
|
function getScheduleId(searchId: SearchId) {
|
|
53
56
|
return `state-${id}-search-${searchId}`
|
|
@@ -56,7 +59,9 @@ export function createSqliteState<Document extends DocType>(options: DbOptions<D
|
|
|
56
59
|
let cachedTable: Table<Document> | undefined
|
|
57
60
|
async function getTable() {
|
|
58
61
|
if (!cachedTable) {
|
|
59
|
-
|
|
62
|
+
const { backend, ...rest } = options
|
|
63
|
+
const resolvedBackend = backend instanceof Promise ? await backend : backend
|
|
64
|
+
cachedTable = await createTable<Document>({ backend: resolvedBackend, ...rest })
|
|
60
65
|
}
|
|
61
66
|
return cachedTable
|
|
62
67
|
}
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { type CreateState } from './select-sql';
|
|
2
|
+
import type { Backend } from './table';
|
|
2
3
|
import type { DbOptions, DocType, Key, MutationResult, SearchOptions } from './table/table.types';
|
|
3
4
|
import type { Where } from './table/where';
|
|
4
5
|
type SearchId = string;
|
|
6
|
+
export interface CreateSqliteOptions<Document extends DocType> extends Omit<DbOptions<Document>, 'backend'> {
|
|
7
|
+
readonly backend: Backend | Promise<Backend>;
|
|
8
|
+
}
|
|
5
9
|
export interface SyncTable<Document extends DocType> {
|
|
6
10
|
readonly updateSearchOptions: <Selected = Document>(searchId: SearchId, options: SearchOptions<Document, Selected>) => void;
|
|
7
11
|
readonly subscribe: (searchId: SearchId, listener: () => void) => () => void;
|
|
@@ -20,5 +24,5 @@ export interface SyncTable<Document extends DocType> {
|
|
|
20
24
|
readonly next: (searchId: SearchId) => Promise<boolean>;
|
|
21
25
|
readonly select: <Params extends unknown[]>(compute: (...args: Params) => SearchOptions<Document>) => CreateState<Document, Params>;
|
|
22
26
|
}
|
|
23
|
-
export declare function createSqliteState<Document extends DocType>(options:
|
|
27
|
+
export declare function createSqliteState<Document extends DocType>(options: CreateSqliteOptions<Document>): SyncTable<Document>;
|
|
24
28
|
export {};
|