@vortexlabs/kirari-react 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +5 -87
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +17 -17
- package/stream.d.ts +1 -0
- package/stream.js +1 -0
- package/dist/index.d.ts +0 -30
- package/dist/index.js +0 -121
- package/dist/stream.d.ts +0 -11
- package/dist/stream.js +0 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @vortexlabs/kirari-react
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ef4462a: Publish under unscoped names: `kirari`, `kirari-react`, and `kirari-rehype`. The `@vortexlabs/kirari`, `@vortexlabs/kirari-react`, and `@vortexlabs/kirari-rehype` packages now re-export them, so existing imports keep working.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [ef4462a]
|
|
12
|
+
- kirari@1.2.0
|
|
13
|
+
- kirari-react@1.2.0
|
|
14
|
+
|
|
15
|
+
## 1.1.1
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- e0cd534: Public object types such as `Token`, `Rule`, `Language`, `RenderOptions`, `Decoration`, `TokenPatch`, `TokenStream`, and `StreamedTokens` are now type aliases instead of interfaces. Their members are unchanged, but they can no longer be extended through declaration merging (module augmentation). Types that extend another type, such as `HighlightOptions` and `CodeProps`, remain interfaces.
|
|
20
|
+
|
|
3
21
|
## 1.1.0
|
|
4
22
|
|
|
5
23
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -1,95 +1,13 @@
|
|
|
1
1
|
# @vortexlabs/kirari-react
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[`@vortexlabs/kirari`](https://github.com/vortexkit/kirari/blob/main/packages/kirari/README.md).
|
|
5
|
-
`<Code>` renders code as React elements and works in Server Components. Use the
|
|
6
|
-
optional streaming hook for code that arrives in chunks.
|
|
3
|
+
This package has been renamed to [`kirari-react`](https://www.npmjs.com/package/kirari-react).
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
## Install
|
|
5
|
+
`@vortexlabs/kirari-react` re-exports `kirari-react` on every entry point, so existing imports keep working and keep receiving updates. New projects should install `kirari-react` directly:
|
|
11
6
|
|
|
12
7
|
```sh
|
|
13
|
-
npm install
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
Requires React 19. Kirari is a peer dependency.
|
|
17
|
-
|
|
18
|
-
## Usage
|
|
19
|
-
|
|
20
|
-
```tsx
|
|
21
|
-
import { Code } from '@vortexlabs/kirari-react'
|
|
22
|
-
import { typescript } from '@vortexlabs/kirari/languages/typescript'
|
|
23
|
-
import '@vortexlabs/kirari/theme.css'
|
|
24
|
-
|
|
25
|
-
export const CodeExample = () => (
|
|
26
|
-
<Code language={typescript}>
|
|
27
|
-
{'const greeting: string = "Hello"'}
|
|
28
|
-
</Code>
|
|
29
|
-
)
|
|
8
|
+
npm install kirari-react
|
|
30
9
|
```
|
|
31
10
|
|
|
32
|
-
|
|
33
|
-
`kirari-*` classes, so every Kirari theme works. Code text is escaped by React;
|
|
34
|
-
the component does not use `dangerouslySetInnerHTML`. Text children are joined
|
|
35
|
-
as React joins text, so `{value}` interpolation works; elements throw. `language`
|
|
36
|
-
defaults to JavaScript. Other props go to the `<pre>`.
|
|
37
|
-
|
|
38
|
-
## Lines and customization
|
|
39
|
-
|
|
40
|
-
Use `highlightLines={[1, [3, 4]]}` to mark line 1 and lines 3–4. Style
|
|
41
|
-
`.kirari-line-highlighted` to choose the highlight's appearance.
|
|
42
|
-
|
|
43
|
-
| Prop | Effect |
|
|
44
|
-
| --- | --- |
|
|
45
|
-
| `lines` | Wrap each line in `<span class="kirari-line" data-line="n">`. |
|
|
46
|
-
| `highlightLines` | Lines or `[first, last]` ranges get `kirari-line-highlighted`. Implies `lines`. |
|
|
47
|
-
| `lineClassName` | `(line) => class` for a line wrapper. `line` is `{ number, start, end }`. |
|
|
48
|
-
| `decorations` | `{ start, end, className }` source ranges, as in [decorations](https://github.com/vortexkit/kirari/blob/main/docs/decorations.md). |
|
|
49
|
-
| `classes` | Extra classes per token type, e.g. `{ keyword: 'kw' }`. |
|
|
50
|
-
| `tokenClassName` | `(token) => class` per token. |
|
|
51
|
-
| `tokens` | Pre-scanned tokens instead of `children`. |
|
|
52
|
-
|
|
53
|
-
Line numbers are CSS:
|
|
54
|
-
|
|
55
|
-
```css
|
|
56
|
-
.kirari-line::before { content: attr(data-line); display: inline-block; width: 3ch; margin-right: 2ch; opacity: 0.5; }
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
## Streaming
|
|
60
|
-
|
|
61
|
-
```tsx
|
|
62
|
-
'use client'
|
|
63
|
-
|
|
64
|
-
import { Code } from '@vortexlabs/kirari-react'
|
|
65
|
-
import { useTokenStream } from '@vortexlabs/kirari-react/stream'
|
|
66
|
-
import { javascript } from '@vortexlabs/kirari/languages/javascript'
|
|
67
|
-
import '@vortexlabs/kirari/theme.css'
|
|
68
|
-
|
|
69
|
-
export const StreamExample = () => {
|
|
70
|
-
const { tokens, write, reset } = useTokenStream(javascript)
|
|
71
|
-
|
|
72
|
-
return (
|
|
73
|
-
<>
|
|
74
|
-
<button type="button" onClick={() => write('const count = 42\n')}>
|
|
75
|
-
Append a line
|
|
76
|
-
</button>
|
|
77
|
-
<button type="button" onClick={reset}>Reset</button>
|
|
78
|
-
<Code tokens={tokens} />
|
|
79
|
-
</>
|
|
80
|
-
)
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
This example adds one chunk per click. In your app, call `write(chunk)` as text
|
|
85
|
-
arrives. The hook also returns `end()`, which closes the stream when it is complete.
|
|
86
|
-
|
|
87
|
-
- Built-in JavaScript, TypeScript, JSX, TSX, JSON, JSONC, and Python reuse completed scanning work. Other languages rescan the full source. Long unfinished tokens can still be expensive; see [token streaming](https://github.com/vortexkit/kirari/blob/main/docs/streaming.md).
|
|
88
|
-
- Changing `language` starts a new, empty stream.
|
|
89
|
-
- After `end()`, call `reset()` before writing again.
|
|
90
|
-
- `/stream` is a client module; `<Code>` is not.
|
|
91
|
-
- Managing tokens yourself? Use `createTokenStream` from `@vortexlabs/kirari/stream` with `applyPatch(tokens, patch)`.
|
|
92
|
-
|
|
93
|
-
## License
|
|
11
|
+
To switch an existing project, replace `@vortexlabs/kirari-react` with `kirari-react` in `package.json` and in import paths. Nothing else changes.
|
|
94
12
|
|
|
95
|
-
[
|
|
13
|
+
Documentation: [kirari.sh](https://kirari.sh)
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'kirari-react'
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'kirari-react'
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vortexlabs/kirari-react",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "Renamed to kirari-react. This package re-exports kirari-react so existing imports keep working.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
7
7
|
"react-component",
|
|
@@ -14,36 +14,40 @@
|
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "git+https://github.com/vortexkit/kirari.git",
|
|
17
|
-
"directory": "packages/react"
|
|
17
|
+
"directory": "packages/compat-react"
|
|
18
18
|
},
|
|
19
19
|
"type": "module",
|
|
20
20
|
"sideEffects": false,
|
|
21
21
|
"files": [
|
|
22
|
-
"
|
|
22
|
+
"index.js",
|
|
23
|
+
"index.d.ts",
|
|
24
|
+
"stream.js",
|
|
25
|
+
"stream.d.ts",
|
|
23
26
|
"README.md",
|
|
24
27
|
"LICENSE",
|
|
25
28
|
"CHANGELOG.md"
|
|
26
29
|
],
|
|
27
30
|
"exports": {
|
|
28
31
|
".": {
|
|
29
|
-
"types": "./
|
|
30
|
-
"import": "./
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"import": "./index.js"
|
|
31
34
|
},
|
|
32
35
|
"./stream": {
|
|
33
|
-
"types": "./
|
|
34
|
-
"import": "./
|
|
36
|
+
"types": "./stream.d.ts",
|
|
37
|
+
"import": "./stream.js"
|
|
35
38
|
}
|
|
36
39
|
},
|
|
37
40
|
"scripts": {
|
|
38
|
-
"build": "tsc -p tsconfig.build.json",
|
|
39
41
|
"test": "bun test ./test",
|
|
40
42
|
"typecheck": "tsc --noEmit",
|
|
41
|
-
"check": "bun run typecheck && bun
|
|
42
|
-
|
|
43
|
+
"check": "bun run typecheck && bun test ./test"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"kirari": "^1.2.0",
|
|
47
|
+
"kirari-react": "^1.2.0"
|
|
43
48
|
},
|
|
44
49
|
"peerDependencies": {
|
|
45
50
|
"@types/react": "^19",
|
|
46
|
-
"@vortexlabs/kirari": "^1.0.0",
|
|
47
51
|
"react": "^19"
|
|
48
52
|
},
|
|
49
53
|
"peerDependenciesMeta": {
|
|
@@ -52,12 +56,8 @@
|
|
|
52
56
|
}
|
|
53
57
|
},
|
|
54
58
|
"devDependencies": {
|
|
55
|
-
"@happy-dom/global-registrator": "^20.14.6",
|
|
56
59
|
"@types/react": "^19.3.0",
|
|
57
|
-
"
|
|
58
|
-
"@vortexlabs/kirari": "1.0.5",
|
|
59
|
-
"react": "^19.3.1",
|
|
60
|
-
"react-dom": "^19.3.1"
|
|
60
|
+
"react": "^19.3.1"
|
|
61
61
|
},
|
|
62
62
|
"publishConfig": {
|
|
63
63
|
"access": "public"
|
package/stream.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'kirari-react/stream'
|
package/stream.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'kirari-react/stream'
|
package/dist/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
|
2
|
-
import type { Language, Token } from '@vortexlabs/kirari/core';
|
|
3
|
-
import type { Decoration } from '@vortexlabs/kirari/decorations';
|
|
4
|
-
export type LineRange = number | readonly [first: number, last: number];
|
|
5
|
-
export interface Line {
|
|
6
|
-
readonly number: number;
|
|
7
|
-
readonly start: number;
|
|
8
|
-
readonly end: number;
|
|
9
|
-
}
|
|
10
|
-
/** Text children, joined like React text: `null`, `undefined`, and booleans render nothing. */
|
|
11
|
-
export type CodeText = string | number | bigint | boolean | null | undefined | readonly CodeText[];
|
|
12
|
-
export interface CodeProps extends Omit<ComponentPropsWithoutRef<'pre'>, 'children'> {
|
|
13
|
-
/** Source to highlight. Ignored when `tokens` is set. */
|
|
14
|
-
readonly children?: CodeText;
|
|
15
|
-
/** Pre-scanned tokens, such as the output of `useTokenStream`. */
|
|
16
|
-
readonly tokens?: readonly Token[];
|
|
17
|
-
readonly language?: Language;
|
|
18
|
-
readonly classes?: Readonly<Record<string, string>>;
|
|
19
|
-
readonly tokenClassName?: (token: Token) => string | undefined;
|
|
20
|
-
readonly decorations?: readonly Decoration[];
|
|
21
|
-
/** Wrap each line in `<span class="kirari-line" data-line="n">`. */
|
|
22
|
-
readonly lines?: boolean;
|
|
23
|
-
readonly lineClassName?: (line: Line) => string | undefined;
|
|
24
|
-
/** One-based lines or inclusive ranges that receive `kirari-line-highlighted`. Implies `lines`. */
|
|
25
|
-
readonly highlightLines?: readonly LineRange[];
|
|
26
|
-
}
|
|
27
|
-
/** Highlighted code as React elements. Uses no hooks, so it also renders as a Server Component. */
|
|
28
|
-
export declare const Code: ({ children, tokens, language, classes, tokenClassName, decorations, lines, lineClassName, highlightLines, className, ...props }: CodeProps) => ReactNode;
|
|
29
|
-
export type { Decoration } from '@vortexlabs/kirari/decorations';
|
|
30
|
-
export type { Language, Token } from '@vortexlabs/kirari/core';
|
package/dist/index.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { tokenize } from '@vortexlabs/kirari/core';
|
|
3
|
-
import { javascript } from '@vortexlabs/kirari/languages/javascript';
|
|
4
|
-
const join = (...names) => {
|
|
5
|
-
return names.filter(Boolean).join(' ');
|
|
6
|
-
};
|
|
7
|
-
const sourceText = (children) => {
|
|
8
|
-
if (children == null || typeof children === 'boolean')
|
|
9
|
-
return '';
|
|
10
|
-
if (typeof children === 'string' || typeof children === 'number' || typeof children === 'bigint')
|
|
11
|
-
return String(children);
|
|
12
|
-
if (Array.isArray(children))
|
|
13
|
-
return children.map(sourceText).join('');
|
|
14
|
-
throw new TypeError('Kirari: <Code> children must be text');
|
|
15
|
-
};
|
|
16
|
-
const splitsSurrogate = (source, offset) => {
|
|
17
|
-
const before = source.charCodeAt(offset - 1);
|
|
18
|
-
const after = source.charCodeAt(offset);
|
|
19
|
-
return before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff;
|
|
20
|
-
};
|
|
21
|
-
const highlighted = (ranges) => {
|
|
22
|
-
const pairs = ranges.map(range => typeof range === 'number' ? [range, range] : range);
|
|
23
|
-
return line => pairs.some(([first, last]) => first <= line && line <= last);
|
|
24
|
-
};
|
|
25
|
-
const assertContiguous = (tokens) => {
|
|
26
|
-
let offset = 0;
|
|
27
|
-
for (const token of tokens) {
|
|
28
|
-
if (token.start !== offset || token.end !== offset + token.value.length) {
|
|
29
|
-
throw new RangeError('Kirari: tokens must cover contiguous source offsets starting at zero');
|
|
30
|
-
}
|
|
31
|
-
offset = token.end;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
/** Validated enter/leave points for each decoration, in source order. */
|
|
35
|
-
const decorationEvents = (decorations, source) => {
|
|
36
|
-
const events = [];
|
|
37
|
-
for (const [index, decoration] of decorations.entries()) {
|
|
38
|
-
if (!Number.isInteger(decoration.start) || !Number.isInteger(decoration.end)
|
|
39
|
-
|| decoration.start < 0 || decoration.start >= decoration.end || decoration.end > source.length) {
|
|
40
|
-
throw new RangeError('Kirari: decoration offsets must be integers with 0 <= start < end <= source length');
|
|
41
|
-
}
|
|
42
|
-
if (splitsSurrogate(source, decoration.start) || splitsSurrogate(source, decoration.end)) {
|
|
43
|
-
throw new RangeError('Kirari: decoration offsets must not split a UTF-16 surrogate pair');
|
|
44
|
-
}
|
|
45
|
-
events.push({ offset: decoration.start, index, entering: true }, { offset: decoration.end, index, entering: false });
|
|
46
|
-
}
|
|
47
|
-
return events.sort((a, b) => a.offset - b.offset);
|
|
48
|
-
};
|
|
49
|
-
const codeNodes = (tokens, options = {}) => {
|
|
50
|
-
assertContiguous(tokens);
|
|
51
|
-
const source = tokens.map(token => token.value).join('');
|
|
52
|
-
const decorations = options.decorations ?? [];
|
|
53
|
-
const events = decorationEvents(decorations, source);
|
|
54
|
-
// Resolve callbacks once so tokens split by lines or decorations see them a single time.
|
|
55
|
-
const tokenClasses = tokens.map(token => join(options.classes && Object.hasOwn(options.classes, token.type) ? options.classes[token.type] : undefined, options.tokenClassName?.(token)));
|
|
56
|
-
const active = new Set();
|
|
57
|
-
let activeClasses = '';
|
|
58
|
-
let eventIndex = 0;
|
|
59
|
-
let tokenIndex = 0;
|
|
60
|
-
const rangeNodes = (start, end) => {
|
|
61
|
-
const nodes = [];
|
|
62
|
-
while (start < end) {
|
|
63
|
-
while (tokens[tokenIndex].end <= start)
|
|
64
|
-
tokenIndex++;
|
|
65
|
-
let changed = false;
|
|
66
|
-
while (eventIndex < events.length && events[eventIndex].offset <= start) {
|
|
67
|
-
const event = events[eventIndex++];
|
|
68
|
-
if (event.entering)
|
|
69
|
-
active.add(event.index);
|
|
70
|
-
else
|
|
71
|
-
active.delete(event.index);
|
|
72
|
-
changed = true;
|
|
73
|
-
}
|
|
74
|
-
if (changed)
|
|
75
|
-
activeClasses = [...active].sort((a, b) => a - b).map(index => decorations[index].className).join(' ');
|
|
76
|
-
const token = tokens[tokenIndex];
|
|
77
|
-
const stop = Math.min(end, token.end, events[eventIndex]?.offset ?? end);
|
|
78
|
-
const value = source.slice(start, stop);
|
|
79
|
-
const extra = join(tokenClasses[tokenIndex], activeClasses);
|
|
80
|
-
nodes.push(token.type === 'text' && !extra
|
|
81
|
-
? value
|
|
82
|
-
: _jsx("span", { className: join(`kirari-${token.type}`, extra), children: value }, start));
|
|
83
|
-
start = stop;
|
|
84
|
-
}
|
|
85
|
-
return nodes;
|
|
86
|
-
};
|
|
87
|
-
const ranges = options.highlightLines ?? [];
|
|
88
|
-
if (!options.lines && ranges.length === 0)
|
|
89
|
-
return rangeNodes(0, source.length);
|
|
90
|
-
const isHighlighted = highlighted(ranges);
|
|
91
|
-
const nodes = [];
|
|
92
|
-
const newline = /\r\n|\r|\n/g;
|
|
93
|
-
let start = 0;
|
|
94
|
-
let number = 1;
|
|
95
|
-
do {
|
|
96
|
-
const match = newline.exec(source);
|
|
97
|
-
const end = match?.index ?? source.length;
|
|
98
|
-
const className = join('kirari-line', options.lineClassName?.({ number, start, end }), isHighlighted(number) ? 'kirari-line-highlighted' : undefined);
|
|
99
|
-
nodes.push(_jsx("span", { className: className, "data-line": number, children: rangeNodes(start, end) }, `line-${number}`));
|
|
100
|
-
if (!match)
|
|
101
|
-
break;
|
|
102
|
-
nodes.push(match[0]);
|
|
103
|
-
start = end + match[0].length;
|
|
104
|
-
if (start === source.length)
|
|
105
|
-
break;
|
|
106
|
-
number++;
|
|
107
|
-
} while (true);
|
|
108
|
-
return nodes;
|
|
109
|
-
};
|
|
110
|
-
/** Highlighted code as React elements. Uses no hooks, so it also renders as a Server Component. */
|
|
111
|
-
export const Code = ({ children, tokens, language = javascript, classes, tokenClassName, decorations, lines, lineClassName, highlightLines, className, ...props }) => {
|
|
112
|
-
const nodes = codeNodes(tokens ?? tokenize(sourceText(children), language), {
|
|
113
|
-
...(classes && { classes }),
|
|
114
|
-
...(tokenClassName && { tokenClassName }),
|
|
115
|
-
...(decorations && { decorations }),
|
|
116
|
-
...(lines && { lines }),
|
|
117
|
-
...(lineClassName && { lineClassName }),
|
|
118
|
-
...(highlightLines && { highlightLines }),
|
|
119
|
-
});
|
|
120
|
-
return _jsx("pre", { ...props, className: join('kirari', className), children: _jsx("code", { children: nodes }) });
|
|
121
|
-
};
|
package/dist/stream.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Language, Token } from '@vortexlabs/kirari/core';
|
|
2
|
-
import type { TokenPatch } from '@vortexlabs/kirari/stream';
|
|
3
|
-
export interface StreamedTokens {
|
|
4
|
-
readonly tokens: readonly Token[];
|
|
5
|
-
write(chunk: string): void;
|
|
6
|
-
end(chunk?: string): void;
|
|
7
|
-
reset(): void;
|
|
8
|
-
}
|
|
9
|
-
export declare const applyPatch: (tokens: readonly Token[], patch: TokenPatch) => readonly Token[];
|
|
10
|
-
/** Tokens for source that arrives in chunks. Pass the result's `tokens` to `<Code tokens>`. */
|
|
11
|
-
export declare const useTokenStream: (language: Language) => StreamedTokens;
|
package/dist/stream.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
import { useCallback, useMemo, useState } from 'react';
|
|
3
|
-
import { createTokenStream } from '@vortexlabs/kirari/stream';
|
|
4
|
-
export const applyPatch = (tokens, patch) => {
|
|
5
|
-
return [...tokens.filter(token => token.end <= patch.from), ...patch.tokens];
|
|
6
|
-
};
|
|
7
|
-
/** Tokens for source that arrives in chunks. Pass the result's `tokens` to `<Code tokens>`. */
|
|
8
|
-
export const useTokenStream = (language) => {
|
|
9
|
-
const stream = useMemo(() => createTokenStream(language), [language]);
|
|
10
|
-
const [snapshot, setSnapshot] = useState({ stream, tokens: [] });
|
|
11
|
-
// A new language starts a new stream; tokens from the previous one no longer apply.
|
|
12
|
-
if (snapshot.stream !== stream)
|
|
13
|
-
setSnapshot({ stream, tokens: [] });
|
|
14
|
-
const apply = useCallback((patch) => {
|
|
15
|
-
setSnapshot(previous => previous.stream === stream ? { stream, tokens: applyPatch(previous.tokens, patch) } : previous);
|
|
16
|
-
}, [stream]);
|
|
17
|
-
const write = useCallback((chunk) => apply(stream.write(chunk)), [stream, apply]);
|
|
18
|
-
const end = useCallback((chunk) => apply(stream.end(chunk)), [stream, apply]);
|
|
19
|
-
const reset = useCallback(() => {
|
|
20
|
-
stream.reset();
|
|
21
|
-
setSnapshot({ stream, tokens: [] });
|
|
22
|
-
}, [stream]);
|
|
23
|
-
return { tokens: snapshot.stream === stream ? snapshot.tokens : [], write, end, reset };
|
|
24
|
-
};
|