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
.valuefor 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)**
---
[](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)