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

rxjs-jquery

v0.0.3

Published

Library for composing asynchronous and event-based operations in JavaScript extending the jQuery library

Downloads

15

Readme

jQuery Bindings for the Reactive Extensions for JavaScript

OVERVIEW

This project provides Reactive Extensions for JavaScript (RxJS) bindings for jQuery to abstract over the event binding, Ajax and Deferreds. The RxJS libraries are not included with this release and must be installed separately.

GETTING STARTED

There are a number of ways to get started with the jQuery Bindings for RxJS.

Download the Source

To download the source of the jQuery Bindings for the Reactive Extensions for JavaScript, type in the following:

git clone https://github.com/Reactive-Extensions/rxjs-jquery.git
cd ./rxjs-jquery

Installing with NuGet

PM> Install-Package RxJS-Bridges-jQuery

Getting Started with the jQuery Bindings

Let's walk through a simple yet powerful example of the Reactive Extensions for JavaScript Bindings for jQuery, autocomplete. In this example, we will take user input from a textbox and trim and throttle the input so that we're not overloading the server with requests for suggestions.

We'll start out with a basic skeleton for our application with script references to jQuery, RxJS, RxJS Time-based methods, and the RxJS Bindings for jQuery, along with a textbox for input and a list for our results.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="rx.min.js"></script>
<script type="text/javascript" src="rx.time.min.js"></script>
<script type="text/javascript" src="rx-jquery.js"><script>
<script type="text/javascript">
	$(function () {
		...
	})();
</script>
...
<input id="textInput" type="text"></input>
<ul id="results"></ul>
...

The goal here is to take the input from our textbox and throttle it in a way that it doesn't overload the service with requests. To do that, we'll get the reference to the textInput using jQuery, then bind to the 'keyup' event using the keyupAsObservable method which then takes the jQuery object and transforms it into an RxJS Observable.

var throttledInput = $('#textInput')
	.keyupAsObservable()

Since we're only interested in the text, we'll use the select method to take the event object and return the target's value.

	.select( function (ev) {
		return $(ev.target).val();
	})

We're also not interested in query terms less than two letters, so we'll trim that user input by using the where method returning whether the string length is appropriate.

	.where( function (text) {
		return text.length > 2;
	})

We also want to slow down the user input a little bit so that the external service won't be flooded with requests. To do that, we'll use the throttle method with a timeout of 500 milliseconds, which will ignore your fast typing and only return a value after you have paused for that time span.

	.throttle(500)

Lastly, we only want distinct values in our input stream, so we can ignore requests that are not unique, for example if I copy and paste the same value twice, the request will be ignored.

	.distinctUntilChanged();

Putting it all together, our throttledInput looks like the following:

var throttledInput = $('#textInput')
	.keyupAsObservable()
	.select( function (ev) {
		return $(ev.target).val();
	})
	.where( function (text) {
		return text.length > 2;
	})
	.throttle(500)
	.distinctUntilChanged();

Now that we have the throttled input from the textbox, we need to query our service, in this case, the Wikipedia API, for suggestions based upon our input. To do this, we'll create a function called searchWikipedia which calls the jQuery.ajaxAsObservable method which wraps the existing jQuery Ajax request in an RxJS AsyncSubject.

function searchWikipedia(term) {
	return $.ajaxAsObservable({
		url: 'http://en.wikipedia.org/w/api.php',
		data: { action: 'opensearch',
				search: term,
				format: 'json' }
		dataType: 'jsonp'
	});
}

Now that the Wikipedia Search has been wrapped, we can tie together throttled input and our service call. In this case, we will call select on the throttledInput to then take the text from our textInput and then use it to query Wikipedia, filtering out empty records. Finally, to deal with concurrency issues, we'll need to ensure we're getting only the latest value. Issues can arise with asynchronous programming where an earlier value, if not cancelled properly, can be returned before the latest value is returned, thus causing bugs. To ensure that this doesn't happen, we have the switchLatest method which returns only the latest value.

var suggestions = throttledInput.select( function (text) {
	return searchWikipedia(text);
})
.where( function (data) {
	return data.length == 2 && data[1].length > 0;
})
.switchLatest();

Finally, we'll subscribe to our observable by calling subscribe which will receive the results and put them into an unordered list. We'll also handle errors, for example if the server is unavailable by passing in a second function which handles the errors.

suggestions.subscribe( function (data) {
	var selector = $('#results');
	selector.clear();
	$.each(data[1], function (_, text) {
		$('<li>' + text + '</li>').appendTo(selector);
	});
}, function (e) {
	selector.clear();
	$('<li>Error: ' + e + '</li>').appendTo('#results');
});

We've only scratched the surface of this library in this simple example.

Implemented Bindings

LICENSE

Copyright 2011 Microsoft Corporation

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.