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

v0.1.2

Published

Sensors for Svelte to help deliver adaptive behaviour based on a user's device and network.

Downloads

30

Readme

svelte-adaptive-sensors

Sensors to help you deliver adaptive sensors to users depending on their network-type, memory, cpu, and saveData settings. A svelte version of react-adaptive-hooks although there are very few differences between the two libraries currently.

This library makes it easier to get information about a user's device, settings and network and alter your app's behaviour using these metrics.

Currently 4 APIs are supported:

Install

With npm:

npm install --save-dev svelte-adaptive-sensors

Or yarn:

yarn add --dev svelte-adaptive-sensors

Use

Import them:

import {
	getCpuInfo,
	getMemoryInfo,
	getNetworkInfo,
	getSaveDataInfo,
} from 'svelte-adaptive-sensors';

And then use them.

API

All functions (or stores, in the case of getNetworkInfo) return an object with a supported property. This value is false if the API is not supported and true if it is.

getCpuInfo

A simple function that returns information about a user's logical processor cores using the navigator.hardwareConcurrency API.

This value is static and will never change. User don't routinely swap out their CPU when using an app and if they do then I wnat to hear about it.

getCpuInfo() = {
  supported: Boolean,
  processors:? Number
};

If supported is false then the processors property will not be present.

<script>
  import { getCpuInfo } from 'svelte-adaptive-sensors';

  const { processors, supported } = getCpuInfo();
</script>

{#if supported && processors > 4}
  <Video src={video_src} />
{:else}
  <Image src={image_src}>
{/if}

getNetworkInfo

A function that returns a store containing information about a user's effect network speed using the navigator.connection.effectiveType API.

This is the only value that can update and as such it returns a readable store instead of a static value. The store has the following contents:

getNetworkInfo() = readable<{
  supported: Boolean,
  effectiveType:? Number
}>;

If supported is false then the effectiveType property will not be present.

<script>
  import { getNetworkInfo } from 'svelte-adaptive-sensors';

  const network = getNetworkInfo();

  function getProps(network_type) {
    let props;
    switch(network_type) {
      case 'slow-2g':
        media = { src: low_res, alt: 'low resolution' };
        break;
      case '2g':
        media = { src: med_res, alt: 'medium resolution' };
        break;
      case '3g':
        media = { src: hi_res, alt: 'high resolution' };
        break;
      case '4g':
        media = { src: very_hi_res, alt: 'very high resolution' };
        break;
      default:
        media = { src: med_res, alt: 'medium resolution' };
        break;
    }

    return props;
  }

  $: media_props = getProps($network.effectiveType);
</script>

<img {...media_props} />

getMemoryInfo

A simple function that returns information about a user's deviceMemory using the navigator.deviceMemory and performance.memory APIs.

This value is static and will never change.

getMemoryInfo() = {
  supported: Boolean,
  deviceMemory:? Number,
  totalJSHeapSize:? Number,
  usedJSHeapSize:? Number,
  jsHeapSizeLimit:? Number,
}

If supported is false then the deviceMemory, totalJSHeapSize, usedJSHeapSize, jsHeapSizeLimit properties will not be present.

<script>
  import { getMemoryInfo } from 'svelte-adaptive-sensors';

  const { deviceMemory, supported } = getMemoryInfo();
</script>

{#if supported && deviceMemory > 4}
  <Video src={video_src} />
{:else}
  <Image src={image_src}>
{/if}

getSaveDataInfo

A simple function that returns a user's current Save Data status

getSaveDataInfo() = {
  supported: Boolean,
  saveData:? Boolean,
};

If supported is false then the saveDataproperty will not be present.

<script>
  import { getMemoryInfo } from 'svelte-adaptive-sensors';

  const { saveData, supported } = getSaveDataInfo();
</script>

{#if supported && !saveData}
  <Video src={video_src} />
{:else}
  <Image src={image_src}>
{/if}