Skip to content

Instantly share code, notes, and snippets.

@talfco
Last active August 5, 2019 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talfco/ee99c535b719f07bff03806f76f797a0 to your computer and use it in GitHub Desktop.
Save talfco/ee99c535b719f07bff03806f76f797a0 to your computer and use it in GitHub Desktop.
public void run1(String endPoint) {
WsProvider wsProvider = new WsProvider(endPoint);
Promise<ApiPromise> ready = ApiPromise.create(wsProvider);
IRpcFunction.Unsubscribe<Promise> unsubscribeHandler = null;
ready.then( (ApiPromise api) -> {
Promise<IRpcFunction.Unsubscribe<Promise>> invoke = api.rpc().chain().function("subscribeNewHead").invoke(
(IRpcFunction.SubscribeCallback<Header>) (Header header) ->
{
System.out.println("Chain is at block: " + header.getBlockNumber()+" trigger "+count);
count++;
if (count >5) {
unsubscribeHandler.unsubscribe();
}
});
return invoke;
}).then((IRpcFunction.Unsubscribe<Promise> result) -> {
// Results in compile error: "Variable used in lambda expression should be final or effectively final
unsubscribeHandler =result;
return null;
})._catch((err) -> {
err.printStackTrace();
return Promise.value(err);
});
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@talfco
Copy link
Author

talfco commented Aug 5, 2019

This results in a compile error on line 19. A local variable out-of-scope of the lambda expression must be final.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment