Skip to content

Instantly share code, notes, and snippets.

Avatar
🇫🇷

Arnaud Esteve aesteve

🇫🇷
View GitHub Profile
View CheckParams.java
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 Oct 11, 2016
Extension method in Kotlin to handle a method reference as a Handler
View HandlerExtension.kt
// 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 Oct 11, 2016
Creating interceptor-like logic just with handlers
View InterceptorExample.java
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
AST transform on AsyncResult methods
View WhatWeCouldDo.groovy
// 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
exception happening on CI only while testing Nubes with 3.3.0-SNAPSHOT
View Uh oh
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)
View Handlers.java
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 Jan 20, 2016
Go Map exercise
View maps.go
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 Jan 20, 2016
Slices (with closure)
View slices.go
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 Jan 19, 2016
Use Future<Void> instead of timers
View MainVerticle.java
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 Jan 18, 2016
What is flux
View whatsflux.md

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

You can’t perform that action at this time.