muya 2.2.1 → 2.2.2

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 R}from"../utils/shallow";import{selectSql as M}from"./select-sql";import{createTable as O,DEFAULT_STEP_SIZE as v}from"./table/table";const f=P();let C=0;function E(){return C++}function N(g){const T=E();function l(e){return`state-${T}-search-${e}`}let m;async function o(){return m||(m=await O(g)),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=v}=a;if(!t)return!1;const r=[];for(let d=0;d<s;d++){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||R(n.items,r)?!1:(n.items=[...n.items,...r],!0)}function b(e){const n=i.get(e);n&&n()}async function k(e){const n=await o(),t=c.get(e);if(!t)return;const{options:a}=t,s=n.search({...a,select:(r,{rowId:d})=>({document:r,rowId:d})});h.set(e,s),t.keys=new Set,t.items=[],await p(e,t)}async function S(e){await k(e),b(e)}function x(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 u(e){const n=new Set;for(const t of e){const a=x(t);for(const s of a)n.add(s)}for(const t of n){const a=l(t);f.schedule(a,{searchId:t})}}const w=new Set;function D(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 u([t]),t},async batchSet(e){const t=await(await o()).batchSet(e);return await u(t),t},async delete(e){const t=await(await o()).delete(e);return t&&await u([t]),t},async deleteBy(e){const t=await(await o()).deleteBy(e);return await u(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=D(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 w.add(a),i.has(e)||i.set(e,n),()=>{i.delete(e),a(),c.delete(e)}},getSnapshot(e){return D(e).items},refresh:S,destroy(){for(const e of w)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 M(I,e)}};return I}export{N as createSqliteState};
1
+ import{createScheduler as P}from"../scheduler";import{shallow as O}from"../utils/shallow";import{selectSql as R}from"./select-sql";import{createTable as M,DEFAULT_STEP_SIZE as v}from"./table/table";const f=P();let B=0;function C(){return B++}function N(g){const k=C();function l(e){return`state-${k}-search-${e}`}let m;async function o(){if(!m){const{backend:e,...n}=g,t=await e;m=await M({...n,backend:t})}return m}const c=new Map,i=new Map,h=new Map;async function b(e,n){const t=h.get(e),{options:a={}}=n,{stepSize:s=v}=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 p(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 b(e,t)}async function S(e){await x(e),p(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 b(e,n);return t&&p(e),t}return!1},select(e){return R(I,e)}};return I}export{N as createSqliteState};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muya",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "author": "samuel.gjabel@gmail.com",
5
5
  "repository": "https://github.com/samuelgjabel/muya",
6
6
  "main": "cjs/index.js",
@@ -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'
@@ -45,7 +46,10 @@ interface DataItems<Document extends DocType> {
45
46
  options?: SearchOptions<Document, unknown>
46
47
  }
47
48
 
48
- export function createSqliteState<Document extends DocType>(options: DbOptions<Document>): SyncTable<Document> {
49
+ export interface ReactDbOptions<Document extends DocType> extends Omit<DbOptions<Document>, 'backend'> {
50
+ readonly backend: Backend | Promise<Backend>
51
+ }
52
+ export function createSqliteState<Document extends DocType>(options: ReactDbOptions<Document>): SyncTable<Document> {
49
53
  // const table = await createTable<Document>(options)
50
54
 
51
55
  const id = getStateId()
@@ -56,7 +60,9 @@ export function createSqliteState<Document extends DocType>(options: DbOptions<D
56
60
  let cachedTable: Table<Document> | undefined
57
61
  async function getTable() {
58
62
  if (!cachedTable) {
59
- cachedTable = await createTable<Document>(options)
63
+ const { backend, ...rest } = options
64
+ const awaitedBackend = await backend
65
+ cachedTable = await createTable<Document>({ ...rest, backend: awaitedBackend })
60
66
  }
61
67
  return cachedTable
62
68
  }
@@ -1,4 +1,5 @@
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;
@@ -20,5 +21,8 @@ export interface SyncTable<Document extends DocType> {
20
21
  readonly next: (searchId: SearchId) => Promise<boolean>;
21
22
  readonly select: <Params extends unknown[]>(compute: (...args: Params) => SearchOptions<Document>) => CreateState<Document, Params>;
22
23
  }
23
- export declare function createSqliteState<Document extends DocType>(options: DbOptions<Document>): SyncTable<Document>;
24
+ export interface ReactDbOptions<Document extends DocType> extends Omit<DbOptions<Document>, 'backend'> {
25
+ readonly backend: Backend | Promise<Backend>;
26
+ }
27
+ export declare function createSqliteState<Document extends DocType>(options: ReactDbOptions<Document>): SyncTable<Document>;
24
28
  export {};