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

svelte-contextify

v0.1.3

Published

A tiny library for vastly improving context managament in Svelte apps by encapsulating the Context API.

Downloads

48

Readme

svelte-contextify

A tiny library for vastly improving context managament in Svelte apps by encapsulating the Context API.

Features

  1. Removes the need for keys
  2. Utilizes symbols to create unique keys
  3. Vastly improves type inference

Install

npm i svelte-contextify
pnpm add svelte-contextify
yarn add svelte-contextify
bun add svelte-contextify

The problem

// session.ts
export type Session = {
	user: string;
};
// session.ts
export type Session = {
	user: string;
};
<!-- Parent.svelte -->
<script lang="ts">
	import { setContext } from 'svelte';
	import type { Session } from '$lib/session.ts';

	setContext<Session>('session', { user: 'Hugos68' });
</script>

<!-- Child.svelte -->
<script lang="ts">
	import { getContext } from 'svelte';
	import type { Session } from '$lib/session.ts';

	const session = getContext<Session>('session');
</script>

The snippet above show a fairly common use case, we want to store session in context (so it is SSR safe) and pass it down to a child so it can access it safely. This has 2 problems/annoyances:

  1. We need to keep track of the context key (session) in 2 different places.
  2. We need to keep track of the type in 2 different places.

How svelte-contextify fixes the problem

import { getContext, setContext } from 'svelte';

export function createContext<T>(): [() => T | undefined, (value: T) => T, symbol];

export function createContext<T>(fallback: T): [() => T, (value: T) => T, symbol];

export function createContext<T>(fallback?: T) {
	const key = Symbol();
	return [() => getContext<T>(key) ?? fallback, (value: T) => setContext<T>(key, value), key];
}

Pretty simple right?

We return 3 things here:

  1. The get function: This is responsible for retrieving the value out of context
  2. The set function: This is responsible for setting the value in context
  3. The generated key: This is a unique symbol that is returned in case you need to use it outside of the supplied get or set functions

This allows you to turn the code snippet above into:

// session.ts
type Session = {
	user: string;
};

export const [getSession, setSession, key] = createContext<Session>();
<!-- Parent.svelte -->
<script lang="ts">
	import { setSession } from './session.ts';

	setSession({ user: 'Hugos68' }); // Full type safety when setting the session
</script>

<!-- Child.svelte -->
<script lang="ts">
	import { getSession } from './session.ts';

	const session = getSession(); // type Session is inferred
</script>

This improves the experience in 2 main ways:

  1. No need to specify a key anymore (or keep track of)
  2. The type only needs to be specified once and is applied to both the getter and setter implicitly