-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path11-hooks.js
43 lines (40 loc) · 1.22 KB
/
11-hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { withFields, string, number } from "@commodo/fields";
import { pipe } from "ramda";
import { withProps } from "repropose";
import { withHooks } from "@commodo/hooks";
// We've wrapped this code sample into an async function, so we can
// make function calls with the await keyword, and make our code look nicer.
(async () => {
try {
const Book = pipe(
withFields({
title: string(),
views: number({ value: 0 })
}),
withProps({
async incrementViews(increment = 1) {
this.views = this.views + increment;
await this.hook("viewsIncremented", increment);
}
}),
withHooks({
async viewsIncremented(increment) {
console.log(
`Views incremented on the ${this.title} book by ${increment}.`
);
}
})
)();
const bookA = new Book();
bookA.title = "Book A";
await bookA.incrementViews(10);
console.log("Book A views:", bookA.views);
const bookB = new Book();
bookB.title = "Book B";
await bookB.incrementViews(20);
console.log("Book B views:", bookB.views);
} catch (e) {
console.log("Error message: ", e.message);
console.log("Error data: ", e.data);
}
})();