Skip to content

Commit

Permalink
dropping previous subscription on element update
Browse files Browse the repository at this point in the history
  • Loading branch information
kosich committed Oct 9, 2020
1 parent 7809aea commit 251208a
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@ export function $(props) {
useEffect(() => {
// array of children, one of em might be observable
if (Array.isArray(children)) {
setCn(children.map(c => createElement($, null, c)));
setCn(
// recursively create another $ for Observables
children.map((c) => (isObservable(c) ? createElement($, null, c) : c))
);
return;
}

// child is a single observable
if (isObservable(children)) {
children
.pipe(
takeUntil(destroy$)
)
.subscribe({
next: setCn, // update the view
error(error) { // wrap error in an object to be safe in case the error is nullish
setError({ error });
}
});
const sub = children.pipe(takeUntil(destroy$)).subscribe({
next: setCn, // update the view
error(error) {
// wrap error in an object to be safe in case the error is nullish
setError({ error });
},
// on complete we just keep displaying accumulated value
});

return;
return () => sub.unsubscribe();
}

// child is a regular child, like you and me
Expand All @@ -63,7 +64,5 @@ export function $(props) {
// without additional <>
// this might be an unwanted behavior difference
// TODO: research for real-life effects of this difference
return Array.isArray(cn)
? createElement(Fragment, null, ...cn)
: cn
return Array.isArray(cn) ? createElement(Fragment, null, ...cn) : cn;
}

0 comments on commit 251208a

Please sign in to comment.