muya 2.1.0 → 2.1.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.
Files changed (55) hide show
  1. package/cjs/index.js +1 -1
  2. package/esm/create-state.js +1 -1
  3. package/esm/create.js +1 -1
  4. package/esm/debug/development-tools.js +1 -1
  5. package/esm/scheduler.js +1 -1
  6. package/esm/select.js +1 -1
  7. package/esm/sqlite/__tests__/create-sqlite-state.test.js +1 -0
  8. package/esm/sqlite/__tests__/map-deque.test.js +1 -0
  9. package/esm/sqlite/__tests__/table.test.js +1 -0
  10. package/esm/sqlite/__tests__/use-sqlite-state.test.js +1 -0
  11. package/esm/sqlite/create-sqlite-state.js +1 -0
  12. package/esm/sqlite/table/backend.js +1 -0
  13. package/esm/sqlite/table/bun-backend.js +1 -0
  14. package/esm/sqlite/table/map-deque.js +1 -0
  15. package/esm/sqlite/table/table.js +10 -0
  16. package/esm/sqlite/table/table.types.js +0 -0
  17. package/esm/sqlite/table/where.js +1 -0
  18. package/esm/sqlite/use-sqlite-value.js +1 -0
  19. package/esm/use-value.js +1 -1
  20. package/esm/utils/common.js +1 -1
  21. package/package.json +4 -12
  22. package/src/__tests__/scheduler.test.tsx +2 -2
  23. package/src/__tests__/test-utils.ts +1 -0
  24. package/src/create-state.ts +3 -2
  25. package/src/create.ts +22 -24
  26. package/src/debug/development-tools.ts +1 -1
  27. package/src/scheduler.ts +15 -7
  28. package/src/select.ts +15 -17
  29. package/src/sqlite/__tests__/create-sqlite-state.test.ts +81 -0
  30. package/src/sqlite/__tests__/map-deque.test.ts +61 -0
  31. package/src/sqlite/__tests__/table.test.ts +142 -0
  32. package/src/sqlite/__tests__/use-sqlite-state.test.ts +213 -0
  33. package/src/sqlite/create-sqlite-state.ts +256 -0
  34. package/src/sqlite/table/backend.ts +21 -0
  35. package/src/sqlite/table/bun-backend.ts +38 -0
  36. package/src/sqlite/table/map-deque.ts +29 -0
  37. package/src/sqlite/table/table.ts +200 -0
  38. package/src/sqlite/table/table.types.ts +55 -0
  39. package/src/sqlite/table/where.ts +267 -0
  40. package/src/sqlite/use-sqlite-value.ts +76 -0
  41. package/src/types.ts +1 -0
  42. package/src/use-value.ts +6 -4
  43. package/src/utils/common.ts +6 -2
  44. package/types/create.d.ts +3 -3
  45. package/types/scheduler.d.ts +12 -3
  46. package/types/sqlite/create-sqlite-state.d.ts +22 -0
  47. package/types/sqlite/table/backend.d.ts +20 -0
  48. package/types/sqlite/table/bun-backend.d.ts +2 -0
  49. package/types/sqlite/table/map-deque.d.ts +5 -0
  50. package/types/sqlite/table/table.d.ts +3 -0
  51. package/types/sqlite/table/table.types.d.ts +52 -0
  52. package/types/sqlite/table/where.d.ts +32 -0
  53. package/types/sqlite/use-sqlite-value.d.ts +21 -0
  54. package/types/types.d.ts +1 -0
  55. package/types/utils/common.d.ts +2 -2
package/src/use-value.ts CHANGED
@@ -1,16 +1,18 @@
1
- import { useDebugValue, useSyncExternalStore } from 'react'
1
+ import { useDebugValue } from 'react'
2
2
  import { EMPTY_SELECTOR, type GetState } from './types'
3
3
  import { isError, isPromise } from './utils/is'
4
+ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector'
4
5
 
