floppy-disk 2.3.0-beta.1 → 2.3.0-beta.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.
package/README.md CHANGED
@@ -28,28 +28,39 @@ import { createQuery, createMutation } from 'floppy-disk'; // 8.4 kB (gzipped: 2
28
28
 
29
29
  ## Key Features
30
30
 
31
- - Create store ✅
32
- - Yes, it also support middleware & set/get state outside component
33
- - Store event (`onSubscribe`, `onUnsubscribe`, etc.)
34
- - Use store as local state manager ✅
35
- - Create stores (yes, it's plural) ✅ \*_controlled with a store key_
36
- - Create query
37
- - Dedupe multiple request
38
- - Auto-update stale data (stale-while-revalidate) ✅
39
- - Enable/disable query
40
- - Auto-fetch or manual (lazy query) ✅
41
- - Retry on error (retry count, retry delay)
42
- - SSR/SSG's initial query data ✅
43
- - Optimistic update ✅
44
- - Invalidate query
45
- - Reset query
46
- - Query with param (query key) ✅
47
- - Infinite query
48
- - Get query data outside component ✅
49
- - Custom reactivity
50
- - Fetching mechanisms are agnostically built on promises
51
- - Can be used with literally any asynchronous data fetching client, including GraphQL ✅
52
- - Create mutation ✅
31
+ - **Create Store**
32
+ - Get/set store inside/outside component
33
+ - Custom reactivity (like `useEffect`'s dependency array)
34
+ - Support middleware
35
+ - Set state interception
36
+ - Store event (`onSubscribe`, `onUnsubscribe`, etc.)
37
+ - Use store as local state manager
38
+ - **Create Stores**
39
+ - Same as store, but controlled with a store key
40
+ - **Create Query & Mutation**
41
+ - Backend agnostic (support GraphQL & any async function)
42
+ - TypeScript ready
43
+ - SSR/SSG support
44
+ - Custom reactivity (we choose when to re-render)
45
+ - **Create query**
46
+ - Dedupe multiple request
47
+ - Auto-fetch on mount or manual (lazy query)
48
+ - Enable/disable query
49
+ - Serve stale data while revalidating
50
+ - Retry on error (customizable)
51
+ - Optimistic update
52
+ - Invalidate query
53
+ - Reset query
54
+ - Query with param (query key)
55
+ - Paginated/infinite query
56
+ - Prefetch query
57
+ - Fetch from inside/outside component
58
+ - Get query state inside/outside component
59
+ - Suspense mode
60
+ - **Create mutation**
61
+ - Mutate from inside/outside component
62
+ - Get mutation state inside/outside component
63
+ - ... and [a lot more](https://floppy-disk.vercel.app/)
53
64
 
54
65
  <br>
55
66
 
@@ -1,3 +1,5 @@
1
+ /// <reference types="react" />
2
+ import { FunctionComponent } from 'preact';
1
3
  import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
2
4
  export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
3
5
  /**
@@ -227,5 +229,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
227
229
  * Use query with suspense mode.
228
230
  */
229
231
  suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
232
+ Render: (props: {
233
+ queryKey?: TKey | null;
234
+ loading?: FunctionComponent<TKey>;
235
+ success?: FunctionComponent<TKey>;
236
+ error?: FunctionComponent<TKey>;
237
+ }) => JSX.Element;
230
238
  };
231
239
  export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -1,3 +1,4 @@
1
+ import { h as createElement } from 'preact';
1
2
  import { useState } from 'preact/hooks';
2
3
  import { hasValue, identityFn, noop } from '../utils';
3
4
  import { createStores } from './create-stores';
@@ -352,5 +353,14 @@ export const createQuery = (queryFn, options = {}) => {
352
353
  throw state.error;
353
354
  return state;
354
355
  };
356
+ const defaultElement = () => null;
357
+ useQuery.Render = (props) => {
358
+ const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
359
+ // eslint-disable-next-line react-hooks/rules-of-hooks
360
+ const state = useQuery(queryKey);
361
+ if (state.data)
362
+ return createElement(success, state.key);
363
+ return createElement(state.isLoading ? loading : error, state.key);
364
+ };
355
365
  return useQuery;
356
366
  };
@@ -1,3 +1,4 @@
1
+ import { FunctionComponent } from 'react';
1
2
  import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
2
3
  export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
3
4
  /**
@@ -227,5 +228,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
227
228
  * Use query with suspense mode.
228
229
  */
229
230
  suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
231
+ Render: (props: {
232
+ queryKey?: TKey | null;
233
+ loading?: FunctionComponent<TKey>;
234
+ success?: FunctionComponent<TKey>;
235
+ error?: FunctionComponent<TKey>;
236
+ }) => JSX.Element;
230
237
  };
