JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q41649F
  • License MIT

A lightweight function wrapper with optional state, history, undo/redo, and reset support. Use like a normal function or unlock powerful time-travel features.

Package Exports

  • executor-fn

Readme

<<<<<<< HEAD # executor-fn

๐Ÿš€ executor-fn

A tiny, powerful utility for wrapping any function with immediate execution,
history tracking, undo/redo, and stateful function calls.


โœจ Features

  • ๐Ÿ”ฅ Immediate Execution โ€” call your function right away with callNow
  • ๐Ÿง  Stateful Functions โ€” access .value for the latest result
  • โช Undo / Redo Support โ€” automatically stores history if enabled
  • ๐Ÿงฉ Lightweight โ€” zero dependencies, works in Node, Vanilla JS, and React
  • ๐ŸŽฏ Perfect for Tenary Expressions โ€” run multiple lines inside ?: easily

๐Ÿ“ฆ Installation

npm install executor-fn

---

## ๐Ÿ–Š Advanced Example: Mini Text Editor

You can even use `Executor` to power a text editor with full undo/redo:

```jsx
import React, { useState } from "react";
import Executor from "executor-fn";

export default function TextEditorApp() {
  const editor = Executor((_, newValue) => newValue, {
    storeHistory: true,
    callNow: true,
    initialArgs: [""],
  });

  const [text, setText] = useState(editor.value);
  const updateUI = () => setText(editor.value);

  return (
    <div>
      <textarea
        rows="4"
        cols="40"
        value={text}
        onChange={(e) => {
          editor(editor.value, e.target.value);
          updateUI();
        }}
      />
      <button onClick={() => { editor.undo(); updateUI(); }}>Undo</button>
      <button onClick={() => { editor.redo(); updateUI(); }}>Redo</button>
      <button onClick={() => { editor.reset(); updateUI(); }}>Reset</button>
      <pre>{JSON.stringify(editor.history, null, 2)}</pre>
    </div>
  );
}

๐Ÿ“„ **Full example: examples/text-editor.jsx**

## React Todo App Example

Hereโ€™s a full-featured React app using `Executor` as a global store
(with undo/redo support out-of-the-box):

- Multiple components share the same `todosStore`.
- Undo/Redo history is automatic โ€” no boilerplate!
- No Redux, no Zustand โ€” just one function.

๐Ÿ“‚ **See full code in [`examples/react-todo-app`](./examples/react-todo-app)**

---

[![Open in CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/martino-kevo/executor-fn/tree/main/demo)

> ๐ŸŽฎ **Try executor-fn live in your browser!**

## ๐Ÿ”ฅ React Hook Support (`useExecutor`)

You can now bind an `Executor` instance directly to your React components without manually forcing re-renders.

```jsx
import React from "react";
import Executor, { useExecutor } from "executor-fn";

const counterStore = Executor((count, delta) => count + delta, {
  storeHistory: true,
  callNow: true,
  initialArgs: [0],
});

export default function CounterApp() {
  const count = useExecutor(counterStore); // Auto-updates on state change

  return (
    <div>
      <h2>Count: {count}</h2>
      <button onClick={() => counterStore(counterStore.value, 1)}>โž•</button>
      <button onClick={() => counterStore(counterStore.value, -1)}>โž–</button>
      <button onClick={counterStore.undo}>โช Undo</button>
      <button onClick={counterStore.redo}>โฉ Redo</button>
    </div>
  );
}



>>>>>>> c428633 (Initial commit: executor-fn)