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

lumbridge-persistor

v0.1.3

Published

Improved logic management for React apps.

Downloads

7

Readme

lumbridge-persistor

🏰 React application management made simple.

npm GitHub react

Installation

Using npm:

npm i --save lumbridge-persistor

Using yarn:

yarn add lumbridge-persistor

Then import the helper classes where needed.

import { Persistor } from 'lumbridge-persistor';

const persistor = Persistor.create({
  // code...
});

API

Config

Each persistor is configured with a config object:

const config = {
  // options...
};

const persistor = Persistor.create(config);

This config object will contain all the information required by the persistor.

config.methods

  • Type: object
  • Required: true

A set of methods which provide an common interface for interacting with a data source.

const serverPersistor = Persistor.create({
  methods: {
    // code...
  },
});

Example:

const serverPersistor = Persistor.create({
  methods: {
    query: {
      payload: {
        query: string().required(),
        variables: object(),
      },
      handler: ({ query, variables }) => {
        return graphQLClient.query({ query, variables })
          .then(({ data }) => data);
      },
    },
    mutate: {
      payload: {
        mutate: string().required(),
        variables: object(),
      },
      handler: ({ mutate, variables }) => {
        return graphQLClient.mutate({ mutation: mutate, variables })
          .then(({ data }) => data);
      },
    },
  },
});

Properties:

  • methods[methodName].payload [object]: a set of validations used to check that the payload is the correct shape and type.
  • methods[methodName].handler [func]: a function which collects and returns data in a Promise.

Note: make sure your handler function returns a promise (e.g. new Promise((resolve, reject) => resolve(data))) or it will not work.

persistor.instance

  • Type: func
  • Returns: persistorInstance

Create a persistor method with more specific properties to the method being called.

const serverPersistor = Persistor.create(config);

const meQueryInstance = serverPersistor.instance({
  name: 'query',
  map: ({ ...args }) => ({
    ...args,
    query: `
      query($id: String) {
        me(id: $id) { name }
      }
    `,
  })
});

Properties:

  • name [string]: the key which corresponding to the persistor method.
  • map [func]: the callback provided is used to map the variables passed to the meQueryInstance.execute function to the handler payload.

persistorInstance.execute

  • Type: func
  • Returns: void

Execute the persistor method with parameters (which you specify).

meQueryInstance.execute({
  variables: { id },
});

Properties:

  • [payloadProperty] [any]: a set of payload properties which are validated and then provided to the corresponding persistor method.

Note: by seperating the functionality of executing an method and receiving that method's data, it enables you to more efficiently re-query the data without the code overhead. A good example would be when you wish to load a list of items that will change based upon a search filter. The list can be requeried easily while you only have to handle how that data is handled only once.

persistorInstance.watch

  • Type: func
  • Returns: unwatch

Listen to any updates in the persistor as the persistor instance executes.

const unwatchData = meQueryInstance.watch({
  done: data => setData(data),
  status: ({ loading }) => setLoading(loading),
});

const unwatchErrors = meQueryInstance.watch({
  catch: error => setError(error),
});

const componentWillUnmount = () => {
  unwatchData();
  unwatchErrors();
};

Properties:

  • done [func]: this function is called whenever the instance resolves with data.
  • catch [func]: this function is called whenever there is an error in the persistor.
  • status [func]: this function is called when there is an update in the state of the instance.

Note: when you start watching a persistor method, don't forget to call the unwatch function when the component unmounts and you stop listening for changes (see above code). If you don't unwatch, then you might cause a memory leak.

Scopes

It is often the case that a persistor will update application data in one location of the app, which may then lead to incorrect information shown in another location of the app.

For example, imagine being on a user settings page and changing a person's name. If the person's name is also shown in the top bar of the app, you will need to update this to the correct information after saving the person's name. Using a store can be a way of solving this problem.

However, sometimes knowing which data need to change is not clearly known. For example, imagine instead of showing a person's name in the top bar, it shows the number of videos a person has watched (on a video watching app). In this case, you can only "guess" the information if you use a store. A better way would be to requery the number of videos watched at the time that another query resolves and changes that.

For those use cases, we created the Scope data structure.

import { Scope } from 'lumbridge-persistor';

const scope = Scope.create({});

Scopes are used to add extra functionality to persistors so that they can listen and react to changes in each other.

scope.absorb

  • Type: func
  • Returns: void

This will connect a persistor method to the scope. Connecting a persistor method will enable the scope to listen to the changes in all the connected methods.

const persistor = Persistor.create({});
const persistorFirstInstance = persistor.instance({ name: 'exampleQuery' });
const persistorSecondInstance = persistor.instance({ name: 'otherQuery' });

const scope = Scope.create({});
scope.absorb(persistorFirstInstance);
scope.absorb(persistorSecondInstance, true);

Parameters:

  • arguments[0] [persistorInstance]: the persistor method to add to the scope.
  • arguments[1] [boolean]: if this is true then the method will re-run the last execution after any of the other connected methods run successfully.

scope.watch

  • Type: func
  • Returns: unwatch

This will combine and watch all of the persistor methods. This can be useful when you want to listen to any errors which occur in your methods and respond to them all in the same way (such as by creating a toast message).

const unwatch = scope.watch({
  catch: error => console.warn(error),
});

Properties:

  • done [func]: this function is called whenever the instance resolves with data.
  • catch [func]: this function is called whenever there is an error in the persistor.
  • status [func]: this function is called when there is an update in the state of the instance.

Note: when you start watching a scope, don't forget to call the unwatch function when you don't need it any more. If you don't unwatch, then you might cause a memory leak.

Packages