Giter Site home page Giter Site logo

tyin's People

Contributors

mausworks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

tengl

tyin's Issues

Feature request: Loaders

How about adding loaders? Something like this:

import { AnyState, StoreAPI } from "tyin/store";
import { Plugin } from "tyin/extend";

/** Options for the persist plugin. */
export type LoaderOptions = {
  defaultUrl: URL | string;
  autoload?: boolean;
};
export type LoaderAPI = {
  load: (overrideUrl?: URL | string) => Promise<void>;
};
export type LoaderPlugin<T extends AnyState> = Plugin<StoreAPI<T>, LoaderAPI>;

/**
 * A plugin that loads the state from an URL.
 * @param options Configure the plugin.
 * @template T The type of the state.
 * @example
 * ```ts
 * import storeHook from "tyin/hook";
 * import extend from "tyin/extend";
 * import jsonLoader from "tyin/plugin-jsonloader";
 *
 * const useExample = extend(storeHook(null))
 *   .with(jsonLoader({ url: "./example.json" })
 *   .seal();
 * ```
 */
export function jsonLoader<T extends AnyState>({
  defaultUrl,
  autoload = true,
}: LoaderOptions): LoaderPlugin<T> {
  return (store) => {
    const loader = async (overrideUrl?: URL | string) => {
      const response = await fetch(overrideUrl ?? defaultUrl);

      if (response.ok) store.set(await response.json());
    };

    if (autoload) {
      loader();
    }

    return {
      load: loader,
    };
  };
}

Which would be used like this:

const useUserState = objectStoreHook<User | null>(
  null,
  undefined,
  false
)
  .with(jsonLoader({ url: "/user_data.json" }))
  .seal();

// Load other data
await useUserState.load("/other_user_data.json")

or

const useUserState = objectStoreHook<User | null>(
  null,
  undefined,
  false
)
  .with(jsonLoader({ url: "/user_data.json", autoload: false }));

await useUserState.load()

Feature request: Pre-configured store hooks (object, array, etc โ€ฆ)

Hi there,

I will try this lib out on an internal tool. It is a bit verbose when it comes to creating and configuring stores, but that is fine, you can encapsulate the code.

Wouldn't it be nice though with some pre-configured store hooks with the most common usages? E.g.

import storeHook, { StateSelectorHook } from "tyin/hook";
import extend, { Extensible } from "tyin/extend";
import { StoreAPI, StoreOptions } from "tyin/store";
import objectAPI, { ObjectAPI, ObjectLike } from "tyin/plugin-object";

export function objectStoreHook<T extends ObjectLike | null>(
  initialState: T,
  options?: StoreOptions<T>,
  sealed?: true
): StateSelectorHook<T> & StoreAPI<T> & ObjectAPI<T>;
export function objectStoreHook<T extends ObjectLike | null>(
  initialState: T,
  options?: StoreOptions<T>,
  sealed?: false
): Extensible<StateSelectorHook<T> & StoreAPI<T> & ObjectAPI<T>>;
export function objectStoreHook<T extends ObjectLike | null>(
  initialState: T,
  options?: StoreOptions<T>,
  sealed: boolean = true
):
  | (StateSelectorHook<T> & StoreAPI<T> & ObjectAPI<T>)
  | Extensible<StateSelectorHook<T> & StoreAPI<T> & ObjectAPI<T>> {
  const hook = extend(storeHook(initialState, options)).with(objectAPI());
  if (sealed) return hook.seal();
  return hook;
}

(The extra function definitions is to handle if it is sealed or not, there is probably a better way to do it)

It would make it easy to use:

const useUserState = objectStoreHook<User | null>(null);

The same can be done for an array store hook.

Also, I noticed that the objectAPI doesn't allow undefined. Is that by design?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.