231
238
  export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -1,4 +1,4 @@
1
- import { useState } from 'react';
1
+ import { createElement, useState } from 'react';
2
2
  import { hasValue, identityFn, noop } from '../utils';
3
3
  import { createStores } from './create-stores';
4
4
  const getDecision = (value, param, { ifTrue, ifAlways }) => {
@@ -352,5 +352,14 @@ export const createQuery = (queryFn, options = {}) => {
352
352
  throw state.error;
353
353
  return state;
354
354
  };
355
+ const defaultElement = () => null;
356
+ useQuery.Render = (props) => {
357
+ const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
358
+ // eslint-disable-next-line react-hooks/rules-of-hooks
359
+ const state = useQuery(queryKey);
360
+ if (state.data)
361
+ return createElement(success, state.key);
362
+ return createElement(state.isLoading ? loading : error, state.key);
363
+ };
355
364
  return useQuery;
356
365
  };
@@ -1,3 +1,5 @@
1
+ /// <reference types="react" />
2
+ import { FunctionComponent } from 'preact';
1
3
  import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
2
4
  export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
3
5
  /**
@@ -227,5 +229,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
227
229
  * Use query with suspense mode.
228
230
  */
229
231
  suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
232
+ Render: (props: {
233
+ queryKey?: TKey | null;
234
+ loading?: FunctionComponent<TKey>;
235
+ success?: FunctionComponent<TKey>;
236
+ error?: FunctionComponent<TKey>;
237
+ }) => JSX.Element;
230
238
  };
231
239
  export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createQuery = void 0;
4
+ const preact_1 = require("preact");
4
5
  const hooks_1 = require("preact/hooks");
5
6
  const utils_1 = require("../utils");
6
7
  const create_stores_1 = require("./create-stores");
@@ -355,6 +356,15 @@ const createQuery = (queryFn, options = {}) => {
355
356
  throw state.error;
356
357
  return state;
357
358
  };
359
+ const defaultElement = () => null;
360
+ useQuery.Render = (props) => {
361
+ const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
362
+ // eslint-disable-next-line react-hooks/rules-of-hooks
363
+ const state = useQuery(queryKey);
364
+ if (state.data)
365
+ return (0, preact_1.h)(success, state.key);
366
+ return (0, preact_1.h)(state.isLoading ? loading : error, state.key);
367
+ };
358
368
  return useQuery;
359
369
  };
360
370
  exports.createQuery = createQuery;
@@ -1,3 +1,4 @@
1
+ import { FunctionComponent } from 'react';
1
2
  import { CreateStoresOptions, StoreKey, UseStores } from './create-stores';
2
3
  export type QueryState<TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown> = {
3
4
  /**
@@ -227,5 +228,11 @@ export type UseQuery<TKey extends StoreKey = StoreKey, TResponse = any, TData =
227
228
  * Use query with suspense mode.
228
229
  */
229
230
  suspend: (key?: TKey | null) => QueryState<TKey, TResponse, TData, TError>;
231
+ Render: (props: {
232
+ queryKey?: TKey | null;
233
+ loading?: FunctionComponent<TKey>;
234
+ success?: FunctionComponent<TKey>;
235
+ error?: FunctionComponent<TKey>;
236
+ }) => JSX.Element;
230
237
  };
231
238
  export declare const createQuery: <TKey extends StoreKey = StoreKey, TResponse = any, TData = TResponse, TError = unknown>(queryFn: (key: TKey, state: QueryState<TKey, TResponse, TData, TError>) => Promise<TResponse>, options?: CreateQueryOptions<TKey, TResponse, TData, TError>) => UseQuery<TKey, TResponse, TData, TError>;
@@ -355,6 +355,15 @@ const createQuery = (queryFn, options = {}) => {
355
355
  throw state.error;
356
356
  return state;
357
357
  };
358
+ const defaultElement = () => null;
359
+ useQuery.Render = (props) => {
360
+ const { queryKey, loading = defaultElement, success = defaultElement, error = defaultElement, } = props;
361
+ // eslint-disable-next-line react-hooks/rules-of-hooks
362
+ const state = useQuery(queryKey);
363
+ if (state.data)
364
+ return (0, react_1.createElement)(success, state.key);
365
+ return (0, react_1.createElement)(state.isLoading ? loading : error, state.key);
366
+ };
358
367
  return useQuery;
359
368
  };
360
369
  exports.createQuery = createQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "floppy-disk",
3
- "version": "2.3.0-beta.1",
3
+ "version": "2.3.0-beta.2",
4
4
  "description": "FloppyDisk - lightweight, simple, and powerful state management library",
5
5
  "keywords": [
6
6
  "state",