|
| 1 | +export type CommitOptions = { |
| 2 | + /** An optional human-readable description of the committed change */ |
| 3 | + title?: string; |
| 4 | + /** |
| 5 | + * If true, the commit will be squashed into the previous commit, |
| 6 | + * overriding the previous title if a title is also provided. |
| 7 | + */ |
| 8 | + squash?: boolean; |
| 9 | +}; |
| 10 | +/** Record of changes committed */ |
| 11 | +export interface Commit<Change> { |
| 12 | + /** The inverse of the changes that were committed */ |
| 13 | + undo: Change[]; |
| 14 | + /** The changes that were committed */ |
| 15 | + redo: Change[]; |
| 16 | + /** An optional human-readable description of the committed changes */ |
| 17 | + title?: string; |
| 18 | +} |
| 19 | +export type TransactedCallback<Change> = (txRecord: Commit<Change>) => void; |
| 20 | +/** |
| 21 | + * Transactor is an interface that defines a transaction manager for managing |
| 22 | + * changes in a system. It provides methods to commit changes, undo and redo |
| 23 | + * changes, and subscribe to transaction events. |
| 24 | + * @template Change - The type of changes that can be committed. |
| 25 | + */ |
| 26 | +export interface Transactor<Change> { |
| 27 | + /** Commits a change, returning the resultant record. */ |
| 28 | + commit(change: Change, options?: CommitOptions): Commit<Change>; |
| 29 | + /** Undoes the most reset `past` `Commit`, if any, returning it. */ |
| 30 | + undo(): Commit<Change> | undefined; |
| 31 | + /** Redoes the most recent `future` `Commit`, if any, returning it. */ |
| 32 | + redo(): Commit<Change> | undefined; |
| 33 | + /** All changes that have been committed and not yet undone. */ |
| 34 | + past: Commit<Change>[]; |
| 35 | + /** All changes that have been undone and can be redone. */ |
| 36 | + future: Commit<Change>[]; |
| 37 | + /** |
| 38 | + * Registers `txCallback`, which will be called on every new `commit`. |
| 39 | + * @returns a function that will **unsubscribe** the callback, returning it. |
| 40 | + */ |
| 41 | + subscribe( |
| 42 | + txCallback: TransactedCallback<Change>, |
| 43 | + ): () => TransactedCallback<Change>; |
| 44 | +} |
0 commit comments