Skip to content

Instantly share code, notes, and snippets.

View aesteve's full-sized avatar
👋

Arnaud Esteve aesteve

👋
  • Sant Cugat del Vallès, Barcelona, Catalonia, Spain
  • X @arnaudesteve
View GitHub Profile
public interface RouteDSL {
RouteDSL param(String paramName, Handler<RoutingContext> checker)
default<T> RouteDSL param(String paramName, Function<String, T> checker) {
return param(paramName, ctx -> {
String p = ctx.request().getParam(name);
try {
ctx.put(paramName, checker.apply(p));
ctx.next();
@aesteve
aesteve / HandlerExtension.kt
Created October 11, 2016 08:34
Extension method in Kotlin to handle a method reference as a Handler
// It's weird it doesn't work ootb since Handler<T> is a functional java interface
// I must have missed something
fun <T> asHandler(handler: (T) -> Unit): Handler<T> =
Handler { event -> handler(event) }
@aesteve
aesteve / InterceptorExample.java
Created October 11, 2016 07:48
Creating interceptor-like logic just with handlers
public class InterceptorExample extends AbstractVerticle {
public final static String API_PATH = "/api/v1/";
public final static String STATIC_FILES = "/static/";
// ...
private Router router;
@Override
public void start(Future<Void> future) {
@aesteve
aesteve / WhatWeCouldDo.groovy
Last active May 24, 2016 07:02
AST transform on AsyncResult methods
// Let's imagin you have such a service
class TodoMongoService {
void fetchById(String todoId, Handler<AsyncResult<Todo>> handler) {
mongo.find(id) { res ->
if(res.succeeded) {
handler.handle(Future.suceededFuture(res.result))
} else {
handler.handle(Future.failedFuture(res.cause))
}
}
@aesteve
aesteve / Uh oh
Created May 8, 2016 11:37
exception happening on CI only while testing Nubes with 3.3.0-SNAPSHOT
java.lang.IllegalStateException: Uh oh! Event loop context executing with wrong thread! Expected null got Thread[Test worker,5,main]
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$3(ContextImpl.java:343)
at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:240)
at io.vertx.core.http.impl.ConnectionManager$ConnQueue.http1xConnected(ConnectionManager.java:422)
at io.vertx.core.http.impl.ConnectionManager$ConnQueue.lambda$internalConnect$159(ConnectionManager.java:384)
at io.vertx.core.http.impl.ConnectionManager$ConnQueue$1.lambda$connect$157(ConnectionManager.java:282)
at io.vertx.core.net.impl.AsyncResolveBindConnectHelper.addListener(AsyncResolveBindConnectHelper.java:43)
at io.vertx.core.http.impl.ConnectionManager$ConnQueue$1.connect(ConnectionManager.java:280)
at io.vertx.core.http.impl.ConnectionManager$ConnQueue.internalConnect(ConnectionManager.java:395)
at io.vertx.core.http.impl.ConnectionManager$ConnQueue.createNewConnection(ConnectionManager.java:226)
List<Handler<RoutingContext>> handlers = /* ... */;
handlers.forEach(router.route("/a/b/")::handler); // KO : "You're attaching a handler to a route more than once"
handlers.forEach(handler -> router.route("/a/b").handler(handler)); // works
@aesteve
aesteve / maps.go
Created January 20, 2016 21:16
Go Map exercise
package main
import (
"golang.org/x/tour/wc";
"strings";
)
func WordCount(s string) map[string]int {
count := make(map[string]int)
words := strings.Fields(s)
@aesteve
aesteve / slices.go
Created January 20, 2016 20:57
Slices (with closure)
package main
import "golang.org/x/tour/pic"
func mult(x, y int) uint8 {
return uint8( x * y )
}
func mid(x, y int) uint8 {
return uint8( (x + y) / 2 )
@aesteve
aesteve / MainVerticle.java
Created January 19, 2016 08:57
Use Future<Void> instead of timers
package com.github.vertx.node.example;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
@aesteve
aesteve / whatsflux.md
Last active January 18, 2016 22:58
What is flux

Flux is an architecture to help client-side application deal with data coming from several endpoints (API endpoints, websockets, user interaction, etc.).

The basic idea is that you're connecting to many endpoints, and when your "connectors" catch a new piece of data, they're dispatching actions (through a dispatcher).

Reducers are listening to these actions. When they catch an event they're interested in (let's say UserReducer catches an USER_DATA_UPDATED event), they "do something" with the action. Most often, they're just storing the data attached to the action somewhere, and returning their new "state". (their state represent a portion of the global store).

Components (views, basically), on the other hand, can be connected to many reducers (or to the whole store). Thanks to a pure function (connect), you'll describe what the component properties will be for a given state of the global store. Let's say your component only needs some users data and not pricing info for instance. You'll c