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

ella-sparse-array

v0.0.7

Published

Sparse array structure for fetching large collections of records a few at a time.

Downloads

13

Readme

Warning: This is a work in progress (WIP)

I accept friendly pull requests and feedback.

Also, use this addon at your own risk. It could be capable of anything, including scaring your cat (even if you don't own a cat) or burning your toast (nobody really likes burnt toast). Please use caution when using addons that are not yet complete.

Emberella Sparse Array

Aloha!

Emberella Sparse Array is an Ember CLI addon that provides a sparse array data structure. Its aim is to provide a means for populating an array of data into the client app in "pages" or "chunks" rather than all at once.

For example, imagine you want to show a list with 246,982 records. Well, you could setup routes to paginate the data, showing a few records to the user at a time. Or you could press your luck and try to grab all 246,982 records from your persistence layer at once. Or you could use a sparse array like this one to gradually load only as much data as you need to make the user happy.

Installation

Ember and Ember CLI are always evolving, so you may wish to verify these instructions in the Ember CLI documentation. As of this writing, you can install this addon by navigating to your project directory in the command line and entering the following:

ember install ella-sparse-array

This will add the Emberella Sparse Array library as a dependency to your app.

Usage

Anywhere you wish to use the Emberella Sparse Array library, add the following to the top of your Javascript file:

import EllaSparseArray from 'ella-sparse-array/lib/sparse-array';

Once you have imported a reference to the library, you can instantiate a new sparse array with:

var arr = EllaSparseArray.create();

Of course, if you do that, you'll get an error when trying to get any data. Sparse Arrays begin life empty and pretty entirely unsure of what type of data they are meant to contain. You will have to "teach" your sparse arrays how to get data by providing two data fetching methods: didRequestLength and didRequestRange.

For example, if you were using Ember Data, you might request length and range like this:

var sparseArray = EllaSparseArray.create({
  didRequestLength: function() {
    var _this = this;
    store.query('cat', {page: {offset: 0, limit: 1}}).then(function(response) {
      _this.provideLength(Ember.get(response, 'meta.page.total'));
    });
  },
  didRequestRange: function(range) {
    var _this = this;
    store.query('cat', {page: {offset: range.start, limit: range.length}}).then(function(response) {
      _this.provideLength(Ember.get(response, 'meta.page.total'));
      _this.provideObjectsInRange(range, response.get('content').mapBy('record'));
    });
  }
});

Note: the names of some models have been changed to protect the innocent. Also, this example has not been verified to work as written. See tests for more examples.

didRequestLength

Your custom length fetching method should fetch the total number of available objects from the persistence layer. Once you successfully fetch the record count, provide it to the sparse array using the .provideLength method.

didRequestRange

Your custom record fetching method should fetch a "page" of items from the persistence layer. Your custom method will be called with a range object containing a start property, the starting index or "offset" of the records to be fetched, and a length, the number of items to retrieve.

Once you successfully fetch this batch of records, inject them into the sparse array by calling the .provideObjectsInRange method. For example: sparseArray.provideObjectsInRange(range, fetchedObjectsArray).