Skip to content

Commit 0616544

Browse files
committed
make useAsyncIterState's iterable's current value property read-only
1 parent 675331f commit 0616544

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

eslint.config.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default [
4343
['@typescript-eslint/no-explicit-any']: 'off',
4444
['@typescript-eslint/no-non-null-assertion']: 'off',
4545
['@typescript-eslint/no-empty-function']: 'off',
46+
['@typescript-eslint/no-this-alias']: 'off',
4647
['@typescript-eslint/no-unused-expressions']: 'warn',
4748
['@typescript-eslint/no-unused-vars']: [
4849
'warn',

spec/tests/useAsyncIterState.spec.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,12 @@ describe('`useAsyncIterState` hook', () => {
153153
expect(currentValues).toStrictEqual([undefined, 'a', 'b', 'c']);
154154
}
155155
);
156+
157+
it(gray("The state iterable's `.current.value` property is read-only"), async () => {
158+
const [values] = renderHook(() => useAsyncIterState<number>()).result.current;
159+
160+
expect(() => {
161+
(values.value as any).current = "can't do this...";
162+
}).toThrow(TypeError);
163+
});
156164
});

src/useAsyncIterState/IterableChannel.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ class IterableChannel<T> {
2121
}
2222

2323
values: AsyncIterableSubject<T> = {
24-
value: {
25-
current: undefined,
26-
},
24+
value: (() => {
25+
const self = this;
26+
return {
27+
get current() {
28+
return self.#currentValue;
29+
},
30+
};
31+
})(),
2732

2833
[Symbol.asyncIterator]: () => {
2934
const whenIteratorClosed = promiseWithResolvers<IteratorReturnResult<undefined>>();
@@ -52,7 +57,7 @@ type AsyncIterableSubject<T> = {
5257
/**
5358
* A React Ref-like object whose inner `current` property shows the most up to date state value.
5459
*/
55-
value: MutableRefObject<T | undefined>;
60+
value: Readonly<MutableRefObject<T | undefined>>;
5661

5762
/**
5863
* Returns an async iterator to iterate over. All iterators returned by this share the same source

0 commit comments

Comments
 (0)