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

@interstice/capsular

v0.0.1

Published

An offline first approach to remote data

Downloads

1

Readme

capsular

Offline first remote data, using a standard fetch api backed by web workers and indexeddb.

Getting started

Install

npm i @interstice/capsular

Initialize store migrations

init command takes optional args: init [dir] [language]

npx @interstice/capsular init src/migrations ts

Create a migration

Migrations can be optionally named, with a target db name being required. If no migration name is provided it will consist solely of the timestamp of generation. migration generate <db> [name]

npx @interstice/capsular migration g offline init

You will end up with a new migration file similar to the following, with timestamps corresponding to the time in which the command was run.

// migrations/offline/1640716285009_init.ts
import { Migration, DBSchema } from "capsular";

export const version = "1640716285009";

export interface Post {
  title: string;
  createdAt: number;
  updatedAt: number;
}

export interface Schema extends DBSchema {
  posts: {
   key: string;
   value: Post[];
  }
}

export const migration: Migration<Schema> = (db, oldVersion) => {
  db.createObjectStore('offline')
};

Fill out the migration accordingly, in a simplified case this might just mean uncommenting the migration sample command and replacing the collection name with your db name.

Usage

Use the provided worker, this worker file should be built out to a location it will be publicly served. ex. dist/worker.js

// worker.ts
import '@interstice/capsular/worker';

To use a store you first create an instance, providing store connection details.

// store.ts
import { Store, StoreConnection } from '@interstice/capsular';
import { version } from './migrations' // this will be the location of where migrations are kept

export const offline = new Store({
    db: 'offline', // target db name
    version // latest version of the migrations
  },
  '/worker.js' // path to public worker file once built relative to the root
) 

Fetch

Fetching requires creation of stable key id references referred to as a ConnectedStoreRecord.

// api.ts
import { KeyID, ConnectedStoreRecord, StoreData } from '@interstice/capsular';
import { offline } from './store';

export const allPosts: ConnectedStoreRecord = {
  key: new KeyID(['all', 'posts']),
  collection: 'offline',
};

const queryAllPosts = async (): Promise<StoreData|undefined> => {
  const query = await offline.createFetch(allPosts);
  return await query();
}

Background Fetch

A Fetch can be refetched in the background of the worker off the main thread.

// api.ts
import { KeyID, ConnectedStoreRecord, StoreData } from '@interstice/capsular';
import { offline } from './store';

export const allPosts: ConnectedStoreRecord = {
  key: new KeyID(['all', 'posts']),
  collection: 'offline',
};

const backgroundQueryAllPosts = (): void => {
  offline.backgroundFetch(allPosts, undefined, {
    cacheFor: 600,  // in seconds
    refetchInterval: 60 // in seconds
  });
}

backgroundQueryAllPosts()

Subscribe Fetch

A Fetch can be subscribed to whenever it is refetched in the worker, so that it can be refetched on the main thread. You can always add more logic to the subscription as to what is fetched from the store, and is not limited to just fetching from the store.

// subscription.ts
import { KeyID, ConnectedStoreRecord, StoreData } from '@interstice/capsular';
import { offline } from './store';
import { allPosts, queryAllPosts } from './api';

let unsubscribeAllPosts = null
const subscribeAllPosts = (): void => {
  if (unsubscribeAllPosts) unsubscribeAllPosts();
  
  // returns the unsubscribe method
  unsubscribeAllPosts = offline.subscribe(allPosts.id, async (info: { updated: boolean } ) => {
    // contains the metadata of the subscription, ie: did this update?
    if (info.updated) {
      // requery the data from the store if it has signalled as having been updated
      const posts = await queryAllPosts()
    }
  }, (error: Error) => {
      // errors that may have occurred
  });
}

subscribeAllPosts()