Skip to content

Releases: pengooseDev/pengoose-jotai

v1.2.0

31 Mar 20:25
Compare
Choose a tag to compare

Enhancements

useManager

  • Advanced type inference with action params

prev

    [P in keyof T['actions']]: T['actions'][P] extends WritableAtom<
      any,
      infer U,
      void
    >
      ? (param: U[0]) => void
      : never;

changed

    [P in keyof T['actions']]: T['actions'][P] extends WritableAtom<
      any,
      infer U,
      void
    >
      ? U[0] extends undefined
        ? () => void
        : (param: U[0]) => void
      : never;

v1.1.9

26 Mar 21:45
Compare
Choose a tag to compare

Enhancements

AtomManager

  • Allows for more flexible abstraction by changing Atom[T[K]] to Atom<any>

prev

  abstract selectors: {
    [K in keyof Partial<T>]: Atom<T[K]>;;
  };

changed

  abstract selectors: {
    [K in keyof Partial<T>]: Atom<any>;
  };

Bug Fixes

useManager

Change to use Curry function to avoid calling the hook inside of callback function

prev

export const useManager = <T extends AtomManager<any>>(manager: T) => {
  const selectors = Object.fromEntries(
    Object.entries(manager.selectors).map(([key, atom]) => [
      key,
      useAtomValue(atom), // 🚩
    ])
  ) as {
    [P in keyof T['selectors']]: T['selectors'][P] extends Atom<infer V>
      ? V
      : never;
  };

  const actions = Object.fromEntries(
    Object.entries(manager.actions).map(([key, actionAtom]) => [
      key,
      useSetAtom(actionAtom), // 🚩
    ])
  ) as {
    [P in keyof T['actions']]: T['actions'][P] extends WritableAtom<
      any,
      infer U,
      void
    >
      ? (param: U[0]) => void
      : never;
  };

  return { selectors, actions };
};

Changed

const createUseSelector = <T>(atom: Atom<T>) => {
  return () => useAtomValue(atom); // ✅
};

const createUseAction = <T>(atom: WritableAtom<T, any, void>) => {
  return () => useSetAtom(atom); // ✅
};

export const useManager = <T extends AtomManager<any>>(manager: T) => {
  const selectors = Object.fromEntries(
    Object.entries(manager.selectors).map(([key, atom]) => [
      key,
      createUseSelector(atom)(), // ✅
    ])
  ) as {
    [P in keyof T['selectors']]: T['selectors'][P] extends Atom<infer V>
      ? V
      : never;
  };

  const actions = Object.fromEntries(
    Object.entries(manager.actions).map(([key, actionAtom]) => [
      key,
      createUseAction(actionAtom)(), // ✅
    ])
  ) as unknown as {
    [P in keyof T['actions']]: T['actions'][P] extends WritableAtom<
      any,
      infer U,
      void
    >
      ? (param: U[0]) => void
      : never;
  };

  return { selectors, actions };
};