This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
11165ee
commit 0f0f5ab
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* @typedef Part { import('../parts.js').Part } | ||
*/ | ||
import { directive, isNodePart } from '../index.js'; | ||
|
||
/** | ||
* Render items of an AsyncIterable, replacing previous items as they are resolved. | ||
* Not possible to render more than once in a server context, so only the first item is rendered. | ||
* | ||
* @type { (value: AsyncIterable<unknown>, mapper?: ((v: unknown, index?: number | undefined) => unknown) | undefined) => (part: Part) => void } | ||
*/ | ||
export const asyncReplace = directive((value, mapper) => (part) => { | ||
if (!isNodePart(part)) { | ||
throw Error('The `asyncReplace` directive can only be used in text nodes'); | ||
} | ||
|
||
part.setValue( | ||
value.next().then(({ value }) => { | ||
if (mapper !== undefined) { | ||
value = mapper(value, 0); | ||
} | ||
|
||
return value; | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters