npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-deca-memory-utils

v1.0.0

Published

React hooks for the deca-memory-utils library

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 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