Skip to content

Commit

Permalink
make useAsyncIterState's iterable's current value property read-only
Browse files Browse the repository at this point in the history
  • Loading branch information
shtaif committed Jan 8, 2025
1 parent 675331f commit 0616544
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default [
['@typescript-eslint/no-explicit-any']: 'off',
['@typescript-eslint/no-non-null-assertion']: 'off',
['@typescript-eslint/no-empty-function']: 'off',
['@typescript-eslint/no-this-alias']: 'off',
['@typescript-eslint/no-unused-expressions']: 'warn',
['@typescript-eslint/no-unused-vars']: [
'warn',
Expand Down
8 changes: 8 additions & 0 deletions spec/tests/useAsyncIterState.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,12 @@ describe('`useAsyncIterState` hook', () => {
expect(currentValues).toStrictEqual([undefined, 'a', 'b', 'c']);
}
);

it(gray("The state iterable's `.current.value` property is read-only"), async () => {
const [values] = renderHook(() => useAsyncIterState<number>()).result.current;

expect(() => {
(values.value as any).current = "can't do this...";
}).toThrow(TypeError);
});
});
13 changes: 9 additions & 4 deletions src/useAsyncIterState/IterableChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ class IterableChannel<T> {
}

values: AsyncIterableSubject<T> = {
value: {
current: undefined,
},
value: (() => {
const self = this;
return {
get current() {
return self.#currentValue;

Check failure on line 28 in src/useAsyncIterState/IterableChannel.ts

View workflow job for this annotation

GitHub Actions / ts_build_test

Property '#currentValue' does not exist on type 'IterableChannel<T>'.
},
};
})(),

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

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

0 comments on commit 0616544

Please sign in to comment.