Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/susisu/tesseract
session and transaction
https://github.com/susisu/tesseract
Last synced: 11 days ago
JSON representation
session and transaction
- Host: GitHub
- URL: https://github.com/susisu/tesseract
- Owner: susisu
- License: mit
- Created: 2019-11-30T12:59:49.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:19:07.000Z (almost 2 years ago)
- Last Synced: 2024-11-29T00:10:24.242Z (28 days ago)
- Language: TypeScript
- Homepage:
- Size: 1.21 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @susisu/tesseract
[![CI](https://github.com/susisu/tesseract/workflows/CI/badge.svg)](https://github.com/susisu/tesseract/actions?query=workflow%3ACI)
``` shell
npm i @susisu/tesseract
# or
yarn add @susisu/tesseract
```## Usage
### Database
Manipulating database with transaction.``` typescript
import { AsyncSession } from "@susisu/tesseract";const session = new AsyncSession({
async initialize() {
await begin();
},
async finalize() {
await commit();
},
async handleError() {
await rollback();
},
});await session.transact(async () => {
await updateA(session);
await updateB(session);
});async function updateA(session: AsyncSession): Promise {
await session.transact(async () => {
await updateA0(session);
await updateA1(session);
});
}
```### Editor
Executing multiple edit operations while pushing at most one history to the undo buffer.``` typescript
import { Session } from "@susisu/tesseract";const session = new Session({
initialize() {
return dumpState();
},
finalize(oldState: State) {
pushHistoryIfUpdated(oldState);
},
handleError(error, phase, oldState: State | undefined) {
if (oldState === undefined) {
return;
}
pushHistoryIfUpdated(oldState);
},
});function pushHistoryIfUpdated(oldState: State): void {
const newState = dumpState();
if (!newState.equals(oldState)) {
pushHistory(oldState);
}
}session.transact(() => {
editA(session);
editB(session);
});
```### Emitter
Updating a state multiple times while emitting at most one update event.``` typescript
import { Session } from "@susisu/tesseract";const session = new Session({
initialize() {
return dumpState();
},
finalize(oldState: State) {
emitIfUpdated(oldState);
},
handleError(error, phase, oldState: State | undefined) {
if (oldState === undefined) {
return;
}
emitIfUpdated(oldState);
},
});function emitIfUpdated(oldState: State): void {
const newState = dumpState();
if (!newState.equals(oldState)) {
emit();
}
}session.transact(() => {
updateA(session);
updateB(session);
});
```## License
[MIT License](http://opensource.org/licenses/mit-license.php)
## Author
Susisu ([GitHub](https://github.com/susisu), [Twitter](https://twitter.com/susisu2413))