Skip to content

Instantly share code, notes, and snippets.

View mattpodwysocki's full-sized avatar

Matthew Podwysocki mattpodwysocki

View GitHub Profile
/**
* Pauses the underlying observable sequence based upon the observable sequence which yields true/false,
* and yields the values that were buffered while paused.
* @example
* var pauser = new Rx.Subject();
* var source = Rx.Observable.interval(100).pausableBuffered(pauser);
* @param {Observable} pauser The observable sequence used to pause the underlying sequence.
* @returns {Observable} The observable sequence which is paused based upon the pauser.
*/
observableProto.pausableBuffered = function (subject) {
// Shim in iterator support
var $iterator$ = (typeof Symbol === 'object' && Symbol.iterator) ||
'_es6shim_iterator_';
// Firefox ships a partial implementation using the name @@iterator.
// https://bugzilla.mozilla.org/show_bug.cgi?id=907077#c14
// So use that name if we detect it.
if (window.Set && typeof new window.Set()['@@iterator'] === 'function') {
$iterator$ = '@@iterator';
}
@mattpodwysocki
mattpodwysocki / fromEvent.js
Created March 26, 2014 17:16
Adding jQuery/Zepto support
var fn = typeof jQuery !== 'undefined' && jQuery ?
jQuery.fn :
(typeof Zepto !== 'undefined' && Zepto ? Zepto.fn : null);
if (fn) {
fn.onAsObservable = function(event, selector, resultSelector) {
var self = this;
return Rx.Observable.fromEventPattern(
function(h) { self.on(event, selector, h); },
function(h) { self.off(event, selector, h); },
/* Fix older IE events */
function fixEvent(event) {
var stopPropagation = function () {
this.cancelBubble = true;
};
var preventDefault = function () {
this.bubbledKeyCode = this.keyCode;
if (this.ctrlKey) {
try {
@mattpodwysocki
mattpodwysocki / pausable.js
Created April 6, 2014 00:20
Pausable and PausableBuffered changes
/*
* Pausable is for hot observables in which values are lost between pause and resume
*/
// With no outside controller
var pausable = Rx.Observable.fromEvent(document, 'mousemove').pausable();
var subscription = pausable.subscribe(function (next) {
// Do something with values
});
/**
* Creates an observable sequence from an incoming generator with no arguments
* @example
* var source = Observable.fromGenerator(function * () { yield 42; });
* @param {Function} generatorFunction A function which is a generator.
* @param {Scheduler} [scheduler] Optional scheduler for scheduling recursive calls. Defaults to current thread if not specified
* @returns {Observable} An observable sequence which is generated by the generator.
*/
Observable.spawn = function (generatorFunction, scheduler) {
return observableFromGenerator(observableFromGenerator, scheduler)();
var fnString = 'function';
function toThunk(obj, ctx) {
if (Array.isArray(obj)) {
return objectToThunk.call(ctx, obj);
}
if (isGeneratorFunction(obj)) {
return observableSpawn(obj.call(ctx));
}
function fakeEvent(value) {
return Rx.Observable.create(function(observer) {
// Not capturing release
setInterval(function () {
observer.onNext(value++);
}, 500);
}).publish().refCount();
}
@mattpodwysocki
mattpodwysocki / select-perf.js
Last active August 29, 2015 14:01
class based versus closure based approach for selectors
Rx.Observable.prototype.map = function (selector, thisArg) {
return new MapObservable(this, selector, thisArg);
};
var MapObserver = (function (__super__) {
function Klass(observable, observer) {
this.observable = observable;
this.observer = observer;
__super__.call(this);
/**
* Invokes a specified action after the source observable sequence terminates gracefully or exceptionally.
*
* @example
* var res = observable.finallyAction(function () { console.log('sequence ended'; });
* @param {Function} finallyAction Action to invoke after the source observable sequence terminates.
* @returns {Observable} Source sequence with the action-invoking termination behavior applied.
*/
observableProto['finally'] = observableProto.finallyAction = function (action) {
var source = this;