Archived
1
0
Fork
You've already forked sq
0
Lightweight asynchronous state management and data fetching solution for Solid.js
This repository has been archived on 2024年02月04日. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
  • TypeScript 98.3%
  • HTML 1.7%
Find a file
2023年10月20日 13:33:52 +07:00
.vscode initial commit 2023年06月30日 14:04:28 +07:00
lib chore: fmt 2023年10月20日 13:33:52 +07:00
.editorconfig initial commit 2023年06月30日 14:04:28 +07:00
.gitignore initial commit 2023年06月30日 14:04:28 +07:00
.npmrc initial commit 2023年06月30日 14:04:28 +07:00
.prettierignore initial commit 2023年06月30日 14:04:28 +07:00
.prettierrc initial commit 2023年06月30日 14:04:28 +07:00
index.html chore: fmt 2023年10月20日 13:33:52 +07:00
jsconfig.json initial commit 2023年06月30日 14:04:28 +07:00
LICENSE initial commit 2023年06月30日 14:04:28 +07:00
package.json v0.3.0 2023年10月19日 16:04:10 +07:00
pnpm-lock.yaml chore: upgrade dependencies 2023年10月19日 16:02:20 +07:00
README.md docs: mention throw on access 2023年07月31日 08:20:25 +07:00
tsconfig.json chore: fix tsconfig 2023年10月19日 16:03:47 +07:00
tsconfig.node.json initial commit 2023年06月30日 14:04:28 +07:00
vite.config.ts initial commit 2023年06月30日 14:04:28 +07:00

sq

Lightweight asynchronous state management and data fetching solution for Solid.js

  • Works similarly to Solid.js' createResource
  • Fits right in between createResource and @tanstack/solid-query
  • Allows directly handling previous query data within the query function (useful for infinite pagination)
  • Does not mix up query data when switching between query keys
  • Configurable throw on access

Infinite query example

import { type QueryFn, createQuery } from '@intrnl/sq';
// Utiltiies
interface Collection<Data, Param = unknown> {
	pages: Data[];
	params: Param[];
}
const pushCollection = <Data, Param>(
	collection: Collection<Data, Param>,
	page: Data,
	param: Param | undefined,
): Collection<Data, Param> => {
	if (collection && param !== undefined) {
		return {
			pages: collection.pages.concat(page),
			params: collection.params.concat(param),
		};
	}
	return {
		pages: [page],
		params: [param],
	};
};
const getCollectionCursor = <Data, Key extends keyof Data>(
	collection: Collection<Data, any> | undefined,
	key: Key,
): Data[Key] | undefined => {
	if (collection) {
		const pages = collection.pages;
		const last = pages[pages.length - 1];
		return last[key];
	}
};
// Query
interface User {
	id: number;
	name: string;
}
interface UsersListing {
	cursor: string | undefined;
	users: User[];
}
export const listUsersByQueryKey = (query: string) => {
	return ['listUsersByQuery', query];
};
export const listUsersByQuery: QueryFn<
	Collection<UsersListing>,
	ReturnType<typeof listUsersByQueryKey>,
	string
> = async (key, { data: collection, param }) => {
	const [, query] = key;
	const page = await request('...');
	return pushCollection(collection, page, param);
};
// Interface
const [users, { refetch }] = createQuery({
	key: () => listUsersByQueryKey(''),
	fetch: listUsersByQuery,
});
<Show when={getCollectionCursor(users(), 'cursor')}>
	{(cursor) => <button onClick={() => refetch(true, cursor())}>Show more</button>}
</Show>;