diff --git a/eslint.config.mjs b/eslint.config.mjs index 4ec4080..197ae9d 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -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', diff --git a/spec/tests/useAsyncIterState.spec.tsx b/spec/tests/useAsyncIterState.spec.tsx index 62c564c..cbdcf42 100644 --- a/spec/tests/useAsyncIterState.spec.tsx +++ b/spec/tests/useAsyncIterState.spec.tsx @@ -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()).result.current; + + expect(() => { + (values.value as any).current = "can't do this..."; + }).toThrow(TypeError); + }); }); diff --git a/src/useAsyncIterState/IterableChannel.ts b/src/useAsyncIterState/IterableChannel.ts index 359b640..84c4b3f 100644 --- a/src/useAsyncIterState/IterableChannel.ts +++ b/src/useAsyncIterState/IterableChannel.ts @@ -21,9 +21,14 @@ class IterableChannel { } values: AsyncIterableSubject = { - value: { - current: undefined, - }, + value: (() => { + const self = this; + return { + get current() { + return self.#currentValue; + }, + }; + })(), [Symbol.asyncIterator]: () => { const whenIteratorClosed = promiseWithResolvers>(); @@ -52,7 +57,7 @@ type AsyncIterableSubject = { /** * A React Ref-like object whose inner `current` property shows the most up to date state value. */ - value: MutableRefObject; + value: Readonly>; /** * Returns an async iterator to iterate over. All iterators returned by this share the same source