Skip to content

Commit

Permalink
feat(zero-solid): Add resultType support to zero-solid
Browse files Browse the repository at this point in the history
  • Loading branch information
grgbkr committed Dec 28, 2024
1 parent 26206db commit 776a685
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
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
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>,
) {
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(
Expand All @@ -49,10 +58,19 @@ export class SolidView<V extends View> implements Output {
}
}),
);
if (queryComplete !== true) {
void queryComplete.then(() => {

Check failure on line 62 in packages/zero-solid/src/solid-view.ts

View workflow job for this annotation

GitHub Actions / Test

src/solid-view.test.ts > basics

TypeError: Cannot read properties of undefined (reading 'then') ❯ new SolidView src/solid-view.ts:62:25 ❯ src/solid-view.test.ts:15:15

Check failure on line 62 in packages/zero-solid/src/solid-view.ts

View workflow job for this annotation

GitHub Actions / Test

src/solid-view.test.ts > single-format

TypeError: Cannot read properties of undefined (reading 'then') ❯ new SolidView src/solid-view.ts:62:25 ❯ src/solid-view.test.ts:53:15

Check failure on line 62 in packages/zero-solid/src/solid-view.ts

View workflow job for this annotation

GitHub Actions / Test

src/solid-view.test.ts > factory

TypeError: Cannot read properties of undefined (reading 'then') ❯ new SolidView src/solid-view.ts:62:25 ❯ solidViewFactory src/solid-view.ts:106:12 ❯ src/solid-view.test.ts:109:45
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;
});
}

0 comments on commit 776a685

Please sign in to comment.