Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zero-solid): Add resultType support to zero-solid #3460

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zero-advanced/src/mod.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
6 changes: 3 additions & 3 deletions packages/zero-react/src/use-query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TReturn extends QueryType> = [
export type QueryResult<TReturn extends QueryType> = readonly [
Smash<TReturn>,
QueryResultDetails,
];
Expand Down
38 changes: 38 additions & 0 deletions packages/zero-solid/src/solid-view.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect, test} from 'vitest';
import {resolver} from '@rocicorp/resolver';
import {MemorySource} from '../../zql/src/ivm/memory-source.js';
import {SolidView, solidViewFactory} from './solid-view.js';
import type {Query, Smash} from '../../zql/src/query/query.js';
Expand All @@ -24,6 +25,8 @@ test('basics', () => {
{a: 2, b: 'b'},
]);

expect(view.resultType).toEqual('complete');

ms.push({row: {a: 3, b: 'c'}, type: 'add'});

expect(view.data).toEqual([
Expand Down Expand Up @@ -71,6 +74,39 @@ test('single-format', () => {
expect(view.data).toEqual(undefined);
});

test('queryComplete promise', async () => {
const ms = new MemorySource(
'table',
{a: {type: 'number'}, b: {type: 'string'}},
['a'],
);
ms.push({row: {a: 1, b: 'a'}, type: 'add'});
ms.push({row: {a: 2, b: 'b'}, type: 'add'});

const queryCompleteResolver = resolver<true>();

const view = new SolidView(
ms.connect([
['b', 'asc'],
['a', 'asc'],
]),
undefined,
undefined,
queryCompleteResolver.promise,
);

expect(view.data).toEqual([
{a: 1, b: 'a'},
{a: 2, b: 'b'},
]);

expect(view.resultType).toEqual('unknown');

queryCompleteResolver.resolve(true);
await 1;
expect(view.resultType).toEqual('complete');
});

type TestSchema = {
tableName: 'test';
columns: {
Expand Down Expand Up @@ -114,6 +150,8 @@ test('factory', () => {
]),
{singular: false, relationships: {}},
onDestroy,
() => undefined,
true,
);
expect(view).toBeDefined();
expect(onDestroyCalled).false;
Expand Down
35 changes: 31 additions & 4 deletions packages/zero-solid/src/solid-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<V extends View> implements Output {
readonly #input: Input;
Expand All @@ -20,20 +22,27 @@ export class SolidView<V extends View> 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<Entry>;

readonly #resultTypeStore: {resultType: ResultType};
readonly #setResultType: SetStoreFunction<{resultType: ResultType}>;

constructor(
input: Input,
format: Format = {singular: false, relationships: {}},
onDestroy: () => void = () => {},
queryComplete: true | Promise<true> = true,
) {
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 === true ? 'complete' : 'unknown',
});
input.setOutput(this);

this.#setRoot(
Expand All @@ -49,10 +58,19 @@ export class SolidView<V extends View> 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() {
Expand Down Expand Up @@ -82,8 +100,17 @@ export function solidViewFactory<
input: Input,
format: Format,
onDestroy: () => void,
_onTransactionCommit: (cb: () => void) => void,
queryComplete: true | Promise<true>,
): SolidView<Smash<TReturn>> {
const v = new SolidView<Smash<TReturn>>(input, format, onDestroy);
const v = new SolidView<Smash<TReturn>>(
input,
format,
onDestroy,
queryComplete,
);

return v;
}

solidViewFactory satisfies ViewFactory<TableSchema, QueryType, unknown>;
14 changes: 12 additions & 2 deletions packages/zero-solid/src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TReturn extends QueryType> = readonly [
Smash<TReturn>,
QueryResultDetails,
];

export function useQuery<
TSchema extends TableSchema,
TReturn extends QueryType,
>(querySignal: () => Query<TSchema, TReturn>): Accessor<Smash<TReturn>> {
>(querySignal: () => Query<TSchema, TReturn>): Accessor<QueryResult<TReturn>> {
return createMemo(() => {
const query = querySignal();
const view = (query as AdvancedQuery<TSchema, TReturn>).materialize(
Expand All @@ -22,6 +32,6 @@ export function useQuery<
view.destroy();
});

return view.data;
return [view.data, {type: view.resultType}] as const;
});
}
Loading