react-deca-memory-utils
v1.0.0
Published
React hooks for the deca-memory-utils library
Downloads
7
Maintainers
Readme
react-deca-memory-utils
React hooks for the deca-memory-utils
library, providing a seamless integration of smart pointer functionality into your React applications.
Installation
npm install react-deca-memory-utils
Features
- useUniquePtr: React hook that wraps the
UniquePtr
class fromdeca-memory-utils
, ensuring single ownership of resources. - useSharedPtr: React hook that wraps the
SharedPtr
class fromdeca-memory-utils
, allowing for shared resource management with reference counting. - Fully typed with TypeScript support.
- Zero additional dependencies beyond
deca-memory-utils
. - Comprehensive test coverage.
- Follows React hooks best practices for lifecycle management.
Usage
useUniquePtr
import { useUniquePtr } from 'react-deca-memory-utils';
const MyComponent = () => {
const [get, reset, move] = useUniquePtr<{ name: string; age: number }>(
{ name: 'John', age: 30 }
);
const handleTransfer = () => {
const movedPtr = move();
if (movedPtr) {
console.log('Transferred value:', movedPtr.get());
}
};
return (
<div>
<h1>Current value: {get()?.name} ({get()?.age})</h1>
<button onClick={() => reset({ name: 'Jane', age: 25 })}>
Reset value
</button>
<button onClick={handleTransfer}>Transfer ownership</button>
</div>
);
};
useSharedPtr
import { useSharedPtr } from 'react-deca-memory-utils';
const MyComponent = () => {
const [get, getRaw, isValid, useCount, reset, swap, destroy] = useSharedPtr<{ name: string; age: number }>(
{ name: 'John', age: 30 },
(value) => console.log('Cleaning up:', value)
);
const handleSharing = () => {
const otherSharedPtr = new SharedPtr({ name: 'Jane', age: 25 }, (value) => console.log('Cleaning up other:', value));
swap(otherSharedPtr);
};
return (
<div>
<h1>Shared value: {get()?.name} ({get()?.age})</h1>
<p>Reference count: {useCount()}</p>
<button onClick={() => reset({ name: 'Alice', age: 40 })}>
Reset value
</button>
<button onClick={handleSharing}>
Share with another pointer
</button>
<button onClick={destroy}>
Destroy shared pointer
</button>
</div>
);
};
API Reference
useUniquePtr
const useUniquePtr = <T>(initialValue: T | null = null): [
get: () => T | null,
reset: (newValue: T | null) => void,
move: () => UniquePtr<T> | null
];
get
: Returns the value held by theUniquePtr
instance, ornull
if the instance is empty.reset
: Updates the value held by theUniquePtr
instance.move
: Transfers ownership of theUniquePtr
instance to a new instance, returning the moved instance.
useSharedPtr
const useSharedPtr = <T>(
initialValue: NonNullable<T> | null = null,
destructor?: Destructor<T>
): [
get: () => NonNullable<T> | null,
getRaw: () => NonNullable<T> | null,
isValid: () => boolean,
useCount: () => number,
reset: (newValue: NonNullable<T> | null, newDestructor?: Destructor<T>) => void,
swap: (other: SharedPtr<T>) => void,
destroy: () => void
];
get
: Returns the value held by theSharedPtr
instance, ornull
if the instance is invalid.getRaw
: Returns the raw pointer value held by theSharedPtr
instance, ornull
if the instance is invalid.isValid
: Checks if theSharedPtr
instance is valid.useCount
: Returns the current reference count of theSharedPtr
instance.reset
: Resets theSharedPtr
instance with a new value and optional destructor.swap
: Swaps the managed resource with anotherSharedPtr
instance.destroy
: Destroys theSharedPtr
instance.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Changelog
[1.0.0] - 2024-11-06
Added
- Initial release
useUniquePtr
hook for managingUniquePtr
instancesuseSharedPtr
hook for managingSharedPtr
instances- Full TypeScript support
Versioning
This project follows Semantic Versioning. For the versions available, see the tags on this repository.
License
MIT