tim-smart / tim-smart/effect-atom

Proposal for multi-arg families

Open
#393 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
792
Forks
49
PR merge metrics
No merged PRs in 30d

Description

The usual advice for when people need multiple inputs to Atom.family is to create a composite object, but this can be cumbersome. More straightforward is to wrap rest-args in Data.array and use that as the key. Here's the util I'm using for this.

Would you consider enhancing Atom.family along these lines?

import { Array, Data, Option, Predicate } from "effect"
import { Atom } from "@effect-atom/atom-react"

export declare namespace family {
  interface Options {
    /**
     * Optimizes caching by omitting args for trailing `undefined` arguments.
     * This is useful for functions with optional arguments, treating
     * provided-`undefined` and absent-`undefined` the same way.
     *
     * This option should be disabled if:
     *
     * - The function has variadic arguments and `undefined` is a valid input
     * - The function distinguishes `f(1)` vs `f(1, undefined)` via
     *   `arguments.length` or `rest.length`
     *
     * @default true
     */
    readonly trimUndefined?: boolean
  }
}

/** Alternative to `Atom.family` that allows multiple args. */
export const family = <Args extends Array<unknown>, T extends object>(
  f: (...args: Args) => T,
  options?: family.Options,
): ((...args: Args) => T) => {
  const shouldTrim = options?. trimUndefined ?? true
  const singleArgFamily = Atom.family((argsArray: Args) => f(...argsArray))

  return (...args) => {
    if (shouldTrim) {
      const definedLength =
        (Option.getOrNull(
          Array.findLastIndex(args, Predicate.isNotUndefined),
        ) ?? -1) + 1
      if (definedLength < args.length) {
        args.length = definedLength
      }
    }
    Data.unsafeArray(args)

    return singleArgFamily(args)
  }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Atom.family entry point and reviewing its current keying and caching behavior. Compare it with the proposed multi-argument family utility, including the trimUndefined option, and define tests for distinct argument tuples and trailing undefined values. Done means the requested API behavior is documented and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.