Skip to content

Instantly share code, notes, and snippets.

View b2whats's full-sized avatar
🎯
Focusing

Akimov Vladimir b2whats

🎯
Focusing
View GitHub Profile
@juandopazo
juandopazo / gist:1182244
Created August 30, 2011 22:14
JavaScript without logical operators or loops
function _if(truthy, then) {
[then, function () {}][+!truthy]();
}
function ifElse(truthy, then, _else) {
[then, _else][+!truthy]();
}
function and(a, b) {
return !!((!!a + !!b) >> 1);
@jgoux
jgoux / helpers.js
Last active April 6, 2016 16:21
redux-loop helpers
import { loop, Effects as E, liftState } from "redux-loop"
import {
cond, pathEq, map, T, reduce, toUpper, pipe, transpose,
lensProp, set as lensSet, view as lensView
} from "ramda"
export const mkInit = (f) => pipe(f, liftState)
export const mkActionType = (namespace) => (type) => `${namespace}/${type}`
// Require hook
function hook(compile, extension) {
require.extensions[extension] = function(m, filename) {
const tokens = compile(filename);
return m._compile('module.exports = ' + JSON.stringify(tokens), filename);
};
}
// Run require hook
hook(fetch, '.css');
@munro
munro / auto_restart_node.js
Created January 11, 2012 18:43
Auto Restart Node
// Auto restart server on file changes
(function () {
var fs = require('fs'),
original = require.extensions['.js'];
function exitOnChange(filename) {
fs.watchFile(filename, function (curr, prev) {
if (curr.mtime !== prev.mtime) {
process.exit();
}
@mattpodwysocki
mattpodwysocki / promises-observables.js
Last active May 15, 2016 17:48
New support with Promises for RxJS
// Observable to Promise
var promise = Rx.Observable.return(42).toPromise(RSVP.Promise);
promise.then(console.log.bind(console));
// => 42
// Using config instead of argument
Rx.config.Promise = RSVP.Promise;
var promise = Rx.Observable.return(42).toPromise();
promise.then(console.log.bind(console));
// => 42
@wmira
wmira / gist:da83efa1831bcc234e74
Created October 21, 2014 15:17
Rxjs ForkJoin
var Rx = require("rx");
/**
* Using rxjs's fork join to wait for multiple remote ajax calls
*/
var remoteData = Rx.Observable.forkJoin(
Rx.Observable.fromPromise($.get('/api/product/list')),
Rx.Observable.fromPromise($.get('/api/channels/list')),
Rx.Observable.fromPromise($.get('/api/entity/list/PERSONNEL'))
@aarongodin
aarongodin / parallel.html
Last active May 19, 2016 22:09
Parallel AJAX with Rx.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Parallel AJAX</title>
<style>
body {
font-family: Arial;
let everythingIsDone = Rx.Observable.fromEvent(em, ‘everythingIsDone’);
let eventSource = Rx.Observable.fromEvent(em, ‘event’).takeUntil(everythingIsDone).toArray().toPromise();
@jooyunghan
jooyunghan / gist:7497419caaca7e579357
Created March 13, 2015 09:53
GitHub Repo Searches
var _ = require('lodash');
var github = require('octonode');
var Rx = require('rx');
// repos :: Search -> Options -> Observable Data
// callback style is converted to Promise and then to Observable
function repos(search, options) {
return Rx.Observable.fromPromise(new Promise(function (resolve, reject) {
search.repos(options, function (err, body, header) {
var source = Rx.Observable.fromArray([1,2,3])
.flatMap(async function (x, i) {
var result = await Promise.resolve(x * i);
return result;
});
source.subscribe(function (x) { console.log(x); });