Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:reactiverse/es4x into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
pmlopes committed Jul 27, 2020
2 parents 426d5f0 + 2a6bfa3 commit ae80495
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
8 changes: 6 additions & 2 deletions es4x/src/main/java/io/reactiverse/es4x/EventEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.reactiverse.es4x;

import org.graalvm.polyglot.Value;

/**
* Simple EventEmitter
*/
Expand All @@ -26,12 +28,14 @@ public interface EventEmitter {
* @param eventName the event name
* @param callback the action to perform
*/
void on(String eventName, Runnable callback);
void on(String eventName, Value callback);

/**
* Will trigger the event callback if any on the given event.
*
* @param eventName the event.
* @param args arguments to be passed
* @return callback arity
*/
void emit(String eventName);
int emit(String eventName, Object... args);
}
25 changes: 16 additions & 9 deletions es4x/src/main/java/io/reactiverse/es4x/impl/EventEmitterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,35 @@
package io.reactiverse.es4x.impl;

import io.reactiverse.es4x.EventEmitter;
import org.graalvm.polyglot.Value;

import java.util.HashMap;
import java.util.Map;

public class EventEmitterImpl implements EventEmitter {

private Map<String, Runnable> events;
private Map<String, Value> events;

@Override
public void on(String eventName, Runnable callback) {
if (events == null) {
events = new HashMap<>();
public void on(String eventName, Value callback) {
if (callback.canExecute()) {
if (events == null) {
events = new HashMap<>();
}
events.put(eventName, callback);
}
events.put(eventName, callback);
}

public void emit(String eventName) {
@Override
public int emit(String eventName, Object... args) {
if (events != null) {
Runnable r = events.get(eventName);
if (r != null) {
r.run();
Value cb = events.get(eventName);
if (cb != null) {
int length = cb.getMember("length").asInt();
cb.executeVoid(args);
return length;
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ public void start(Future<Void> startFuture) {
public void stop(Future<Void> stopFuture) {
try {
runtime.enter();
runtime.emit("undeploy");
stopFuture.complete();
int arity = runtime.emit("undeploy", stopFuture);
if (arity == 0) {
stopFuture.complete();
}
} catch (RuntimeException e) {
stopFuture.fail(e);
} finally {
Expand Down
12 changes: 12 additions & 0 deletions es4x/src/test/java/io/reactiverse/es4x/test/FactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public void shouldDeployVerticleWithOnStop(TestContext ctx) {
});
}

@Test(timeout = 10000)
public void shouldDeployVerticleWithOnStopAsync(TestContext ctx) {
final Async async = ctx.async();
rule.vertx().deployVerticle("js:./verticle4.js", deploy -> {
ctx.assertTrue(deploy.succeeded());
rule.vertx().setTimer(1000L, t -> rule.vertx().undeploy(deploy.result(), undeploy -> {
ctx.assertTrue(undeploy.succeeded());
async.complete();
}));
});
}

@Test(timeout = 10000)
public void shouldDeployVerticleWithoutOnStop(TestContext ctx) {
final Async async = ctx.async();
Expand Down
6 changes: 6 additions & 0 deletions es4x/src/test/resources/verticle4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
print('deployed (4)!');

process.on('undeploy', function (fut) {
print('onStop');
fut.complete();
});

0 comments on commit ae80495

Please sign in to comment.