Skip to content

Commit 0ace58f

Browse files
committed
add re-rxjs
1 parent 51578c7 commit 0ace58f

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,19 @@ in your browser, and click the button very quickly. (check the console log)
304304
check with intensive auto increment
305305
✓ check 8: updated properly with auto increment (3055 ms)
306306
✕ check 9: no tearing with auto increment (2 ms)
307+
re-rxjs
308+
check with events from outside
309+
✓ check 1: updated properly (8274 ms)
310+
✓ check 2: no tearing during update (1 ms)
311+
✓ check 3: ability to interrupt render
312+
✓ check 4: proper update after interrupt (2359 ms)
313+
check with useTransition
314+
✓ check 5: updated properly with transition (4509 ms)
315+
✓ check 6: no tearing with transition (3 ms)
316+
✓ check 7: proper branching with transition (2628 ms)
317+
check with intensive auto increment
318+
✓ check 8: updated properly with auto increment (6106 ms)
319+
✓ check 9: no tearing with auto increment
307320
```
308321

309322
</details>
@@ -554,6 +567,19 @@ in your browser, and click the button very quickly. (check the console log)
554567
<td>:white_check_mark:</td>
555568
<td>:x:</td>
556569
</tr>
570+
571+
<tr>
572+
<th><a href="https://github.com/re-rxjs/re-rxjs">re-rxjs</a></th>
573+
<td>:white_check_mark:</td>
574+
<td>:white_check_mark:</td>
575+
<td>:white_check_mark:</td>
576+
<td>:white_check_mark:</td>
577+
<td>:white_check_mark:</td>
578+
<td>:white_check_mark:</td>
579+
<td>:white_check_mark:</td>
580+
<td>:white_check_mark:</td>
581+
<td>:white_check_mark:</td>
582+
</tr>
557583
</table>
558584

559585
## Caution

__tests__/all_spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const port = process.env.PORT || '8080';
44

55
const names = [
6+
're-rxjs',
67
'react-redux',
78
'redux-use-mutable-source',
89
'reactive-react-redux',

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"build:react-apollo": "cross-env NAME=react-apollo webpack",
3535
"build:recoil": "cross-env NAME=recoil webpack",
3636
"build:effector": "cross-env NAME=effector webpack",
37+
"build:re-rxjs": "cross-env NAME=re-rxjs webpack",
3738
"build-all": "run-s build:*"
3839
},
3940
"keywords": [
@@ -55,6 +56,7 @@
5556
"graphql": "^15.0.0",
5657
"mobx": "^5.15.4",
5758
"mobx-react-lite": "^2.0.6",
59+
"re-rxjs": "^0.1.0",
5860
"react": "experimental",
5961
"react-dom": "experimental",
6062
"react-hooks-global-state": "^1.0.0",
@@ -64,6 +66,7 @@
6466
"reactive-react-redux": "^4.9.0",
6567
"recoil": "^0.0.7",
6668
"redux": "^4.0.5",
69+
"rxjs": "^6.5.5",
6770
"storeon": "^2.0.2",
6871
"use-context-selector": "^1.1.1",
6972
"use-subscription": "^1.4.1",

src/re-rxjs/index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React, { unstable_useTransition as useTransition } from "react";
2+
import { Subject, merge, asapScheduler } from "rxjs";
3+
import { map, scan, startWith, observeOn } from "rxjs/operators";
4+
import { connectObservable } from "re-rxjs";
5+
import {
6+
syncBlock,
7+
useRegisterIncrementDispatcher,
8+
ids,
9+
useCheckTearing,
10+
} from "../common";
11+
12+
const normalClicks$ = new Subject();
13+
const normalIncrement = () => normalClicks$.next();
14+
15+
const externalClicks$ = new Subject();
16+
// This is only needed to prevent the tearing with auto increment (check #9)
17+
const externalClicksAsap$ = externalClicks$.pipe(observeOn(asapScheduler));
18+
const externalIncrement = () => externalClicks$.next();
19+
20+
const [useCounter] = connectObservable(
21+
merge(normalClicks$, externalClicksAsap$).pipe(
22+
scan((x) => x + 0.5, 0),
23+
map(Math.floor),
24+
startWith(0)
25+
)
26+
);
27+
28+
const Counter = React.memo(() => {
29+
const count = useCounter();
30+
syncBlock();
31+
return <div className="count">{count}</div>;
32+
});
33+
34+
const Main = () => {
35+
const count = useCounter();
36+
useCheckTearing();
37+
useRegisterIncrementDispatcher(externalIncrement);
38+
const [localCount, localIncrement] = React.useReducer((c) => c + 1, 0);
39+
const [startTransition, isPending] = useTransition();
40+
const transitionIncrement = () => {
41+
startTransition(() => {
42+
normalIncrement();
43+
});
44+
};
45+
46+
return (
47+
<div>
48+
<button
49+
type="button"
50+
id="normalIncrement"
51+
onClick={isPending ? transitionIncrement : normalIncrement}
52+
>
53+
Increment shared count normally (two clicks to increment one)
54+
</button>
55+
<button
56+
type="button"
57+
id="transitionIncrement"
58+
onClick={transitionIncrement}
59+
>
60+
Increment shared count in transition (two clicks to increment one)
61+
</button>
62+
<span id="pending">{isPending && "Pending..."}</span>
63+
<h1>Shared Count</h1>
64+
{ids.map((id) => (
65+
<Counter key={id} />
66+
))}
67+
<div className="count">{count}</div>
68+
<h1>Local Count</h1>
69+
{localCount}
70+
<button type="button" id="localIncrement" onClick={localIncrement}>
71+
Increment local count
72+
</button>
73+
</div>
74+
);
75+
};
76+
77+
export default Main;

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6852,6 +6852,11 @@ [email protected]:
68526852
iconv-lite "0.4.24"
68536853
unpipe "1.0.0"
68546854

6855+
re-rxjs@^0.1.0:
6856+
version "0.1.0"
6857+
resolved "https://registry.yarnpkg.com/re-rxjs/-/re-rxjs-0.1.0.tgz#2bb74c717b4a036684047180c67f35af9465ea23"
6858+
integrity sha512-0cLFD5rhsbtPw302Usb57oxELzMeZQNDivDLs5ymPrEoFbXSHD/+S0M0e+sm4H+VE6zfz8Ca5y06YlF1zkCOmw==
6859+
68556860
react-dom@experimental:
68566861
version "0.0.0-experimental-33c3af284"
68576862
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-0.0.0-experimental-33c3af284.tgz#d4acd21209a299832d9233ceafc25a6fe1b1d076"
@@ -7278,7 +7283,7 @@ rx@^4.1.0:
72787283
resolved "https://registry.yarnpkg.com/rx/-/rx-4.1.0.tgz#a5f13ff79ef3b740fe30aa803fb09f98805d4782"
72797284
integrity sha1-pfE/957zt0D+MKqAP7CfmIBdR4I=
72807285

7281-
rxjs@^6.5.3:
7286+
rxjs@^6.5.3, rxjs@^6.5.5:
72827287
version "6.5.5"
72837288
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.5.tgz#c5c884e3094c8cfee31bf27eb87e54ccfc87f9ec"
72847289
integrity sha512-WfQI+1gohdf0Dai/Bbmk5L5ItH5tYqm3ki2c5GdWhKjalzjg93N3avFjVStyZZz+A2Em+ZxKH5bNghw9UeylGQ==

0 commit comments

Comments
 (0)