1.0.0 • Published 7 months ago

react-deca-memory-utils v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

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 from deca-memory-utils, ensuring single ownership of resources.
  • useSharedPtr: React hook that wraps the SharedPtr class from deca-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 the UniquePtr instance, or null if the instance is empty.
  • reset: Updates the value held by the UniquePtr instance.
  • move: Transfers ownership of the UniquePtr 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 the SharedPtr instance, or null if the instance is invalid.
  • getRaw: Returns the raw pointer value held by the SharedPtr instance, or null if the instance is invalid.
  • isValid: Checks if the SharedPtr instance is valid.
  • useCount: Returns the current reference count of the SharedPtr instance.
  • reset: Resets the SharedPtr instance with a new value and optional destructor.
  • swap: Swaps the managed resource with another SharedPtr instance.
  • destroy: Destroys the SharedPtr instance.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Changelog

1.0.0 - 2024-11-06

Added

  • Initial release
  • useUniquePtr hook for managing UniquePtr instances
  • useSharedPtr hook for managing SharedPtr instances
  • Full TypeScript support

Versioning

This project follows Semantic Versioning. For the versions available, see the tags on this repository.

License

MIT