5
6
  export function useValue<T, S>(
6
7
  state: GetState<T>,
7
8
  selector: (stateValue: Awaited<T>) => S = EMPTY_SELECTOR,
8
9
  ): undefined extends S ? Awaited<T> : S {
9
10
  const { emitter } = state
10
- const value = useSyncExternalStore<S>(
11
+ const value = useSyncExternalStoreWithSelector<T, S>(
11
12
  state.emitter.subscribe,
12
- () => selector(emitter.getSnapshot() as Awaited<T>),
13
- () => selector((emitter.getInitialSnapshot ? emitter.getInitialSnapshot() : emitter.getSnapshot()) as Awaited<T>),
13
+ emitter.getSnapshot,
14
+ emitter.getInitialSnapshot,
15
+ selector as (stateValue: T) => S,
14
16
  )
15
17
  useDebugValue(value)
16
18
  if (isPromise(value)) {
@@ -1,4 +1,4 @@
1
- import type { Cache, IsEqual } from '../types'
1
+ import type { Cache, IsEqual, State } from '../types'
2
2
  import { isAbortError, isEqualBase, isPromise, isUndefined } from './is'
3
3
 
4
4
  export interface CancelablePromise<T> {
@@ -46,7 +46,11 @@ export function canUpdate<T>(cache: Cache<T>, isEqual: IsEqual<T> = isEqualBase)
46
46
  /**
47
47
  * Handle async updates for `create` and `select`
48
48
  */
49
- export function handleAsyncUpdate<T>(cache: Cache<T>, emit: () => void, value: T): T {
49
+ export function handleAsyncUpdate<T>(state: State<T>, value: T): T {
50
+ const {
51
+ cache,
52
+ emitter: { emit },
53
+ } = state
50
54
  if (!isPromise(value)) {
51
55
  return value
52
56
  }
package/types/create.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { DefaultValue, IsEqual, State } from './types';
2
- export declare const stateScheduler: {
3
- add<T>(id: number, option: import("./scheduler").SchedulerOptions<T>): () => void;
4
- schedule<T>(id: number, value: T): void;
2
+ export declare const STATE_SCHEDULER: {
3
+ add<T>(id: string | number | symbol, option: import("./scheduler").SchedulerOptions<T>): () => void;
4
+ schedule<T>(id: string | number | symbol, value: T): void;
5
5
  };
6
6
  /**
7
7
  * Create state from a default value.
@@ -1,11 +1,20 @@
1
1
  export declare const THRESHOLD = 0.2;
2
2
  export declare const THRESHOLD_ITEMS = 10;
3
3
  export declare const RESCHEDULE_COUNT = 0;
4
+ type ScheduleId = string | number | symbol;
4
5
  export interface SchedulerOptions<T> {
5
6
  readonly onResolveItem?: (item: T) => void;
6
- readonly onFinish: () => void;
7
+ readonly onScheduleDone: () => void | Promise<void>;
7
8
  }
9
+ /**
10
+ * A simple scheduler to batch updates and avoid blocking the main thread
11
+ * It uses a combination of time-based and count-based strategies to determine when to flush the queue.
12
+ * - Time-based: If the time taken to process the current batch is less than a threshold (THRESHOLD), it continues processing.
13
+ * - Count-based: If the ScheduleId of items in the batch exceeds a certain limit (THRESHOLD_ITEMS), it defers processing to the next microtask.
14
+ * @returns An object with methods to add listeners and schedule tasks.
15
+ */
8
16
  export declare function createScheduler(): {
9
- add<T>(id: number, option: SchedulerOptions<T>): () => void;
10
- schedule<T>(id: number, value: T): void;
17
+ add<T>(id: ScheduleId, option: SchedulerOptions<T>): () => void;
18
+ schedule<T>(id: ScheduleId, value: T): void;
11
19
  };
20
+ export {};
@@ -0,0 +1,22 @@
1
+ import type { DbOptions, DocType, Key, MutationResult, SearchOptions } from './table/table.types';
2
+ import type { Where } from './table/where';
3
+ type SearchId = string;
4
+ export interface SyncTable<Document extends DocType> {
5
+ readonly updateSearchOptions: <Selected = Document>(searchId: SearchId, options: SearchOptions<Document, Selected>) => void;
6
+ readonly subscribe: (searchId: SearchId, listener: () => void) => () => void;
7
+ readonly getSnapshot: (searchId: SearchId) => Document[];
8
+ readonly refresh: (searchId: SearchId) => Promise<void>;
9
+ readonly set: (document: Document) => Promise<MutationResult>;
10
+ readonly batchSet: (documents: Document[]) => Promise<MutationResult[]>;
11
+ readonly get: <Selected = Document>(key: Key, selector?: (document: Document) => Selected) => Promise<Selected | undefined>;
12
+ readonly delete: (key: Key) => Promise<MutationResult | undefined>;
13
+ readonly search: <Selected = Document>(options?: SearchOptions<Document, Selected>) => AsyncIterableIterator<Selected>;
14
+ readonly count: (options?: {
15
+ where?: Where<Document>;
16
+ }) => Promise<number>;
17
+ readonly deleteBy: (where: Where<Document>) => Promise<MutationResult[]>;
18
+ readonly destroy: () => void;
19
+ readonly next: (searchId: SearchId) => Promise<boolean>;
20
+ }
21
+ export declare function createSqliteState<Document extends DocType>(options: DbOptions<Document>): Promise<SyncTable<Document>>;
22
+ export {};
@@ -0,0 +1,20 @@
1
+ export declare const IN_MEMORY_DB = ":memory:";
2
+ export interface QueryResult {
3
+ /** The number of rows affected by the query. */
4
+ rowsAffected: number;
5
+ /**
6
+ * The last inserted `id`.
7
+ *
8
+ * This value is not set for Postgres databases. If the
9
+ * last inserted id is required on Postgres, the `select` function
10
+ * must be used, with a `RETURNING` clause
11
+ * (`INSERT INTO todos (title) VALUES ($1) RETURNING id`).
12
+ */
13
+ lastInsertId?: number;
14
+ }
15
+ export interface Backend {
16
+ execute: (query: string, bindValues?: unknown[]) => Promise<QueryResult>;
17
+ select: <T>(query: string, bindValues?: unknown[]) => Promise<T>;
18
+ transaction: (callback: (tx: Backend) => Promise<void>) => Promise<void>;
19
+ path: string;
20
+ }
@@ -0,0 +1,2 @@
1
+ import type { Backend } from './backend';
2
+ export declare function bunMemoryBackend(): Backend;
@@ -0,0 +1,5 @@
1
+ export declare class MapDeque<K, V> extends Map<K, V> {
2
+ private maxSize;
3
+ constructor(maxSize: number, entries?: ReadonlyArray<readonly [K, V]> | null);
4
+ set(key: K, value: V): this;
5
+ }
@@ -0,0 +1,3 @@
1
+ import type { Table, DbOptions, DocType } from './table.types';
2
+ export declare const DEFAULT_STEP_SIZE = 100;
3
+ export declare function createTable<Document extends DocType>(options: DbOptions<Document>): Promise<Table<Document>>;
@@ -0,0 +1,52 @@
1
+ import type { Backend } from './backend';
2
+ import type { Where } from './where';
3
+ export type DocType = {
4
+ [key: string]: any;
5
+ };
6
+ export type KeyTypeAvailable = 'string' | 'number';
7
+ export interface DbOptions<Document extends DocType> {
8
+ readonly sorBy?: keyof Document;
9
+ readonly order?: 'asc' | 'desc';
10
+ readonly tableName: string;
11
+ readonly indexes?: Array<keyof Document>;
12
+ readonly backend: Backend;
13
+ /**
14
+ * Optional key. If omitted, the table uses implicit SQLite ROWID as the key.
15
+ */
16
+ readonly key?: keyof Document;
17
+ }
18
+ interface DbNotGeneric {
19
+ readonly backend: Backend;
20
+ }
21
+ export interface SearchOptions<Document extends DocType, Selected = Document> {
22
+ readonly sorBy?: keyof Document;
23
+ readonly order?: 'asc' | 'desc';
24
+ readonly limit?: number;
25
+ readonly offset?: number;
26
+ readonly where?: Where<Document>;
27
+ readonly stepSize?: number;
28
+ /**
29
+ * Naive projection. Prefer specialized queries for heavy fan-out graphs.
30
+ */
31
+ readonly select?: (document: Document, meta: {
32
+ rowId: number;
33
+ }) => Selected;
34
+ }
35
+ export type Key = string | number;
36
+ export type MutationOp = 'insert' | 'update' | 'delete';
37
+ export interface MutationResult {
38
+ key: Key;
39
+ op: MutationOp;
40
+ }
41
+ export interface Table<Document extends DocType> extends DbNotGeneric {
42
+ readonly set: (document: Document, backendOverride?: Backend) => Promise<MutationResult>;
43
+ readonly batchSet: (documents: Document[]) => Promise<MutationResult[]>;
44
+ readonly get: <Selected = Document>(key: Key, selector?: (document: Document) => Selected) => Promise<Selected | undefined>;
45
+ readonly delete: (key: Key) => Promise<MutationResult | undefined>;
46
+ readonly search: <Selected = Document>(options?: SearchOptions<Document, Selected>) => AsyncIterableIterator<Selected>;
47
+ readonly count: (options?: {
48
+ where?: Where<Document>;
49
+ }) => Promise<number>;
50
+ readonly deleteBy: (where: Where<Document>) => Promise<MutationResult[]>;
51
+ }
52
+ export {};
@@ -0,0 +1,32 @@
1
+ export interface Field {
2
+ readonly table: string;
3
+ readonly field: string;
4
+ }
5
+ interface Condition<Document extends Record<string, unknown>, K extends keyof Document = keyof Document> {
6
+ readonly is?: Document[K] | Array<Document[K]>;
7
+ readonly isNot?: Document[K] | Array<Document[K]>;
8
+ readonly gt?: Document[K] | Array<Document[K]>;
9
+ readonly gte?: Document[K] | Array<Document[K]>;
10
+ readonly lt?: Document[K] | Array<Document[K]>;
11
+ readonly lte?: Document[K] | Array<Document[K]>;
12
+ readonly in?: Document[K] | Array<Document[K]>;
13
+ readonly notIn?: Document[K] | Array<Document[K]>;
14
+ readonly like?: Document[K] | Array<Document[K]>;
15
+ }
16
+ /**
17
+ * We extend `keyof Document` by the special literal "KEY".
18
+ * That means users can write `{ KEY: ... }` in addition to `{ someField: ... }`.
19
+ *
20
+ * - If K extends keyof Document, then primitive values must match Document[K].
21
+ * - If K === "KEY", then primitive values are treated as strings/Array<string>.
22
+ */
23
+ export type Where<Document extends Record<string, unknown>> = {
24
+ [K in keyof Document | 'KEY']?: Condition<Document, K extends keyof Document ? K : keyof Document> | (K extends keyof Document ? Document[K] : string) | (K extends keyof Document ? Array<Document[K]> : string[]);
25
+ } | {
26
+ readonly AND?: Array<Where<Document>>;
27
+ readonly OR?: Array<Where<Document>>;
28
+ readonly NOT?: Where<Document>;
29
+ };
30
+ export declare function getWhere<Document extends Record<string, unknown>>(where: Where<Document>, tableAlias?: string): string;
31
+ export declare function getWhereQuery<Document extends Record<string, unknown>>(where: Where<Document>): string;
32
+ export {};
@@ -0,0 +1,21 @@
1
+ import { type DependencyList } from 'react';
2
+ import type { SyncTable } from './create-sqlite-state';
3
+ import type { DocType } from './table/table.types';
4
+ import type { Where } from './table/where';
5
+ export interface SqLiteActions {
6
+ readonly next: () => Promise<boolean>;
7
+ readonly reset: () => Promise<void>;
8
+ }
9
+ export interface UseSearchOptions<Document extends DocType, Selected = Document> {
10
+ readonly sorBy?: keyof Document;
11
+ readonly order?: 'asc' | 'desc';
12
+ readonly limit?: number;
13
+ readonly offset?: number;
14
+ readonly where?: Where<Document>;
15
+ readonly stepSize?: number;
16
+ /**
17
+ * Naive projection. Prefer specialized queries for heavy fan-out graphs.
18
+ */
19
+ readonly select?: (document: Document) => Selected;
20
+ }
21
+ export declare function useSqliteValue<Document extends DocType, Selected = Document>(state: SyncTable<Document>, options?: UseSearchOptions<Document, Selected>, deps?: DependencyList): [undefined extends Selected ? Document[] : Selected[], SqLiteActions];
package/types/types.d.ts CHANGED
@@ -59,4 +59,5 @@ export interface State<T> extends GetState<T> {
59
59
  */
60
60
  withName: (name: string) => State<T>;
61
61
  isSet: true;
62
+ cache: Cache<T>;
62
63
  }
@@ -1,4 +1,4 @@
1
- import type { Cache, IsEqual } from '../types';
1
+ import type { Cache, IsEqual, State } from '../types';
2
2
  export interface CancelablePromise<T> {
3
3
  promise: Promise<T>;
4
4
  controller?: AbortController;
@@ -17,4 +17,4 @@ export declare function canUpdate<T>(cache: Cache<T>, isEqual?: IsEqual<T>): boo
17
17
  /**
18
18
  * Handle async updates for `create` and `select`
19
19
  */
20
- export declare function handleAsyncUpdate<T>(cache: Cache<T>, emit: () => void, value: T): T;
20
+ export declare function handleAsyncUpdate<T>(state: State<T>, value: T): T;