From 776a685aea3e90240e38c3fbf47d622ada77e62e Mon Sep 17 00:00:00 2001 From: Gregory Baker Date: Sat, 28 Dec 2024 12:33:55 -0700 Subject: [PATCH] feat(zero-solid): Add resultType support to zero-solid --- packages/zero-advanced/src/mod.ts | 2 +- packages/zero-react/src/use-query.tsx | 6 ++--- packages/zero-solid/src/solid-view.ts | 35 ++++++++++++++++++++++++--- packages/zero-solid/src/use-query.ts | 14 +++++++++-- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/zero-advanced/src/mod.ts b/packages/zero-advanced/src/mod.ts index e53ad647fc..5784da8a71 100644 --- a/packages/zero-advanced/src/mod.ts +++ b/packages/zero-advanced/src/mod.ts @@ -1,7 +1,7 @@ export type {Change} from '../../zql/src/ivm/change.js'; export type {Input, Output} from '../../zql/src/ivm/operator.js'; export {applyChange} from '../../zql/src/ivm/view-apply-change.js'; -export type {Entry, Format, View} from '../../zql/src/ivm/view.js'; +export type {Entry, Format, View, ViewFactory} from '../../zql/src/ivm/view.js'; export type {AdvancedQuery} from '../../zql/src/query/query-internal.js'; export type {Query, QueryType, Smash} from '../../zql/src/query/query.js'; export type {TableSchema} from '../../zero-schema/src/table-schema.js'; diff --git a/packages/zero-react/src/use-query.tsx b/packages/zero-react/src/use-query.tsx index d758ed4201..054ea7fdba 100644 --- a/packages/zero-react/src/use-query.tsx +++ b/packages/zero-react/src/use-query.tsx @@ -13,11 +13,11 @@ import {useZero} from './use-zero.js'; import type {TableSchema} from '../../zero-schema/src/table-schema.js'; import type {ResultType} from '../../zql/src/query/typed-view.js'; -export type QueryResultDetails = { +export type QueryResultDetails = Readonly<{ type: ResultType; -}; +}>; -export type QueryResult = [ +export type QueryResult = readonly [ Smash, QueryResultDetails, ]; diff --git a/packages/zero-solid/src/solid-view.ts b/packages/zero-solid/src/solid-view.ts index 9099961815..983a80980b 100644 --- a/packages/zero-solid/src/solid-view.ts +++ b/packages/zero-solid/src/solid-view.ts @@ -11,7 +11,9 @@ import { type Smash, type TableSchema, type View, + type ViewFactory, } from '../../zero-advanced/src/mod.js'; +import type {ResultType} from '../../zql/src/query/typed-view.js'; export class SolidView implements Output { readonly #input: Input; @@ -20,20 +22,27 @@ export class SolidView implements Output { // Synthetic "root" entry that has a single "" relationship, so that we can // treat all changes, including the root change, generically. - readonly #root: Entry; + readonly #rootStore: Entry; readonly #setRoot: SetStoreFunction; + readonly #resultTypeStore: {resultType: ResultType}; + readonly #setResultType: SetStoreFunction<{resultType: ResultType}>; + constructor( input: Input, format: Format = {singular: false, relationships: {}}, onDestroy: () => void = () => {}, + queryComplete: true | Promise, ) { this.#input = input; this.#format = format; this.#onDestroy = onDestroy; - [this.#root, this.#setRoot] = createStore({ + [this.#rootStore, this.#setRoot] = createStore({ '': format.singular ? undefined : [], }); + [this.#resultTypeStore, this.#setResultType] = createStore({ + resultType: queryComplete ? 'complete' : 'unknown', + }); input.setOutput(this); this.#setRoot( @@ -49,10 +58,19 @@ export class SolidView implements Output { } }), ); + if (queryComplete !== true) { + void queryComplete.then(() => { + this.#setResultType({resultType: 'complete'}); + }); + } } get data() { - return this.#root[''] as V; + return this.#rootStore[''] as V; + } + + get resultType() { + return this.#resultTypeStore.resultType; } destroy() { @@ -82,8 +100,17 @@ export function solidViewFactory< input: Input, format: Format, onDestroy: () => void, + _onTransactionCommit: (cb: () => void) => void, + queryComplete: true | Promise, ): SolidView> { - const v = new SolidView>(input, format, onDestroy); + const v = new SolidView>( + input, + format, + onDestroy, + queryComplete, + ); return v; } + +solidViewFactory satisfies ViewFactory; diff --git a/packages/zero-solid/src/use-query.ts b/packages/zero-solid/src/use-query.ts index b74bafcd91..cbf70ca766 100644 --- a/packages/zero-solid/src/use-query.ts +++ b/packages/zero-solid/src/use-query.ts @@ -7,11 +7,21 @@ import type { TableSchema, } from '../../zero-advanced/src/mod.js'; import {solidViewFactory} from './solid-view.js'; +import type {ResultType} from '../../zql/src/query/typed-view.js'; + +export type QueryResultDetails = Readonly<{ + type: ResultType; +}>; + +export type QueryResult = readonly [ + Smash, + QueryResultDetails, +]; export function useQuery< TSchema extends TableSchema, TReturn extends QueryType, ->(querySignal: () => Query): Accessor> { +>(querySignal: () => Query): Accessor> { return createMemo(() => { const query = querySignal(); const view = (query as AdvancedQuery).materialize( @@ -22,6 +32,6 @@ export function useQuery< view.destroy(); }); - return view.data; + return [view.data, {type: view.resultType}] as const; }); }