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

solid-services

v4.3.2

Published

Solid.js library adding a services layer for global shared state.

Downloads

239

Readme

Solid Services

Solid Services is a package that provides a way to manage shared state and persistent connections in Solid.js applications. It consists of a ServiceRegistry component, which creates a context around your components and allows you to scope the services to specific parts of the application, and the useService hook, which registers a service or returns an existing object if it has already been used in the past.

Installation

npm install solid-services

Compatibility

  • Solid.js ^1.0

Demo

Open on CodeSandbox

Services

Service is an object that provides a particular function or set of functions. Services are designed to be "global" objects that can be accessed and used throughout an application, and are often used for features that require shared state or persistent connections.

Some common examples of services include user authentication, geolocation, WebSockets, server-sent events or notifications, server-backed API call libraries, third-party APIs, and logging. Services can be implemented as either a class or a plain object (POJO) and are usually defined as a function that returns an instance of the class or object. For example:

import { createSignal } from 'solid-js';

export function AuthService() {
  const [getUser, setUser] = createSignal();

  return {
    get user() {
      return getUser();
    },

    login(user) {
      setUser(user);
    },

    logout() {
      setUser(undefined);
    },
  };
}

You can also define a service using a class:

import { Service } from 'solid-services';

export class AuthService extends Service {
  @signal #user; // Decorators not included in solid-services

  get user() {
    return this.#user;
  }

  @action
  login(user) {
    this.#user = user;
  }

  @action
  logout() {
    this.#user = undefined;
  }
}

To access a service in your components or other services, you can use the useService hook. This hook registers the service or returns an existing object if it has already been used in the past. For example:

import { useService } from "solid-services";
import AuthService from "./services/auth";

export default function LogoutComponent() {
  const getAuthService = useService(AuthService);

  function logout() {
    getAuthService().logout();
  }

  return <button onClick={logout}>Logout</button>;
}

Service Registry

The ServiceRegistry is a component that creates a context around the components within an application, allowing developers to scope the services to specific parts of the application.

To use the ServiceRegistry, you can simply wrap the components that you want to be within the context of the registry in a element. For example:

import { ServiceRegistry } from 'solid-services';

export default function App() {
  return (
    <ServiceRegistry>
      {/* All components within this element will have access to the services defined in this registry */}
      <SomeComponent />
      <AnotherComponent />
    </ServiceRegistry>
  );
}

Remember!

It is important to wrap your application with a top-level <ServiceRegistry> before using services in components. Otherwise, services won't be able to register and their usage will throw an error.

By default, the ServiceRegistry does not expose any services to sub-registries. This means that the components within a sub-registry will not have access to the services defined in the parent registry. However, you can configure this behavior using the expose property of the ServiceRegistry.

For example, to expose a specific service to a sub-registry, you can set the expose property to an array containing the service(s) that you want to expose:

import { ServiceRegistry } from 'solid-services';
import AuthService from './services/auth';

export default function App() {
  return (
    <ServiceRegistry expose={[AuthService]}>
      {/* All components within this element will have access
          to all the services defined in this registry */}
      
      <ServiceRegistry>
        {/* All components within this element will have access
            to the AuthService from the parent registry,
            as well as any services defined in this registry */}
      </ServiceRegistry>
    </ServiceRegistry>
  );
}

You can also set the expose property to true to expose all services to sub-registries. This can be useful if you want to granularly control the availability of services in different parts of your application.

import { ServiceRegistry } from 'solid-services';

export default function App() {
  return (
    <ServiceRegistry expose={true}>
      {/* All components within this element will have access to the services defined in this registry */}
      
      <ServiceRegistry>
        {/* All components within this element will have access to all services from the parent registry,
            as well as any services defined in this registry */}
      </ServiceRegistry>
    </ServiceRegistry>
  );
}

By using the ServiceRegistry and the expose property, you can control which services are available to different parts of your application, and manage the shared state and persistent connections within your Solid.js application.