Skip to content

Commit 442aa19

Browse files
author
Duke
committed
fix: corrected usePipe impleemntation
1 parent 5623c3c commit 442aa19

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

bun.lockb

0 Bytes
Binary file not shown.

examples/react/bun.lockb

0 Bytes
Binary file not shown.

examples/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "react",
33
"version": "0.0.0",
44
"dependencies": {
5-
"@submodule/core": "^9.3.2",
6-
"@submodule/react": "^1.0.5",
5+
"@submodule/core": "^9.3.3",
6+
"@submodule/react": "^1.0.6",
77
"react": "^18.3.1",
88
"react-dom": "^18.3.1"
99
},

examples/react/src/App.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import React, { Suspense } from "react";
2-
import { ScopeProvider, useController, useObservable } from "@submodule/react";
3-
import { counter, config } from "../subs/counter";
1+
import React, { Suspense, useCallback, useState } from "react";
2+
import {
3+
ScopeProvider,
4+
useController,
5+
useObservable,
6+
useObservableN,
7+
usePipe,
8+
type PipeDispatcher,
9+
} from "@submodule/react";
10+
import { counter, config, onlyOddStream } from "../subs/counter";
411

512
export default function App() {
613
return (
@@ -14,11 +21,20 @@ export default function App() {
1421
);
1522
}
1623

24+
const includeEven: PipeDispatcher<number, number> = (value, set) => {
25+
if (value % 2 === 0) {
26+
set(value);
27+
}
28+
};
29+
1730
function Counter() {
1831
const counterValue = useObservable(config);
1932
const controller = useController(config);
2033
const counterApp = useObservable(counter);
2134

35+
const onlyOdd = useObservableN(onlyOddStream, 1);
36+
const onlyEven = usePipe(counter, includeEven, 0);
37+
2238
return (
2339
<>
2440
<div>
@@ -62,6 +78,8 @@ function Counter() {
6278
<div>
6379
<h2>Counter</h2>
6480
<div>{counterApp}</div>
81+
<div>only odd: {onlyOdd}</div>
82+
<div>only even: {onlyEven}</div>
6583
</div>
6684
</>
6785
);

examples/react/subs/counter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,10 @@ export const counter = flatMap(config, config => {
7575
}
7676
})
7777

78+
})
79+
80+
export const onlyOddStream = pipe<number, number>(counter, (v, set) => {
81+
if (v % 2 !== 0) {
82+
set(v)
83+
}
7884
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.1.0",
44
"description": "make use of @submodule/core in react",
55
"dependencies": {
6-
"@submodule/core": "^9.3.2"
6+
"@submodule/core": "^9.3.3"
77
},
88
"peerDependencies": {
99
"react": "*"

src/index.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ export function useObservableN<P>(
176176
[resource],
177177
);
178178

179-
return useSyncExternalStore(subs, () => resource.get() || defaultValue);
179+
return useSyncExternalStore(subs, () => {
180+
return resource.get() || defaultValue;
181+
});
180182
}
181183

182184
/**
@@ -198,8 +200,25 @@ export function usePipe<Value, Upstream>(
198200
ppipe: PipeDispatcher<Value, Upstream>,
199201
defaultValue: Value,
200202
): Value {
201-
const piped = useMemo(() => pipe(upstream, ppipe), [upstream, ppipe]);
202-
return useObservableN(piped, defaultValue);
203+
const [value, setValue] = useState(defaultValue);
204+
const u = useResolve(upstream);
205+
206+
useEffect(() => {
207+
let mounted = true;
208+
209+
const cleanup = u.pipe(ppipe, (next) => {
210+
if (mounted) {
211+
setValue(next);
212+
}
213+
});
214+
215+
return () => {
216+
mounted = false;
217+
cleanup();
218+
};
219+
}, [ppipe, u]);
220+
221+
return value;
203222
}
204223

205224
export type { PipeDispatcher } from "@submodule/core";

0 commit comments

Comments
 (0)