Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

1.3 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import { arrayShallowEquals } from "./arrays";

export function once<F extends (...args: never[]) => unknown>(fn: F): F;
export function once<Args extends unknown[], Result, This>(
  fn: (this: This, ...args: Args) => Result,
): (this: This, ...args: Args) => Result {
  let result: Result;
  let called = false;
  return function (this: This, ...args: Args): Result {
    if (!called) {
      called = true;
      result = fn.apply(this, args);
    }
    return result;
  };
}

export function memoizeLastValue<F extends (...args: never[]) => unknown>(
  fn: F,
): F;
export function memoizeLastValue<Args extends unknown[], Result, This>(
  fn: (this: This, ...args: Args) => Result,
): (this: This, ...args: Args) => Result {
  let result: Result;
  let lastArgs: Args | undefined;
  let lastError: unknown;
  let hasError = false;

  return function (this: This, ...args: Args): Result {
    if (lastArgs === undefined || !arrayShallowEquals(args, lastArgs)) {
      try {
        result = fn.apply(this, args);
        hasError = false;
        lastError = undefined;
      } catch (error) {
        hasError = true;
        lastError = error;
      }
      lastArgs = args;
    }

    if (hasError) {
      throw lastError;
    }
    return result;
  };
}