-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
68 lines (46 loc) · 1.68 KB
/
main.js
File metadata and controls
68 lines (46 loc) · 1.68 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createStore, createEvent } from "effector";
const app = document.getElementById("app");
// 1
const addNumber = createEvent();
const $store = createStore(0).on(addNumber, (state, number) => state + number);
$store.watch((state) => {
app.textContent = state;
});
addNumber(10);
// 2
const extractPartOfArray = createEvent();
const array = createStore([]).on(extractPartOfArray, (_, arr) => arr.slice(2));
array.watch((part) => {
console.log(part);
});
extractPartOfArray([1, 2, 3, 4, 5]); // 3, 4, 5
// 3 map(fn)
const updateUser = createEvent();
const userNameUpdated = updateUser.map(({ name }) => name);
const userRoleUpdated = updateUser.map(({ role }) => role.toUpperCase());
userNameUpdated.watch((name) =>
console.log(`Имя пользователя изменено на ${name}`)
);
userRoleUpdated.watch((role) =>
console.log(`Роль пользователя изменена на ${role}`)
);
updateUser({ name: "john", role: "admin" });
// 4 prepend(fn)
const userPropertyChanged = createEvent();
userPropertyChanged.watch(({ field, value }) => {
console.log(`Свойство пользователя "${field}" изменено на ${value}`);
});
const changeName = userPropertyChanged.prepend((name) => ({
field: "name",
value: name,
}));
const changeRole = userPropertyChanged.prepend((role) => ({
field: "role",
value: role.toUpperCase(),
}));
changeName("john");
// => Свойство пользователя "name" изменено на john
changeRole("admin");
// => Свойство пользователя "role" изменено на ADMIN
changeName("alice");
// => Свойство пользователя "name" изменено на alice