-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a "ShortcutReader" interface #2025
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.phoenicis.library; | ||
|
||
import org.phoenicis.library.dto.ShortcutDTO; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Interface for all shortcut reader implementations | ||
*/ | ||
public interface ShortcutReader { | ||
/** | ||
* Sets the shortcut | ||
* | ||
* @param shortcut The shortcut | ||
*/ | ||
void of(ShortcutDTO shortcut); | ||
|
||
/** | ||
* Returns the name of the container belonging to the shortcut | ||
* | ||
* @return The container name | ||
*/ | ||
String getContainer(); | ||
|
||
/** | ||
* Runs a shortcut with the given user arguments | ||
* | ||
* @param arguments The user arguments | ||
*/ | ||
void run(List<String> arguments); | ||
|
||
/** | ||
* Stops the running shortcut | ||
*/ | ||
void stop(); | ||
|
||
/** | ||
* Uninstalls the shortcut | ||
*/ | ||
void uninstall(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,8 @@ | |
|
||
import org.graalvm.polyglot.Value; | ||
import org.phoenicis.library.dto.ShortcutDTO; | ||
import org.phoenicis.scripts.session.InteractiveScriptSession; | ||
import org.phoenicis.scripts.interpreter.ScriptInterpreter; | ||
import org.phoenicis.scripts.session.InteractiveScriptSession; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
@@ -42,25 +42,31 @@ public void run(String shortcutName, List<String> arguments, Consumer<Exception> | |
public void run(ShortcutDTO shortcutDTO, List<String> arguments, Consumer<Exception> errorCallback) { | ||
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession(); | ||
|
||
interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");", | ||
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> { | ||
final Value shortcutReader = (Value) output; | ||
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");", | ||
result -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is the same everywhere. Can we extract it to a separate method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My idea was to do this later on in a centralized method in public interface PhoenicisScriptEngine {
<E> E evalAndReturn(String script, Class<E> type);
<E> void eval(String script, Class<E> type, Consumer<E> onSuccess, Consumer<Exception> onError);
} See also our documentation. For now I would suggest to retain the redundancy and fix this later for all script types (i.e. engines, installers, plugins, shortcuts, verbs etc.) at once. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with that. |
||
Value shortcutReaderClass = (Value) result; | ||
|
||
shortcutReader.invokeMember("of", shortcutDTO); | ||
shortcutReader.invokeMember("run", arguments); | ||
}, errorCallback), errorCallback); | ||
ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class); | ||
|
||
shortcutReader.of(shortcutDTO); | ||
shortcutReader.run(arguments); | ||
}, | ||
errorCallback); | ||
} | ||
|
||
public void stop(ShortcutDTO shortcutDTO, Consumer<Exception> errorCallback) { | ||
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession(); | ||
|
||
interactiveScriptSession.eval("const ShortcutReader = include(\"engines.wine.shortcuts.reader\");", | ||
ignored -> interactiveScriptSession.eval("new ShortcutReader()", output -> { | ||
final Value shortcutReader = (Value) output; | ||
interactiveScriptSession.eval("include(\"engines.wine.shortcuts.reader\");", | ||
result -> { | ||
Value shortcutReaderClass = (Value) result; | ||
|
||
ShortcutReader shortcutReader = shortcutReaderClass.newInstance().as(ShortcutReader.class); | ||
|
||
shortcutReader.invokeMember("of", shortcutDTO); | ||
shortcutReader.invokeMember("stop"); | ||
}, errorCallback), errorCallback); | ||
shortcutReader.of(shortcutDTO); | ||
shortcutReader.stop(); | ||
}, | ||
errorCallback); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still thinking whether it is better to add the functionality of this method to the constructor.
Changing the constructor to have the signature:
has the benefit that the object usage is cleaner. In addition it has the disadvantage that we can't add the constructor requirement to the interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is part of the constructor, it cannot be defined in the interface, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly