Skip to content

Instantly share code, notes, and snippets.

View hermanbanken's full-sized avatar
🇳🇱

Herman hermanbanken

🇳🇱
View GitHub Profile
/* This file contains 5 core meteor packages
* Base64
* EJSON
* Tracker (formerly Deps)
* ReactiveDict
* ReactiveVar
* mrt:reactive-object
* http://stackoverflow.com/a/26996309/552203
* Copyright: Dean Radcliffe
*/
@hermanbanken
hermanbanken / index.html
Last active July 1, 2016 16:11
Difficulties with implicit syntax #2
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdn.rawgit.com/hermanbanken/f1af739c111f2f918406026705f53761/raw/c22eddb2c36c01a51c66740cf674093ba4bf8729/meteor-reactive-packages.js"></script>
<script>
var energyUse = ReactiveVar(0);
var energyAccumulate = ReactiveVar(0);
Tracker.autorun(() => {
// Do not register dependency on energyAccumulate
var prev = Tracker.nonreactive(() => energyAccumulate.get());
// Registering dependency on energyUse
energyAccumulate.set(prev + energyUse.get());
@hermanbanken
hermanbanken / index.html
Last active July 1, 2016 16:24
Difficulties with implicit syntax #1
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdn.rawgit.com/hermanbanken/f1af739c111f2f918406026705f53761/raw/c22eddb2c36c01a51c66740cf674093ba4bf8729/meteor-reactive-packages.js"></script>
@hermanbanken
hermanbanken / scaling.js
Last active July 29, 2016 11:29
Disposing and switching inner observables
'use strict';
var Rx = require("rx")
var test = require("qunitjs").test
/** stupid RxJS testing code **/
function createMessage(expected, actual) {
return ['Expected: [' + expected.toString() + "]", "Actual: [" + actual.toString() + ']'];
}
@hermanbanken
hermanbanken / rx.split.js
Created August 17, 2016 12:40
Using 1 observable, subscribe once and do two things
let observable: Observable<Bool>
////////// A
return observable.publish(obs =>
Observable.merge(
obs.filter(v => v).doOnNext(":)"),
obs.filter(v =>!v).doOnNext(":(")
)
)
@hermanbanken
hermanbanken / ShareReplayLatestWhileNext.kt
Last active August 26, 2016 13:04
Reactive Extensions RxJava/RxKotlin ShareReplayLatestWhileNext implementation. It differs from shareReplay and shareReplayLatestWhileConnected, as explained in the comment above the method.
package com.example.rx
import rx.Observable
import rx.Subscriber
/**
* Warning: these extensions do not do any synchronization so no thread-safety guarantees!
* Implication of this is that subscribers that did dispose on another thread could still receive values.
* As long as these are synchronizing themselves everything is OK.
*
@hermanbanken
hermanbanken / rx-debug.js
Last active September 27, 2016 17:36
Rx Debug, `.debug` operator for RxJS
import Rx from 'rx';
// ES6 source, compile with babel
// Usage:
// import hook from 'rx-debug'
// hook()
// Then:
// Observable.just(1).debug("your tag")
// You will observe subscriptions, nexts, error and completion.
@hermanbanken
hermanbanken / rx-collection.js
Last active October 4, 2016 06:32
Cycle.JS (RxJS 4.1) combining component collection properties (see https://github.com/cyclejs/cyclejs/issues/312)
import Rx from 'rx';
var Observable = Rx.Observable,
observableProto = Observable.prototype;
export default function(){
observableProto.collection = function(config) {
let root = this.share(), merge = [], latest = [];
if(config.merge) {
@hermanbanken
hermanbanken / WebSocketRx.scala
Created October 14, 2016 13:07
Accept WebSocket requests using Rx instead of Play Framework Iteratee and Enumerator
package org.example.play
import play.api.libs.iteratee.{Concurrent, Enumerator, Execution, Iteratee}
import play.api.mvc.{RequestHeader, Result, WebSocket}
import rx.lang.scala.subjects.PublishSubject
import rx.lang.scala.{Observable, Subject, Subscription}
import scala.concurrent.Future
import scala.language.postfixOps
@hermanbanken
hermanbanken / JsonTest.scala
Last active November 8, 2016 11:38
JSON readNullable bug
package controllers
import org.specs2.mutable.Specification
import play.api.libs.functional.syntax._
import play.api.libs.json._
object JsonTest {
val json = Json.obj(
"foo" -> Json.obj("a" -> 1, "b" -> 0),
"barA" -> 0,