Skip to content

Commit 3245df8

Browse files
committed
[docs] More transaction listener docs
1 parent 21d072a commit 3245df8

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

site/guides/10_releases.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,58 @@
33
This is a reverse chronological list of the major TinyBase releases, with
44
highlighted features.
55

6+
## v3.2
7+
8+
This release lets you add a listener to the start of a transaction. This lets
9+
you detect that a set of changes are about to be made to a Store.
10+
11+
To use this, call the addStartTransaction method on your Store. The listener you
12+
add can itself mutate the data in the Store.
13+
14+
From this release onwards, listeners added with the existing
15+
addWillFinishTransaction are also able to mutate data. Transactions added with
16+
the existing addDidFinishTransaction _cannot_ mutate data.
17+
18+
```js
19+
const store = createStore();
20+
21+
const startListenerId = store.addStartTransactionListener(() => {
22+
console.log('Start transaction');
23+
console.log(store.getTables());
24+
// Can mutate data
25+
});
26+
27+
const willFinishListenerId = store.addWillFinishTransactionListener(() => {
28+
console.log('Will finish transaction');
29+
console.log(store.getTables());
30+
// Can mutate data
31+
});
32+
33+
const didFinishListenerId = store.addDidFinishTransactionListener(() => {
34+
console.log('Did finish transaction');
35+
console.log(store.getTables());
36+
// Cannot mutate data
37+
});
38+
39+
store.setTable('pets', {fido: {species: 'dog'}});
40+
// -> 'Start transaction'
41+
// -> {}
42+
// -> 'Will finish transaction'
43+
// -> {pets: {fido: {species: 'dog'}}}
44+
// -> 'Did finish transaction'
45+
// -> {pets: {fido: {species: 'dog'}}}
46+
47+
store
48+
.delListener(startListenerId)
49+
.delListener(willFinishListenerId)
50+
.delListener(didFinishListenerId);
51+
52+
store.delTables();
53+
```
54+
55+
This release also fixes a bug where using the explicit startTransaction method
56+
_inside_ another listener could create infinite recursion.
57+
658
## v3.1
759

860
This new release adds a powerful schema-based type system to TinyBase.
@@ -55,7 +107,7 @@ existing tabular data, it allows you to get, set, and listen to, individual
55107
Value items, each with a unique Id.
56108

57109
```js
58-
const store = createStore().setValues({employees: 3, open: true});
110+
store.setValues({employees: 3, open: true});
59111
console.log(store.getValues());
60112
// -> {employees: 3, open: true}
61113

site/guides/1_the_basics/6_transactions.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,54 @@ console.log(store.getTables());
103103
// -> {pets: {fido: {species: 'dog', color: 'brown', sold: true}}}
104104
```
105105

106+
## Listening to transactions
107+
108+
You can register listeners to the start and finish of a transaction. There are
109+
three points in its lifecycle:
110+
111+
| Event | Add a listener with | When | Can mutate data |
112+
| ---------- | -------------------------------- | ----------------------------------------- | --------------- |
113+
| Start | addStartTransactionListener | Before changes | Yes |
114+
| WillFinish | addWillFinishTransactionListener | After changes and other mutator listeners | Yes |
115+
| DidFinish | addDidFinishTransactionListener | After non-mutator listeners | No |
116+
117+
For example:
118+
119+
```js
120+
store.delTables();
121+
122+
const startListenerId = store.addStartTransactionListener(() => {
123+
console.log('Start transaction');
124+
console.log(store.getTables());
125+
// Can mutate data
126+
});
127+
128+
const willFinishListenerId = store.addWillFinishTransactionListener(() => {
129+
console.log('Will finish transaction');
130+
console.log(store.getTables());
131+
// Can mutate data
132+
});
133+
134+
const didFinishListenerId = store.addDidFinishTransactionListener(() => {
135+
console.log('Did finish transaction');
136+
console.log(store.getTables());
137+
// Cannot mutate data
138+
});
139+
140+
store.setTable('pets', {fido: {species: 'dog'}});
141+
// -> 'Start transaction'
142+
// -> {}
143+
// -> 'Will finish transaction'
144+
// -> {pets: {fido: {species: 'dog'}}}
145+
// -> 'Did finish transaction'
146+
// -> {pets: {fido: {species: 'dog'}}}
147+
148+
store
149+
.delListener(startListenerId)
150+
.delListener(willFinishListenerId)
151+
.delListener(didFinishListenerId);
152+
```
153+
106154
## Summary
107155

108156
We've covered all of the essential basics of working with a TinyBase Store, but

0 commit comments

Comments
 (0)