@Override public <T> void subscribe( final Observable<T> observable, final Object onNext, final Object onError, final Object onCompleted) { // final ExecutionContext context = ExecutionContext // .createGlobalExecutionContext(((DynJSRuntime)NodynRunner.getRuntime()).runtime); // .createFunctionExecutionContext(((JavascriptFunction)onNext)., // onNext, null, "test"); /* try { context.setFunctionParameters(new Object[] { context.createPropertyReference("testval", "someName") //new DynObject() // GlobalObject.newGlobalObject(NodynRunner.getRuntime()) }); ((JavascriptFunction) onNext).call(context); } catch (final Exception e1) { e1.printStackTrace(); } */ if (observable == null) throw new NullPointerException("Can't subscribe to null-observer"); for (Object callback : Arrays.asList(onNext, onError, onCompleted)) if (callback != null && callback instanceof JavascriptFunction == false) throw new IllegalStateException( "UNEXPECTED callback type: " + callback.getClass().getName()); try { final ExecutionContext context = ExecutionContext.createGlobalExecutionContext( (DynJS) DynJSRuntime.class.getField("runtime").get(NodynRunner.getRuntime())); observable.subscribe( new Observer<T>() { @Override public void onCompleted() { if (onCompleted != null) ((JavascriptFunction) onCompleted).call(context); } @Override public void onError(final Throwable e) { if (onError == null) return; context.setFunctionParameters(new Object[] {e}); ((JavascriptFunction) onError).call(context); } @Override public void onNext(final T t) { if (onNext == null) return; context.setFunctionParameters(new Object[] {t}); ((JavascriptFunction) onNext).call(context); } }); } catch (final Exception e) { LOG.error("failed", e); } }