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

ember-xhr

v0.0.3

Published

Easily achieve beautiful, transparent uploads and downloads with a reactive XmlHttpRequest

Downloads

5

Readme

ember-xhr

Easily achieve beautiful, transparent uploads and downloads with a reactive XmlHttpRequest

Why?

The standard XmlHttpReuest object that is part of the standard browser toolkit provides an imperative / callback based API, and so reacting to changes can be a frustrating exercise in "what callbacks do I register? onloadend? onload?", "In what order are they fired?", "what are the properties of the event", etc...

Ember.XHR is a drop-in replacement for the native browser XMLHttpRequest whose properties are fully reactive, and so integrating it into your UI is a breeze. There are no need to register any callbacks since all of its properties can be observed, bound to, or computed from.

All of the properties of the the XHR, including readyState, status, response, responseText, progress, etc... are reactive and can be bound to in templates, and can also be used as the input for other computed properties.

This is particularly useful in tracking the progress of an upload or a download.

What is Progress?

There are progress properties that correspond to the state of the upload and the download, and that react as that state changes. The download progress properties are the progress property. E.g. progress.total, while the upload progress is on the upload.progress property. E.g. upload.progress.total

These reactive properties are:

  • total: the total number of bytes that will be sent in this transfer.
  • loaded: how many bytes of the total have been transferred thus far.
  • percentage: what percentage of the total bytes have been transferred thus far.
  • totalTime: time in milliseconds that this transfer has been in progress.
  • averageSpeed: Over the coures of the transfer, the average speed in bytes per second.

Because Ember.XHR radiates all information about its progress, building UIs to track it are a snap.

Installation

Ember XHR can be used via ember-cli, or from a global build. The choice is yours, but to use the global build, you'll have to jump through a few more hoops.

Ember CLI

Using it with ember-cli is a snap:

npm install --save ember-xhr

Then, anywhere in your app:

import XHR from 'ember-xhr';

Global Build

git clone git://github.com/cowboyd/ember-xhr.git
cd ember-xhr
npm install
ember build

This will build dist/ember-xhr.js and dist/ember-xhr.js.map which you can place into your vendor/ directory or wherever it is you keep your javascript files. This will define Ember.XHR, so anywhere in your app, you can say:

var XHR = Ember.XHR;

Example

var file = getDraggedFile();
var xhr = XHR.create();
xhr.open('PUT', '/path/to/some/blob', true);
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.send(file);
this.set('xhr', xhr);

The xhr can just be dropped into a template.

{{xhr.readyState}}
Bytes Uploaded: {{xhr.upload.progress.loaded}}
Bytes Downloaded: {{xhr.progress.loaded}}
Avg. Upload Speed (bytes/second): {{xhr.upload.progress.averageSpeed}}
Avg. Download Speed (bytes/second): {{xhr.progress.averageSpeed}}
Percentage Uploaded: {{xhr.upload.progress.percentage}} {{!=> .45}}
Percentage Downloaded: {{xhr.progress.percentage}}

It's not just for templates though... binding the upload percentage to the width of a progress bar is another simple application.

And that's a promise!

As a kicker, every instance of Ember.XHR implements the promise interface.

xhr.then(function(response) {
  console.log('response', response);
})