Skip to content

Commit

Permalink
per-query storage, serializer, equals
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Aug 28, 2022
1 parent 9ecc858 commit c04eab3
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 446 deletions.
299 changes: 158 additions & 141 deletions dist/index.esm.js

Large diffs are not rendered by default.

297 changes: 157 additions & 140 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions dist/libs/ortho.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ export declare class Ortho<K = any, S = any> {
subscribe(key: K, listener: (x: S) => void): void;
unsubscribe(key: K, listener: (x: S) => void): void;
}
/**
* Orthogonal state listener
*/
export declare function useOrtho<K, S>(ortho: Ortho<K, S>, key: K | undefined, callback: (s: S) => void): void;
29 changes: 17 additions & 12 deletions dist/mods/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export declare type PosterMore<D = any> = {
};
export declare type Scroller<D = any, K = any> = (previous?: D) => K | undefined;
export declare type Updater<D = any> = (previous?: D) => D;
export interface Params<D = any, E = any, K = any> extends TimeParams {
storage?: Storage<State<D, E>>;
serializer?: Serializer<K>;
equals?: Equals;
}
export interface CoreParams extends TimeParams {
storage?: Storage<State>;
serializer?: Serializer;
Expand All @@ -36,36 +41,36 @@ export declare class Core extends Ortho<string, State | undefined> {
readonly cooldown: number;
readonly expiration: number;
readonly timeout: number;
protected mounted: boolean;
_mounted: boolean;
constructor(params?: CoreParams);
get async(): boolean;
hasSync(key: string | undefined): boolean;
has(key: string | undefined): Promise<boolean>;
getSync<D = any, E = any>(key: string | undefined): State<D, E> | undefined;
get<D = any, E = any>(key: string | undefined): Promise<State<D, E> | undefined>;
hasSync<D = any, E = any>(key: string | undefined, params?: Params<D, E>): boolean;
has<D = any, E = any>(key: string | undefined, params?: Params<D, E>): Promise<boolean>;
getSync<D = any, E = any>(key: string | undefined, params?: Params<D, E>): State<D, E> | undefined;
get<D = any, E = any>(key: string | undefined, params?: Params<D, E>): Promise<State<D, E> | undefined>;
/**
* Force set a key to a state and publish it
* No check, no merge
* @param key Key
* @param state New state
* @returns
*/
set<D = any, E = any>(key: string | undefined, state: State<D, E>): Promise<void>;
set<D = any, E = any>(key: string | undefined, state: State<D, E>, params?: Params<D, E>): Promise<void>;
/**
* Delete key and publish undefined
* @param key
* @returns
*/
delete(key: string | undefined): Promise<void>;
apply<D = any, E = any>(key: string | undefined, current?: State<D, E>, state?: State<D, E>): Promise<State<D, E> | undefined>;
mutate<D = any, E = any>(key: string | undefined, state?: State<D, E>): Promise<State<D, E> | undefined>;
delete<D = any, E = any>(key: string | undefined, params?: Params<D, E>): Promise<void>;
apply<D = any, E = any>(key: string | undefined, current?: State<D, E>, state?: State<D, E>, params?: Params<D, E>): Promise<State<D, E> | undefined>;
mutate<D = any, E = any>(key: string | undefined, state?: State<D, E>, params?: Params<D, E>): Promise<State<D, E> | undefined>;
/**
* True if we should cooldown this resource
*/
shouldCooldown<D = any, E = any>(current?: State<D, E>, force?: boolean): boolean;
counts: Map<string, number>;
timeouts: Map<string, NodeJS.Timeout>;
subscribe(key: string | undefined, listener: (x: State) => void): void;
unsubscribe(key: string | undefined, listener: (x: State) => void): Promise<void>;
subscribe<D = any, E = any>(key: string | undefined, listener: (x: State<D, E>) => void, params?: Params<D, E>): void;
unsubscribe<D = any, E = any>(key: string | undefined, listener: (x: State<D, E>) => void, params?: Params<D, E>): Promise<void>;
get mounted(): boolean;
unmount(): void;
}
5 changes: 2 additions & 3 deletions dist/mods/handles/scroll.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Fetcher, Scroller } from "../core.js";
import { Fetcher, Params, Scroller } from "../core.js";
import { State } from "../storages/storage.js";
import { TimeParams } from "../time.js";
import { Handle } from "./handle.js";
/**
* Handle for a scrolling resource
Expand All @@ -18,4 +17,4 @@ export interface ScrollHandle<D = any, E = any, K = any> extends Handle<D[], E,
* @param tparams Time parameters (constant)
* @returns Scrolling handle
*/
export declare function useScroll<D = any, E = any, K = any>(scroller: Scroller<D, K>, fetcher: Fetcher<D, K>, tparams?: TimeParams): ScrollHandle<D, E, K>;
export declare function useScroll<D = any, E = any, K = any>(scroller: Scroller<D, K>, fetcher: Fetcher<D, K>, params?: Params<D[], E>): ScrollHandle<D, E, K>;
5 changes: 2 additions & 3 deletions dist/mods/handles/single.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Poster, Updater } from "../core.js";
import { Params, Poster, Updater } from "../core.js";
import { State } from "../storages/storage.js";
import { TimeParams } from "../time.js";
import { Handle } from "./handle.js";
/**
* Handle for a single resource
Expand All @@ -20,4 +19,4 @@ export interface SingleHandle<D = any, E = any, K = any> extends Handle<D, E, K>
* @param tparams Time parameters (constant)
* @returns Single handle
*/
export declare function useSingle<D = any, E = any, K = any>(key: K | undefined, poster: Poster<D, K>, tparams?: TimeParams): SingleHandle<D, E, K>;
export declare function useSingle<D = any, E = any, K = any>(key: K | undefined, poster: Poster<D, K>, params?: Params<D, E>): SingleHandle<D, E, K>;
6 changes: 3 additions & 3 deletions dist/mods/scroll.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Core, Fetcher, Scroller } from "./core.js";
import { TimeParams } from "./time.js";
import { Params } from "./index.js";
export declare class Scroll {
readonly core: Core;
constructor(core: Core);
Expand All @@ -13,7 +13,7 @@ export declare class Scroll {
* @param force Should ignore cooldown
* @returns The new state
*/
first<D = any, E = any, K = any>(skey: string | undefined, scroller: Scroller<D, K>, fetcher: Fetcher<D, K>, aborter?: AbortController, tparams?: TimeParams, force?: boolean): Promise<import("./index.js").State<D[], E>>;
first<D = any, E = any, K = any>(skey: string | undefined, scroller: Scroller<D, K>, fetcher: Fetcher<D, K>, aborter?: AbortController, params?: Params<D[], E>, force?: boolean): Promise<import("./index.js").State<D[], E>>;
/**
* Scroll to the next page
* @param skey Storage key
Expand All @@ -24,5 +24,5 @@ export declare class Scroll {
* @param force Should ignore cooldown
* @returns The new state
*/
scroll<D = any, E = any, K = any>(skey: string | undefined, scroller: Scroller<D, K>, fetcher: Fetcher<D, K>, aborter?: AbortController, tparams?: TimeParams, force?: boolean): Promise<import("./index.js").State<D[], E>>;
scroll<D = any, E = any, K = any>(skey: string | undefined, scroller: Scroller<D, K>, fetcher: Fetcher<D, K>, aborter?: AbortController, params?: Params<D[], E>, force?: boolean): Promise<import("./index.js").State<D[], E>>;
}
6 changes: 3 additions & 3 deletions dist/mods/single.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Core, Fetcher, Poster, Updater } from "./core.js";
import { Params } from "./index.js";
import { State } from "./storages/storage.js";
import { TimeParams } from "./time.js";
export declare class Single {
readonly core: Core;
constructor(core: Core);
Expand All @@ -14,7 +14,7 @@ export declare class Single {
* @param force Should ignore cooldown
* @returns The new state
*/
fetch<D = any, E = any, K = any>(key: K | undefined, skey: string | undefined, fetcher: Fetcher<D, K>, aborter?: AbortController, tparams?: TimeParams, force?: boolean): Promise<State<D, E> | undefined>;
fetch<D = any, E = any, K = any>(key: K | undefined, skey: string | undefined, fetcher: Fetcher<D, K>, aborter?: AbortController, params?: Params<D, E>, force?: boolean): Promise<State<D, E> | undefined>;
/**
* Optimistic update
* @param key Key (:K) (passed to poster)
Expand All @@ -26,5 +26,5 @@ export declare class Single {
* @returns The new state
* @throws Error
*/
update<D = any, E = any, K = any>(key: K | undefined, skey: string | undefined, poster: Poster<D, K>, updater: Updater<D>, aborter?: AbortController, tparams?: TimeParams): Promise<State<D, E>>;
update<D = any, E = any, K = any>(key: K | undefined, skey: string | undefined, poster: Poster<D, K>, updater: Updater<D>, aborter?: AbortController, params?: Params<D, E>): Promise<State<D, E>>;
}
16 changes: 0 additions & 16 deletions src/libs/ortho.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect } from "react"

/**
* Map of arrays data structure
Expand Down Expand Up @@ -50,18 +49,3 @@ export class Ortho<K = any, S = any> {
this.listeners.erase(key, listener)
}
}

/**
* Orthogonal state listener
*/
export function useOrtho<K, S>(
ortho: Ortho<K, S>,
key: K | undefined,
callback: (s: S) => void
) {
useEffect(() => {
if (!key) return
ortho.subscribe(key, callback)
return () => ortho.unsubscribe(key, callback)
}, [ortho, key, callback])
}
Loading

0 comments on commit c04eab3

Please sign in to comment.