|
1 | 1 | import { type Element } from 'hast'; |
2 | | -import { type CSSProperties, useRef } from 'react'; |
| 2 | +import { type CSSProperties, memo, useRef } from 'react'; |
3 | 3 |
|
4 | | -import { getNow } from './internal'; |
| 4 | +import { getNow, isDeepEqual } from './internal'; |
5 | 5 | import { STREAM_FADE_DURATION } from './styles'; |
6 | 6 |
|
7 | 7 | // hast-util-to-jsx-runtime keys siblings as `<tag>-<n>`, so dropping a faded |
@@ -47,43 +47,56 @@ export const createStreamTailNode = (data: StreamTailData): Element => ({ |
47 | 47 | export const readStreamTailData = (node?: Element): StreamTailData | undefined => |
48 | 48 | (node?.data as StreamTailElementData | undefined)?.streamdown; |
49 | 49 |
|
| 50 | +interface StreamTailProps { |
| 51 | + node?: Element; |
| 52 | +} |
| 53 | + |
50 | 54 | // Delays are frozen per mounted instance, not in the plugin runtime: a |
51 | 55 | // structural remount (a paragraph turning into a setext heading swaps <p> |
52 | 56 | // for <h2>) restarts every CSS animation, and the fresh instance has to |
53 | 57 | // resume each char from its real elapsed time instead of replaying the |
54 | 58 | // delay captured at its first ever render. |
55 | | -export const StreamTail = ({ node }: { node?: Element }) => { |
56 | | - const delaysRef = useRef<Map<number, CSSProperties | null>>(new Map()); |
57 | | - const data = readStreamTailData(node); |
58 | | - if (!data) return null; |
| 59 | +// |
| 60 | +// Every rehype run rebuilds the node, so memo compares the tail data it |
| 61 | +// carries. Skipping a render is safe: per-key delays are already frozen in |
| 62 | +// delaysRef, so an equal tail would produce identical output. |
| 63 | +export const StreamTail = memo<StreamTailProps>( |
| 64 | + ({ node }) => { |
| 65 | + const delaysRef = useRef<Map<number, CSSProperties | null>>(new Map()); |
| 66 | + const data = readStreamTailData(node); |
| 67 | + if (!data) return null; |
| 68 | + |
| 69 | + const now = getNow(); |
| 70 | + const styleOf = (item: StreamCharItem): CSSProperties | null => { |
| 71 | + if (item.birth === null) return null; |
| 72 | + const cached = delaysRef.current.get(item.key); |
| 73 | + if (cached !== undefined) return cached; |
| 74 | + const elapsed = now - item.birth; |
| 75 | + const style = elapsed >= STREAM_FADE_DURATION ? null : { animationDelay: `${-elapsed}ms` }; |
| 76 | + delaysRef.current.set(item.key, style); |
| 77 | + return style; |
| 78 | + }; |
59 | 79 |
|
60 | | - const now = getNow(); |
61 | | - const styleOf = (item: StreamCharItem): CSSProperties | null => { |
62 | | - if (item.birth === null) return null; |
63 | | - const cached = delaysRef.current.get(item.key); |
64 | | - if (cached !== undefined) return cached; |
65 | | - const elapsed = now - item.birth; |
66 | | - const style = elapsed >= STREAM_FADE_DURATION ? null : { animationDelay: `${-elapsed}ms` }; |
67 | | - delaysRef.current.set(item.key, style); |
68 | | - return style; |
69 | | - }; |
| 80 | + return ( |
| 81 | + <> |
| 82 | + {data.text} |
| 83 | + {data.items.map((item) => { |
| 84 | + if (typeof item === 'string') return item; |
| 85 | + const style = styleOf(item); |
| 86 | + return ( |
| 87 | + <span |
| 88 | + className={style ? 'stream-char' : 'stream-char stream-char-revealed'} |
| 89 | + key={item.key} |
| 90 | + style={style ?? undefined} |
| 91 | + > |
| 92 | + {item.value} |
| 93 | + </span> |
| 94 | + ); |
| 95 | + })} |
| 96 | + </> |
| 97 | + ); |
| 98 | + }, |
| 99 | + (prev, next) => isDeepEqual(readStreamTailData(prev.node), readStreamTailData(next.node)), |
| 100 | +); |
70 | 101 |
|
71 | | - return ( |
72 | | - <> |
73 | | - {data.text} |
74 | | - {data.items.map((item) => { |
75 | | - if (typeof item === 'string') return item; |
76 | | - const style = styleOf(item); |
77 | | - return ( |
78 | | - <span |
79 | | - className={style ? 'stream-char' : 'stream-char stream-char-revealed'} |
80 | | - key={item.key} |
81 | | - style={style ?? undefined} |
82 | | - > |
83 | | - {item.value} |
84 | | - </span> |
85 | | - ); |
86 | | - })} |
87 | | - </> |
88 | | - ); |
89 | | -}; |
| 102 | +StreamTail.displayName = 'StreamTail'; |
0 commit comments