Skip to content

Commit de0fc18

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

File tree

5 files changed

+115
-1
lines changed

5 files changed

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