chain-store 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +140 -52
- package/dist/index.es.js +144 -149
- package/dist/index.umd.js +1 -1
- package/dist/store.d.ts +1 -1
- package/dist/types.d.ts +23 -19
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -1,51 +1,135 @@
|
|
|
1
|
-
|
|
2
|
-
<path d="M197.587 12.5661H205.578V19.4548H197.587V28.9343C197.587 30.6612 197.881 31.8739 198.469 32.572C199.093 33.2332 200.214 33.5093 201.83 33.3991C202.601 33.3624 203.244 33.1968 203.759 32.903C204.31 32.6091 204.696 32.4437 204.917 32.4069L205.853 39.2409C205.486 39.4981 204.935 39.7553 204.2 40.0124C203.502 40.2696 202.749 40.4719 201.94 40.6189C201.132 40.8025 200.415 40.8942 199.791 40.8942C196.154 41.0045 193.398 40.2142 191.524 38.5241C189.687 36.834 188.768 34.1703 188.768 30.5329V19.4548H184.47V12.5661H189.485L191.028 3.14132H197.587V12.5661ZM174.724 23.6394C176.687 23.9218 178.229 24.4123 179.344 25.1335C180.592 25.9416 181.454 26.9149 181.931 28.0534C182.409 29.1917 182.649 30.3662 182.649 31.5778C182.649 32.7903 182.375 33.9691 181.824 35.1081C181.31 36.2469 180.517 37.2586 179.452 38.1403C178.387 38.9847 177.027 39.6454 175.375 40.1228C173.721 40.6371 171.81 40.8942 169.642 40.8942C166.85 40.8942 164.442 40.5255 162.422 39.7907C160.402 39.0559 158.785 38.0271 157.573 36.7048C156.982 36.0392 156.493 35.3243 156.115 34.5564L165.027 32.987C165.312 33.1597 165.636 33.3185 166.006 33.4528C166.814 33.7467 167.898 33.8923 169.257 33.8923C170.431 33.8923 171.349 33.8022 172.011 33.6189C172.708 33.4353 173.206 33.1778 173.5 32.8474C173.794 32.4801 173.944 32.0004 173.944 31.4128C173.944 30.7526 173.665 30.2748 173.114 29.9812C172.955 29.8965 172.76 29.8194 172.531 29.7439C173.988 28.1032 174.81 25.9363 174.724 23.6394ZM222.528 11.7946C225.908 11.7946 228.72 12.4374 230.961 13.7234C233.202 14.9726 234.892 16.6996 236.031 18.904C237.17 21.1085 237.739 23.5889 237.739 26.3444C237.739 29.1367 237.17 31.6351 236.031 33.8396C234.892 36.0073 233.202 37.7343 230.961 39.0202C228.72 40.2695 225.908 40.8942 222.528 40.8942C219.111 40.8942 216.282 40.2694 214.041 39.0202C211.8 37.7343 210.11 36.0073 208.971 33.8396C207.868 31.6351 207.317 29.1367 207.317 26.3444C207.317 23.5888 207.868 21.1085 208.971 18.904C210.11 16.6995 211.8 14.9726 214.041 13.7234C216.282 12.4374 219.111 11.7946 222.528 11.7946ZM278.494 11.7946C281.948 11.7946 284.777 12.4374 286.981 13.7234C289.186 14.9726 290.821 16.6995 291.887 18.904C292.989 21.1085 293.54 23.5888 293.54 26.3444V28.8239H272.487C272.561 30.514 273.203 31.7816 274.416 32.6267C275.628 33.435 277.208 33.8395 279.155 33.8396C280.294 33.8396 281.268 33.7109 282.076 33.4538C282.921 33.1599 283.602 32.7922 284.116 32.3513C284.667 31.9105 285.126 31.4509 285.493 30.9734L293.264 34.115C292.236 35.5846 291.097 36.8342 289.848 37.863C288.635 38.855 287.165 39.6084 285.438 40.1228C283.748 40.6371 281.654 40.8942 279.155 40.8942C275.701 40.8942 272.799 40.2694 270.447 39.0202C268.096 37.7343 266.314 36.0073 265.101 33.8396C263.926 31.6351 263.338 29.1367 263.338 26.3444C263.338 23.5888 263.871 21.1085 264.936 18.904C266.039 16.6996 267.71 14.9726 269.951 13.7234C272.192 12.4374 275.04 11.7947 278.494 11.7946ZM258.709 11.904C259.664 11.904 260.583 11.9967 261.465 12.1804C262.346 12.3273 263.063 12.5287 263.614 12.7859L260.362 20.447C259.958 20.1898 259.517 19.9506 259.039 19.7302C258.598 19.4732 257.918 19.3444 257 19.3444C255.677 19.3445 254.501 19.6574 253.473 20.2819C252.444 20.8698 251.636 21.7518 251.048 22.9275C250.497 24.1031 250.222 25.5363 250.222 27.2263V40.2331H241.403V12.6765H250.222V15.8171C251.066 15.0089 251.893 14.3291 252.701 13.778C253.546 13.1902 254.447 12.7308 255.402 12.4001C256.357 12.0695 257.46 11.904 258.709 11.904ZM222.528 19.2341C221.058 19.2341 219.846 19.4922 218.89 20.0066C217.935 20.5209 217.238 21.3105 216.797 22.3757C216.356 23.4045 216.135 24.7278 216.135 26.3444C216.135 27.9977 216.356 29.3571 216.797 30.4226C217.238 31.4512 217.935 32.2227 218.89 32.737C219.846 33.2147 221.058 33.4538 222.528 33.4538C223.998 33.4538 225.192 33.2146 226.11 32.737C227.029 32.2227 227.708 31.4513 228.149 30.4226C228.627 29.3571 228.866 27.9977 228.866 26.3444C228.866 24.7278 228.627 23.4045 228.149 22.3757C227.708 21.3104 227.029 20.5209 226.11 20.0066C225.192 19.4922 223.998 19.2341 222.528 19.2341ZM168.983 11.7946C171.26 11.7948 173.336 12.142 175.209 12.8396C177.119 13.5009 178.7 14.4776 179.949 15.7634C181.235 17.0125 182.096 18.5386 182.537 20.3386L174.33 21.4958L174.159 21.0651C174.143 21.0313 174.122 21 174.105 20.9665C174.087 20.9307 174.071 20.8942 174.052 20.8591C173.969 20.7095 173.876 20.564 173.769 20.4235C173.745 20.3916 173.719 20.3611 173.693 20.3298C173.594 20.2076 173.485 20.089 173.365 19.9753C173.34 19.9514 173.315 19.9265 173.289 19.903L173.244 19.8591L173.181 19.8142L172.877 19.5808L172.827 19.5398L172.801 19.5222L172.782 19.5085C172.644 19.4167 172.489 19.3378 172.32 19.2624L172.28 19.24C172.259 19.231 172.239 19.2213 172.218 19.2126C172.135 19.1784 172.047 19.1494 171.957 19.1189C171.901 19.0998 171.845 19.0778 171.787 19.0603C171.678 19.0277 171.564 18.9981 171.446 18.9704C171.412 18.9623 171.378 18.9517 171.343 18.9441C171.29 18.9327 171.236 18.9271 171.181 18.9167C170.897 18.8615 170.592 18.8157 170.27 18.7868L170.225 18.7819H170.177L169.639 18.7595C169.459 18.7526 169.273 18.7375 169.082 18.737H169.037C168.566 18.7371 168.128 18.7592 167.723 18.7907C167.484 18.8092 167.256 18.8287 167.041 18.8581C167.027 18.86 167.011 18.8611 166.997 18.863L166.44 18.9567C166.422 18.9606 166.404 18.9663 166.387 18.9704C165.719 19.1192 165.223 19.3718 164.902 19.7282C164.572 20.0585 164.408 20.5012 164.408 21.0515C164.408 21.3188 164.459 21.5515 164.561 21.7468C164.359 22.334 163.858 22.7976 163.207 22.9128L156.389 24.111C155.933 23.0687 155.698 21.993 155.698 20.8855C155.699 19.1597 156.196 17.6163 157.187 16.2575C158.179 14.8618 159.65 13.777 161.597 13.0056C163.581 12.1974 166.044 11.7947 168.983 11.7946ZM278.494 18.8484C276.694 18.8484 275.315 19.235 274.36 20.0066C273.405 20.7781 272.836 21.8436 272.652 23.2028H284.171C284.134 21.9169 283.62 20.8696 282.628 20.0612C281.672 19.2529 280.294 18.8484 278.494 18.8484Z" />
|
|
3
|
-
<path d="M14.9902 11.7939C17.7459 11.7939 20.1165 12.2354 22.1006 13.1172C24.0845 13.9622 25.6831 15.1562 26.8955 16.6992C28.1079 18.2056 28.9889 19.9327 29.54 21.8799L20.502 22.4863C19.9509 21.5311 19.2343 20.7597 18.3525 20.1719C17.5075 19.5473 16.3864 19.2344 14.9902 19.2344C13.7044 19.2344 12.6021 19.5104 11.6836 20.0615C10.7652 20.6127 10.0485 21.4209 9.53418 22.4863C9.05662 23.515 8.8184 24.8008 8.81836 26.3438C8.81836 28.6953 9.35049 30.4779 10.416 31.6904C11.5182 32.8661 13.0431 33.454 14.9902 33.4541C16.3864 33.4541 17.5075 33.1601 18.3525 32.5723C19.2343 31.9844 19.9508 31.1942 20.502 30.2021L29.54 30.7529C29.0257 32.737 28.1626 34.5008 26.9502 36.0439C25.7377 37.5504 24.1394 38.7451 22.1553 39.627C20.1712 40.472 17.7826 40.8945 14.9902 40.8945C11.6836 40.8945 8.90917 40.2697 6.66797 39.0205C4.42692 37.7346 2.75555 36.0074 1.65332 33.8398C0.551054 31.6353 0 29.1362 0 26.3438C5.60273e-05 23.5884 0.5512 21.1087 1.65332 18.9043C2.7555 16.6999 4.42697 14.9728 6.66797 13.7236C8.90917 12.4377 11.6836 11.794 14.9902 11.7939ZM81.0645 11.7939C84.5915 11.8307 87.3101 12.3639 89.2207 13.3926C91.168 14.4214 92.5282 15.7995 93.2998 17.5264C94.0712 19.2163 94.457 21.1451 94.457 23.3125V40.1221H85.8594V37.1465C85.345 37.8078 84.6465 38.4326 83.7646 39.0205C82.8829 39.5716 81.8354 40.0121 80.623 40.3428C79.4106 40.7101 78.0881 40.8945 76.6553 40.8945C74.4508 40.8945 72.5953 40.5268 71.0889 39.792C69.6192 39.0571 68.4981 38.0278 67.7266 36.7051C66.9552 35.3826 66.5694 33.9132 66.5693 32.2969V31.4697C66.5693 30.5144 66.7896 29.4671 67.2305 28.3281C67.7081 27.1891 68.5721 26.1787 69.8213 25.2969C71.0705 24.3784 72.8712 23.8093 75.2227 23.5889L82.1113 22.9268C83.5075 22.8165 84.4083 22.6511 84.8125 22.4307C85.253 22.2102 85.4736 21.8612 85.4736 21.3838C85.4736 20.3552 85.1058 19.6575 84.3711 19.29C83.673 18.8859 82.4603 18.6836 80.7334 18.6836C79.5577 18.6836 78.584 18.7753 77.8125 18.959C77.041 19.1427 76.435 19.4187 75.9941 19.7861C75.5901 20.1535 75.314 20.649 75.167 21.2734L66.9551 19.7861C67.3225 18.4268 67.9474 17.2508 68.8291 16.2588C69.7109 15.23 70.7582 14.385 71.9707 13.7236C73.2199 13.0623 74.616 12.566 76.1592 12.2354C77.7024 11.9047 79.3376 11.7572 81.0645 11.7939ZM131.335 11.7939C134.236 11.794 136.569 12.3818 138.332 13.5566C140.132 14.6956 141.439 16.3158 142.247 18.4102C143.055 20.5039 143.458 22.9469 143.458 25.7383V26.3887L136.673 27.5908C135.727 27.7547 134.826 27.1308 134.641 26.1875V26.0703C134.641 23.6822 134.238 21.9512 133.43 20.8857C132.621 19.821 131.095 19.2891 128.854 19.2891C127.606 19.2891 126.466 19.6193 125.438 20.2803C124.409 20.9416 123.582 21.8803 122.957 23.0928C122.369 24.2684 122.073 25.6479 122.073 27.2275V40.2344H113.256V12.6777H122.073V15.8174C122.918 15.0091 123.823 14.3105 124.778 13.7227C125.732 13.1357 126.742 12.6761 127.806 12.3457C128.908 11.9784 130.086 11.794 131.335 11.7939ZM143.458 40.2344H134.641V38.2383C135.057 38.2261 135.479 38.1965 135.901 38.1221L143.458 36.7852V40.2344ZM41.6406 15.4316C42.9266 14.2192 44.3048 13.3193 45.7744 12.7314C47.2807 12.1069 48.8973 11.794 50.624 11.7939C53.4899 11.7939 55.8416 12.364 57.6787 13.5029C59.5158 14.6419 60.876 16.2582 61.7578 18.3525C62.6396 20.4468 63.0801 22.8723 63.0801 25.6279V40.1221H54.0967L54.207 25.958C54.207 24.3048 54.0227 22.9822 53.6553 21.9902C53.3246 20.9983 52.7187 20.2997 51.8369 19.8955C50.9552 19.4547 49.7611 19.2344 48.2549 19.2344C46.9322 19.2344 45.7743 19.5104 44.7822 20.0615C43.7904 20.5759 43.0188 21.3654 42.4678 22.4307C41.9534 23.4962 41.6963 24.8744 41.6963 26.5645L41.6406 40.1221H32.8232V0H41.6406V15.4316ZM108.021 40.1221H99.2031V12.5654H108.021V40.1221ZM85.9697 27.502C85.6758 27.6857 85.1975 27.8322 84.5361 27.9424C83.8749 28.0158 83.0852 28.1076 82.167 28.2178L79.0254 28.5488C77.9231 28.6591 77.1509 28.9161 76.71 29.3203C76.2692 29.6877 76.0488 30.2578 76.0488 31.0293V31.1943C76.0488 31.7821 76.1406 32.2965 76.3242 32.7373C76.5446 33.1414 76.9125 33.4534 77.4268 33.6738C77.9411 33.8575 78.6577 33.9502 79.5762 33.9502C80.7151 33.9135 81.6888 33.748 82.4971 33.4541C83.3051 33.1235 83.9482 32.7376 84.4258 32.2969C84.9034 31.8192 85.1974 31.3778 85.3076 30.9736C85.5648 30.5695 85.7302 30.1101 85.8037 29.5957C85.9139 29.0447 85.9697 28.3468 85.9697 27.502ZM103.612 0.550781C104.862 0.550781 105.928 0.992208 106.81 1.87402C107.691 2.75575 108.132 3.82128 108.132 5.07031C108.132 6.31934 107.691 7.38487 106.81 8.2666C105.928 9.14841 104.862 9.58984 103.612 9.58984C102.4 9.58979 101.352 9.14836 100.471 8.2666C99.5892 7.38486 99.1484 6.31936 99.1484 5.07031C99.1485 3.82126 99.5891 2.75576 100.471 1.87402C101.352 0.992264 102.4 0.550837 103.612 0.550781Z" />
|
|
4
|
-
<path d="M170.111 20.6182C171.032 20.7009 171.535 20.8868 171.766 21.0398L172.071 21.273C172.337 21.5146 172.493 21.7756 172.582 22.0534L172.788 22.7082C172.791 22.7244 172.799 22.7413 172.802 22.7576C173.461 26.5034 170.959 30.0797 167.213 30.7411L135.584 36.3161C131.838 36.9764 128.262 34.4737 127.6 30.7277C126.987 27.2425 129.111 23.9075 132.426 22.928C132.625 23.5421 132.762 24.421 132.767 25.7492C132.761 25.9874 132.778 26.232 132.821 26.4757C133.168 28.4348 135.037 29.7443 136.997 29.4001L163.526 24.7221C165.381 24.395 166.652 22.7022 166.491 20.8604C166.573 20.8305 166.68 20.7874 166.823 20.7572C167.354 20.6455 168.086 20.5778 169.039 20.5778L170.111 20.6182Z" fill="#FF4343"/>
|
|
5
|
-
<path d="M154.22 18.241C153.985 19.0873 153.861 19.9723 153.861 20.8918C153.861 22.1159 154.102 23.3015 154.547 24.444L145.299 26.0721V25.7402C145.299 23.6576 145.08 21.7168 144.639 19.9319L154.22 18.241Z" fill="#FF4343"/>
|
|
6
|
-
</svg>
|
|
1
|
+
# chain-store
|
|
7
2
|
|
|
8
|
-
**
|
|
3
|
+
**A simple event-based state manager.** It works with just two functions!
|
|
9
4
|
|
|
5
|
+
```
|
|
6
|
+
/** Any package manager */
|
|
7
|
+
|
|
8
|
+
> npm install chain-store
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Actions
|
|
13
|
+
|
|
14
|
+
Create an action, trigger it, and subscribe or unsubscribe to updates:
|
|
10
15
|
```ts
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
import { createAction } from "chain-store";
|
|
17
|
+
import type { Action, Subscription } from "chain-store";
|
|
18
|
+
|
|
19
|
+
/* Create */
|
|
20
|
+
const setNumber: Action<number> = createAction<number>()
|
|
21
|
+
|
|
22
|
+
/* Subscribe */
|
|
23
|
+
const subscription: Subscription = setNumber.watch(console.log)
|
|
24
|
+
|
|
25
|
+
/* Trigger */
|
|
26
|
+
setNumber(42); // -> 42
|
|
27
|
+
|
|
28
|
+
/* Unsubscribe */
|
|
29
|
+
subscription.dispose();
|
|
13
30
|
```
|
|
14
31
|
|
|
15
|
-
|
|
32
|
+
|
|
33
|
+
### Chaining
|
|
34
|
+
Every action can be chained with other actions using the `.map` and `.filter` methods.
|
|
35
|
+
These methods take a callback as input. The callback returns a new value for `.map`
|
|
36
|
+
and a boolean for `.filter`. Callbacks can be asynchronous.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const asString = setNumber.map((n: number) => n.toString());
|
|
40
|
+
const asObject = asString.map(async (s: string) => ({ value: s }));
|
|
41
|
+
const negativeOnly = setNumber.filter((n: number) => n < 0)
|
|
42
|
+
const squareRoot = setNumber.filter((n: number) => n >= 0).map(Math.sqrt)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
All actions in a chain are connected, and triggering any of them will trigger
|
|
47
|
+
the others. Each action triggers in the order of chaining. Subscriptions
|
|
48
|
+
fire after all callbacks for that action have resolved, so asynchronous callbacks
|
|
49
|
+
will take longer to process.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
/* Subscribe to see the output */
|
|
53
|
+
asString.watch(s => console.log("asString", s));
|
|
54
|
+
asObject.watch(o => console.log("asObject", o));
|
|
55
|
+
negativeOnly.watch(n => console.log("negativeOnly", n));
|
|
56
|
+
squareRoot.watch(n => console.log("squareRoot", n));
|
|
57
|
+
|
|
58
|
+
asObject(16); // -> "asString", "16"
|
|
59
|
+
// -> "squareRoot", 4
|
|
60
|
+
// -> "asObject", { value: "16" } */
|
|
61
|
+
|
|
62
|
+
/* "negativeOnly" is filtered out */
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
squareRoot(-10); // -> "asString", "-10"
|
|
66
|
+
// -> "negativeOnly", -10
|
|
67
|
+
// -> "asObject", { value: "-10" }
|
|
68
|
+
|
|
69
|
+
/* "squareRoot" is filtered out, even though it was the original trigger */
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Of course, you can choose which actions to watch. And there’s no need to create
|
|
73
|
+
each action separately:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const chainAction = createAction<number>()
|
|
77
|
+
.filter(n > 0)
|
|
78
|
+
.map(n => Math.pow(n, 2))
|
|
79
|
+
.filter(n < 1000)
|
|
80
|
+
.map(n => `Power of two is: ${n}`)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
### Async Actions
|
|
85
|
+
An async action is the end of a chain. The main difference between an async action
|
|
86
|
+
and a regular action is that an async action has `.done`, `.fail`, and `.finally` methods,
|
|
87
|
+
which can be watched just like triggers.
|
|
88
|
+
|
|
89
|
+
Create an async action by chaining an action with the `.target` method:
|
|
90
|
+
|
|
16
91
|
```ts
|
|
17
92
|
import { createAction } from "chain-store";
|
|
18
|
-
import type {
|
|
93
|
+
import type { AsyncAction } from "chain-store";
|
|
94
|
+
|
|
95
|
+
const asyncAction: AsyncAction<number, { data: number }>
|
|
96
|
+
= createAction<number>().target(async (payload: number) => ({ data: payload }))
|
|
19
97
|
|
|
20
|
-
|
|
21
|
-
|
|
98
|
+
asynsAction.watch((payload: number) =>
|
|
99
|
+
console.log("start", payload));
|
|
22
100
|
|
|
23
|
-
|
|
24
|
-
|
|
101
|
+
asyncActions.done.watch((result: { data: number }, payload: number) =>
|
|
102
|
+
console.log("done", result, payload));
|
|
103
|
+
|
|
104
|
+
asyncActions.fail.watch((error: Error, payload: number) =>
|
|
105
|
+
console.log("fail", error, payload));
|
|
106
|
+
|
|
107
|
+
asyncActions.finally.watch((payload: number) =>
|
|
108
|
+
console.log("finally", payload));
|
|
109
|
+
```
|
|
25
110
|
|
|
26
|
-
|
|
27
|
-
|
|
111
|
+
Async actions return an `ActionPromise`. It’s like a promise, but it isn’t one.
|
|
112
|
+
It’s useful when you need to wait for an async action to complete before doing
|
|
113
|
+
something elsewhere in the app, outside the store.
|
|
28
114
|
|
|
29
|
-
|
|
30
|
-
|
|
115
|
+
```ts
|
|
116
|
+
const actionPromise = asyncAction(20);
|
|
31
117
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
);
|
|
118
|
+
actionPromise.done((result: { data: number }, payload: number) => {
|
|
119
|
+
console.log(result, params); // -> { data: 20 }, 20, "done"
|
|
120
|
+
console.log(actionPromise.state); // -> "done"
|
|
121
|
+
});
|
|
36
122
|
|
|
37
|
-
//
|
|
38
|
-
powerOfTwo.done.watch((powered) => console.log(`square is ${powered}`));
|
|
123
|
+
console.log(actionPromise.state); // -> "pending"
|
|
39
124
|
|
|
40
|
-
// trigger async action
|
|
41
|
-
powerOfTwo(4);
|
|
42
125
|
|
|
43
|
-
// output:
|
|
44
|
-
// "value is 4" - comes from setNumber action since all actions in chain are executed at once
|
|
45
|
-
// "square is 16"
|
|
46
126
|
```
|
|
47
127
|
|
|
48
|
-
|
|
128
|
+
**Note I:** An async action fails only if its own callback is rejected. Any other
|
|
129
|
+
rejection earlier in the chain simply stops further execution, so no fail event
|
|
130
|
+
occurs in that case.
|
|
131
|
+
|
|
132
|
+
### Store
|
|
49
133
|
|
|
50
134
|
```ts
|
|
51
135
|
import { createStore, createAction } from "chain-store";
|
|
@@ -53,43 +137,47 @@ import { createStore, createAction } from "chain-store";
|
|
|
53
137
|
type State = { name: string, age: number };
|
|
54
138
|
const initialState: State = { name: "John", age: 30 };
|
|
55
139
|
|
|
56
|
-
// create store
|
|
57
|
-
const store = createStore<State>(initialState);
|
|
58
140
|
|
|
59
|
-
|
|
60
|
-
|
|
141
|
+
const changeName = createAction<string>();
|
|
142
|
+
|
|
143
|
+
/* Create a store */
|
|
144
|
+
const store = createStore<State>(initialState);
|
|
61
145
|
|
|
62
|
-
|
|
146
|
+
/* Subscribe the store to an action */
|
|
63
147
|
store.on(changeName, (state: State, name: string) => {
|
|
64
148
|
return { ...state, name: name };
|
|
65
149
|
});
|
|
66
150
|
|
|
67
|
-
|
|
68
|
-
store.watch((state: State) => console.log(`name is ${state.name}`));
|
|
151
|
+
/* Subscribe to store changes */
|
|
152
|
+
const subscription = store.watch((state: State) => console.log(`My name is ${state.name}`));
|
|
69
153
|
|
|
70
|
-
changeName("Alex");
|
|
71
|
-
|
|
72
|
-
|
|
154
|
+
changeName("Alex"); // -> "My name is Alex"
|
|
155
|
+
|
|
156
|
+
/* Unsubscribe from store changes */
|
|
157
|
+
subscription.dispose();
|
|
158
|
+
|
|
159
|
+
/* Unsubscribe the store from the action */
|
|
160
|
+
store.off(changeName)
|
|
73
161
|
```
|
|
74
|
-
|
|
162
|
+
|
|
163
|
+
The store also works with mutable state, if needed:
|
|
164
|
+
|
|
75
165
|
```ts
|
|
76
166
|
const setAge = createAction<number>();
|
|
77
167
|
|
|
78
168
|
store.on(setAge, (state: State, age: number) => {
|
|
79
|
-
state.age = age;
|
|
169
|
+
state.age = age; /* State mutation */
|
|
80
170
|
return state;
|
|
81
171
|
});
|
|
82
172
|
|
|
83
|
-
|
|
84
|
-
store
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// output:
|
|
89
|
-
// "Now I'm 42"
|
|
173
|
+
/* Subscribe to partial state updates */
|
|
174
|
+
store
|
|
175
|
+
.map((state: State) => state.age)
|
|
176
|
+
.watch((age: number) => console.log(`Now I'm ${age}`));
|
|
90
177
|
|
|
91
|
-
//
|
|
178
|
+
setAge(42); // -> "Now I'm 42"
|
|
92
179
|
|
|
180
|
+
/* The store.watch() subscription won't be triggered
|
|
181
|
+
because the state is the same object */
|
|
93
182
|
```
|
|
94
183
|
|
|
95
|
-
Complete documentation is coming soon... ¯\\\_(ツ)_/¯
|
package/dist/index.es.js
CHANGED
|
@@ -1,170 +1,165 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const B = () => j(), C = (n) => {
|
|
2
|
+
const e = [], o = [];
|
|
3
|
+
let r = {}, u = [], t = !1, s = n;
|
|
4
|
+
const d = (c) => {
|
|
5
|
+
var a;
|
|
6
|
+
return (a = y(e, void 0, (h) => h[0] === c)) == null ? void 0 : a[1].dispose();
|
|
7
|
+
}, g = {
|
|
8
|
+
on: (c, a) => (d(c), e.push([
|
|
9
|
+
c,
|
|
10
|
+
c.watch((...h) => {
|
|
11
|
+
var f;
|
|
12
|
+
t = !0;
|
|
13
|
+
const i = o.map(([b]) => b(s)), l = a(s, ...h);
|
|
14
|
+
l !== s && (s = l, (f = r.emit) == null || f.call(r, s)), o.forEach(([b, w], x) => {
|
|
15
|
+
const A = b(s);
|
|
16
|
+
A !== (i == null ? void 0 : i[x]) && w(A);
|
|
17
|
+
}), t = !1, u.forEach((b) => b()), u = [];
|
|
18
|
+
})
|
|
19
|
+
]), g),
|
|
20
|
+
off: (c) => (d(c), g),
|
|
21
|
+
map: (c) => ({
|
|
22
|
+
getState: () => c(s),
|
|
23
|
+
watch: (a) => (o.push([c, a]), {
|
|
24
|
+
dispose: () => {
|
|
25
|
+
const h = () => y(
|
|
26
|
+
o,
|
|
27
|
+
void 0,
|
|
28
|
+
([i, l]) => i === c && l === a
|
|
29
|
+
);
|
|
30
|
+
t ? u.push(h) : h();
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
}),
|
|
34
|
+
getState: () => s,
|
|
35
|
+
watch: S(r)
|
|
36
|
+
};
|
|
37
|
+
return g;
|
|
38
|
+
}, j = (n, e, o, r) => {
|
|
39
|
+
let u = {};
|
|
40
|
+
const t = [], s = (c, a) => {
|
|
41
|
+
var h;
|
|
42
|
+
(h = u.emit) == null || h.call(u, c), t.forEach((i) => i(c, a));
|
|
43
|
+
}, d = (c, a) => {
|
|
6
44
|
try {
|
|
7
|
-
|
|
45
|
+
if (o) return q(c, o, (h) => s(h, a));
|
|
46
|
+
if (r)
|
|
47
|
+
return q(
|
|
48
|
+
c,
|
|
49
|
+
r,
|
|
50
|
+
(h) => h && s(c, a)
|
|
51
|
+
);
|
|
52
|
+
s(c, a);
|
|
8
53
|
} catch {
|
|
9
|
-
return;
|
|
10
54
|
}
|
|
11
55
|
};
|
|
12
|
-
return
|
|
13
|
-
|
|
56
|
+
return e == null || e(d), Object.assign((c) => {
|
|
57
|
+
(n ?? d)(c);
|
|
14
58
|
}, {
|
|
15
|
-
|
|
16
|
-
map: (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
}) : m(i, { target: s }),
|
|
26
|
-
filter: (s) => t != null && t.filter ? y(i, {
|
|
27
|
-
filter: async (r) => !!await t.filter(
|
|
28
|
-
r
|
|
29
|
-
) && s(r)
|
|
30
|
-
}) : y(i, { filter: s })
|
|
59
|
+
watch: S(u),
|
|
60
|
+
map: (c) => j(n ?? d, E(t), c),
|
|
61
|
+
filter: (c) => j(n ?? d, E(t), void 0, c),
|
|
62
|
+
target: (c, a) => K(
|
|
63
|
+
n ?? d,
|
|
64
|
+
E(t),
|
|
65
|
+
c,
|
|
66
|
+
a
|
|
67
|
+
)
|
|
31
68
|
});
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
!
|
|
69
|
+
}, K = (n, e, o, r) => {
|
|
70
|
+
let u = {}, t = {}, s = {}, d = {};
|
|
71
|
+
const g = [], c = async (i, l) => {
|
|
72
|
+
var x, A, O, P, D, m;
|
|
73
|
+
const f = o(i);
|
|
74
|
+
if (!(f instanceof Promise)) {
|
|
75
|
+
(x = u.emit) == null || x.call(u, i), (A = t.emit) == null || A.call(t, f, i), l == null || l.done(f, i);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const b = g.includes(f), w = (r == null ? void 0 : r.skipDuplicates) !== !1;
|
|
79
|
+
!b && w && g.push(f), (!b || !w) && ((O = u.emit) == null || O.call(u, i));
|
|
43
80
|
try {
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
l.emit("fail")(u, r), n == null || n.fail(u, r);
|
|
81
|
+
const p = await f;
|
|
82
|
+
(!b || !w) && ((P = t.emit) == null || P.call(t, p, i)), l == null || l.done(p, i);
|
|
83
|
+
} catch (p) {
|
|
84
|
+
(!b || !w) && ((D = s.emit) == null || D.call(s, p, i)), l == null || l.fail(p, i);
|
|
49
85
|
} finally {
|
|
50
|
-
|
|
51
|
-
|
|
86
|
+
!b || !w ? (m = d.emit) == null || m.call(d, i) : window.requestAnimationFrame(() => {
|
|
87
|
+
y(g, f);
|
|
88
|
+
}), l == null || l.finish(i);
|
|
52
89
|
}
|
|
53
|
-
}
|
|
54
|
-
|
|
90
|
+
};
|
|
91
|
+
return e(async (i, l) => {
|
|
55
92
|
try {
|
|
56
|
-
|
|
57
|
-
a(o, n);
|
|
93
|
+
await c(i, l);
|
|
58
94
|
} catch {
|
|
59
|
-
return;
|
|
60
95
|
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return i.forEach((f) => {
|
|
65
|
-
c === f ? c(r, o) : f(r);
|
|
66
|
-
}), h;
|
|
96
|
+
}), Object.assign((i) => {
|
|
97
|
+
const { events: l, methods: f } = I();
|
|
98
|
+
return n(i, l), f;
|
|
67
99
|
}, {
|
|
68
|
-
|
|
69
|
-
done:
|
|
70
|
-
fail:
|
|
71
|
-
finish:
|
|
72
|
-
filter: (r) => m(i, {
|
|
73
|
-
...t,
|
|
74
|
-
filter: t.filter ? async (n) => await t.filter(n) && r(n) : r
|
|
75
|
-
}),
|
|
76
|
-
map: (r, n) => m(i, {
|
|
77
|
-
target: r,
|
|
78
|
-
match: n,
|
|
79
|
-
map: async (o) => {
|
|
80
|
-
const h = t.map ? await t.map(o) : o, f = await t.target(h);
|
|
81
|
-
if (t.filter && !await t.filter(f)) throw new Error();
|
|
82
|
-
return f;
|
|
83
|
-
}
|
|
84
|
-
})
|
|
100
|
+
watch: S(u),
|
|
101
|
+
done: { watch: S(t) },
|
|
102
|
+
fail: { watch: S(s) },
|
|
103
|
+
finish: { watch: S(d) }
|
|
85
104
|
});
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
})
|
|
103
|
-
]), s),
|
|
104
|
-
off: (r) => (c(r), s),
|
|
105
|
-
map: (r) => ({
|
|
106
|
-
getState: () => r(a),
|
|
107
|
-
watch: (n) => (l.push([r, n]), {
|
|
108
|
-
dispose: () => {
|
|
109
|
-
const o = l.findIndex((h) => h[0] === r);
|
|
110
|
-
l.splice(o, 1);
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
}),
|
|
114
|
-
getState: () => a,
|
|
115
|
-
watch: (r) => {
|
|
116
|
-
e.init("watch").watch(r);
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
return s;
|
|
120
|
-
}, w = (i) => {
|
|
121
|
-
const t = {};
|
|
122
|
-
return {
|
|
123
|
-
events: {
|
|
124
|
-
done: (e, a) => {
|
|
125
|
-
var c;
|
|
126
|
-
i.result = e, i.params = a, (c = t.done) == null || c.call(t, e, a);
|
|
127
|
-
},
|
|
128
|
-
fail: (e, a) => {
|
|
129
|
-
var c;
|
|
130
|
-
i.error = e, i.params = a, (c = t.fail) == null || c.call(t, e, a);
|
|
131
|
-
},
|
|
132
|
-
finally: (e) => {
|
|
105
|
+
}, q = (n, e, o) => {
|
|
106
|
+
if (!e) return o(n);
|
|
107
|
+
const r = e(n);
|
|
108
|
+
if (!(r instanceof Promise)) return o(r);
|
|
109
|
+
r.then(o).catch(() => {
|
|
110
|
+
});
|
|
111
|
+
}, F = ["done", "fail", "finish"], I = (n) => {
|
|
112
|
+
const e = {}, o = n ?? {
|
|
113
|
+
state: "pending",
|
|
114
|
+
stored: [],
|
|
115
|
+
set: (t) => o.state = t,
|
|
116
|
+
controller: new z(() => o.state)
|
|
117
|
+
}, r = F.reduce(
|
|
118
|
+
(t, s, d) => ({
|
|
119
|
+
...t,
|
|
120
|
+
[s]: (g, c) => {
|
|
133
121
|
var a;
|
|
134
|
-
|
|
122
|
+
o.stored[d] = g, d < 2 && o.set(s), (a = e[s]) == null || a.call(e, g, c);
|
|
135
123
|
}
|
|
136
|
-
},
|
|
137
|
-
methods: {
|
|
138
|
-
done: t.done ? void 0 : (e) => (t.done = e, i.result && i.params && e(i.result, i.params), w(i).methods),
|
|
139
|
-
fail: t.fail ? void 0 : (e) => (t.fail = e, i.error && i.params && e(i.error, i.params), w(i).methods),
|
|
140
|
-
finally: t.finally ? void 0 : (e) => (t.finally = e, i.params && e(i.params), w(i).methods)
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
}, p = () => {
|
|
144
|
-
const i = {}, t = (e, a) => {
|
|
145
|
-
var c;
|
|
146
|
-
(c = i[e]) != null && c.includes(a) && (i[e] = i[e].filter(
|
|
147
|
-
(s) => s !== a
|
|
148
|
-
));
|
|
149
|
-
}, l = (e, a) => {
|
|
150
|
-
var c;
|
|
151
|
-
(c = i[e]) != null && c.includes(a) || (i[e] = [...i[e] ?? [], a]);
|
|
152
|
-
};
|
|
153
|
-
return {
|
|
154
|
-
init: (e) => ({
|
|
155
|
-
watch: (a) => (l(e, a), {
|
|
156
|
-
dispose: () => t(e, a)
|
|
157
|
-
})
|
|
158
124
|
}),
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
125
|
+
{}
|
|
126
|
+
), u = (t, s) => ({
|
|
127
|
+
[t]: e[t] ? void 0 : (d) => (e[t] = d, o.state !== "pending" && //@ts-expect-error
|
|
128
|
+
d(o.stored[s], s < 2 ? o.stored[2] : void 0), I(o).methods)
|
|
129
|
+
});
|
|
130
|
+
return {
|
|
131
|
+
events: r,
|
|
132
|
+
methods: Object.assign(
|
|
133
|
+
o.controller,
|
|
134
|
+
F.reduce(
|
|
135
|
+
(t, s, d) => ({
|
|
136
|
+
...t,
|
|
137
|
+
...u(s, d)
|
|
138
|
+
}),
|
|
139
|
+
{}
|
|
140
|
+
)
|
|
141
|
+
)
|
|
165
142
|
};
|
|
166
143
|
};
|
|
144
|
+
class z {
|
|
145
|
+
constructor(e) {
|
|
146
|
+
this.getState = e;
|
|
147
|
+
}
|
|
148
|
+
get state() {
|
|
149
|
+
return this.getState();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const E = (n) => (e) => n.push(e), y = (n, e, o) => {
|
|
153
|
+
const r = n.findIndex(o ?? ((u) => e === u));
|
|
154
|
+
return r >= 0 ? n.splice(r, 1)[0] : void 0;
|
|
155
|
+
}, S = (n) => (e) => {
|
|
156
|
+
if (n != null && n.watch) return n.watch(e);
|
|
157
|
+
const o = [], r = (t) => y(o, t), u = (t) => !o.includes(t) && o.push(t);
|
|
158
|
+
return n.watch = (t) => (u(t), {
|
|
159
|
+
dispose: () => r(t)
|
|
160
|
+
}), n.emit = (...t) => o.forEach((s) => s(...t)), n.watch(e);
|
|
161
|
+
};
|
|
167
162
|
export {
|
|
168
|
-
|
|
169
|
-
|
|
163
|
+
B as createAction,
|
|
164
|
+
C as createStore
|
|
170
165
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(w,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(w=typeof globalThis<"u"?globalThis:w||self,y(w["chain-store"]={}))})(this,function(w){"use strict";const y=()=>E(),M=n=>{const s=[],e=[];let r={},u=[],t=!1,o=n;const d=c=>{var l;return(l=x(s,void 0,a=>a[0]===c))==null?void 0:l[1].dispose()},g={on:(c,l)=>(d(c),s.push([c,c.watch((...a)=>{var h;t=!0;const i=e.map(([b])=>b(o)),f=l(o,...a);f!==o&&(o=f,(h=r.emit)==null||h.call(r,o)),e.forEach(([b,p],m)=>{const A=b(o);A!==(i==null?void 0:i[m])&&p(A)}),t=!1,u.forEach(b=>b()),u=[]})]),g),off:c=>(d(c),g),map:c=>({getState:()=>c(o),watch:l=>(e.push([c,l]),{dispose:()=>{const a=()=>x(e,void 0,([i,f])=>i===c&&f===l);t?u.push(a):a()}})}),getState:()=>o,watch:S(r)};return g},E=(n,s,e,r)=>{let u={};const t=[],o=(c,l)=>{var a;(a=u.emit)==null||a.call(u,c),t.forEach(i=>i(c,l))},d=(c,l)=>{try{if(e)return P(c,e,a=>o(a,l));if(r)return P(c,r,a=>a&&o(c,l));o(c,l)}catch{}};return s==null||s(d),Object.assign(c=>{(n??d)(c)},{watch:S(u),map:c=>E(n??d,O(t),c),filter:c=>E(n??d,O(t),void 0,c),target:(c,l)=>z(n??d,O(t),c,l)})},z=(n,s,e,r)=>{let u={},t={},o={},d={};const g=[],c=async(i,f)=>{var m,A,q,F,I,K;const h=e(i);if(!(h instanceof Promise)){(m=u.emit)==null||m.call(u,i),(A=t.emit)==null||A.call(t,h,i),f==null||f.done(h,i);return}const b=g.includes(h),p=(r==null?void 0:r.skipDuplicates)!==!1;!b&&p&&g.push(h),(!b||!p)&&((q=u.emit)==null||q.call(u,i));try{const j=await h;(!b||!p)&&((F=t.emit)==null||F.call(t,j,i)),f==null||f.done(j,i)}catch(j){(!b||!p)&&((I=o.emit)==null||I.call(o,j,i)),f==null||f.fail(j,i)}finally{!b||!p?(K=d.emit)==null||K.call(d,i):window.requestAnimationFrame(()=>{x(g,h)}),f==null||f.finish(i)}};return s(async(i,f)=>{try{await c(i,f)}catch{}}),Object.assign(i=>{const{events:f,methods:h}=D();return n(i,f),h},{watch:S(u),done:{watch:S(t)},fail:{watch:S(o)},finish:{watch:S(d)}})},P=(n,s,e)=>{if(!s)return e(n);const r=s(n);if(!(r instanceof Promise))return e(r);r.then(e).catch(()=>{})},T=["done","fail","finish"],D=n=>{const s={},e=n??{state:"pending",stored:[],set:t=>e.state=t,controller:new B(()=>e.state)},r=T.reduce((t,o,d)=>({...t,[o]:(g,c)=>{var l;e.stored[d]=g,d<2&&e.set(o),(l=s[o])==null||l.call(s,g,c)}}),{}),u=(t,o)=>({[t]:s[t]?void 0:d=>(s[t]=d,e.state!=="pending"&&d(e.stored[o],o<2?e.stored[2]:void 0),D(e).methods)});return{events:r,methods:Object.assign(e.controller,T.reduce((t,o,d)=>({...t,...u(o,d)}),{}))}};class B{constructor(s){this.getState=s}get state(){return this.getState()}}const O=n=>s=>n.push(s),x=(n,s,e)=>{const r=n.findIndex(e??(u=>s===u));return r>=0?n.splice(r,1)[0]:void 0},S=n=>s=>{if(n!=null&&n.watch)return n.watch(s);const e=[],r=t=>x(e,t),u=t=>!e.includes(t)&&e.push(t);return n.watch=t=>(u(t),{dispose:()=>r(t)}),n.emit=(...t)=>e.forEach(o=>o(...t)),n.watch(s)};w.createAction=y,w.createStore=M,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"})});
|
package/dist/store.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
|
+
type ActionCall<Params, Result extends unknown = void> = (params: Params) => Result;
|
|
2
|
+
export type ActionPromise<Params, Result, Error, Methods = ActionPromiseEvents<Params, Result, Error>, Remaining extends keyof Methods = keyof Methods> = {
|
|
3
|
+
state: "pending" | "done" | "fail";
|
|
4
|
+
} & {
|
|
5
|
+
[K in Remaining]: (fn: Methods[K]) => ActionPromise<Params, Result, Error, Methods, Exclude<Remaining, K>>;
|
|
6
|
+
};
|
|
7
|
+
export type ActionPromiseEvents<Params = any, Result = any, Error = any> = {
|
|
8
|
+
done: (result: Result, params: Params) => void;
|
|
9
|
+
fail: (error: Error, params: Params) => void;
|
|
10
|
+
finish: (params: Params) => void;
|
|
11
|
+
};
|
|
1
12
|
export type Args<Params> = Params extends Array<unknown> ? Params : [Params];
|
|
2
|
-
export type
|
|
3
|
-
|
|
13
|
+
export type AsyncActionOptions = {
|
|
14
|
+
skipDuplicates?: boolean;
|
|
4
15
|
};
|
|
16
|
+
export type ChainFn<Input, Output> = (params: Input) => Output | Promise<Output>;
|
|
17
|
+
export interface Subscription {
|
|
18
|
+
dispose: VoidFunction;
|
|
19
|
+
}
|
|
5
20
|
export interface Unit<Params> {
|
|
6
21
|
watch: (watcher: (...args: Args<Params>) => void) => Subscription;
|
|
7
22
|
}
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
map: <Mapped, Err = Error>(target: ChainFn<Input, Mapped>, match?: (input: Input, prev: Input) => boolean) => AsyncAction<Input, Mapped, Err>;
|
|
23
|
+
export interface Action<Input, Output = Input> extends ActionCall<Input>, Unit<Output> {
|
|
24
|
+
filter: (fn: ChainFn<Output, boolean>) => Action<Input, Output>;
|
|
25
|
+
map: <Mapped>(fn: ChainFn<Output, Mapped>) => Action<Input, Mapped>;
|
|
26
|
+
target: <Result, Err = Error>(fn: ChainFn<Output, Result>) => AsyncAction<Input, Result, Output, Err>;
|
|
13
27
|
}
|
|
14
|
-
|
|
15
|
-
done: (result: Result, params: Params) => void;
|
|
16
|
-
fail: (error: Error, params: Params) => void;
|
|
17
|
-
finally: (params: Params) => void;
|
|
18
|
-
}, Remaining extends keyof Methods = keyof Methods> = {
|
|
19
|
-
[K in Remaining]: (fn: Methods[K]) => ActionPromise<Params, Result, Error, Methods, Exclude<Remaining, K>>;
|
|
20
|
-
};
|
|
21
|
-
export type AsyncActionCall<Input, Params, Result, Err = Error> = (input: Input) => ActionPromise<Params, Result, Err>;
|
|
22
|
-
export interface AsyncAction<Input, Result, Params = Input, Err = Error> extends AsyncActionCall<Input, Params, Result, Err>, Unit<Input> {
|
|
23
|
-
map: <Mapped>(target: ChainFn<Result, Mapped>, match?: (params: Result, prev: Result) => boolean) => AsyncAction<Input, Mapped, Result, Err>;
|
|
24
|
-
filter: (filter: ChainFn<Result, boolean>) => AsyncAction<Input, Result, Params, Err>;
|
|
28
|
+
export interface AsyncAction<Input, Result, Params = Input, Err = Error> extends ActionCall<Input, ActionPromise<Params, Result, Err>>, Unit<Params> {
|
|
25
29
|
done: {
|
|
26
30
|
watch: (watcher: (result: Result, params: Params) => void) => Subscription;
|
|
27
31
|
};
|
|
@@ -39,6 +43,6 @@ export interface ReadStore<State> {
|
|
|
39
43
|
export interface Store<State> extends ReadStore<State> {
|
|
40
44
|
on: <T>(action: Unit<T>, f: (state: State, ...args: Args<T>) => State) => Store<State>;
|
|
41
45
|
off: <T>(action: Unit<T>) => Store<State>;
|
|
42
|
-
map: <Part>(mapper: (state: State) => Part) =>
|
|
46
|
+
map: <Part>(mapper: (state: State) => Part) => ReadStore<Part>;
|
|
43
47
|
}
|
|
44
48
|
export {};
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chain-store",
|
|
3
|
-
"
|
|
3
|
+
"description": "Simple event-based state manager",
|
|
4
|
+
"version": "0.0.3",
|
|
4
5
|
"author": {
|
|
5
6
|
"name": "Aleksandr Gurianov",
|
|
6
7
|
"url": "http://golova.pro"
|
|
7
8
|
},
|
|
9
|
+
"homepage": "https://graverok.github.io/chain-store",
|
|
8
10
|
"repository": {
|
|
9
11
|
"type": "git",
|
|
10
12
|
"url": "git+https://github.com/graverok/chain-store.git"
|