-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
144 changed files
with
3,368 additions
and
46,749 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -771,13 +771,14 @@ private static native void runPromiseExpression(String expression, | |
/*-{ | ||
try { | ||
var promise = [email protected]::get(*)(); | ||
if ( !(promise instanceof Promise )){ | ||
if ( !(promise instanceof $wnd.Promise )){ | ||
throw new Error('The expression "'+expression+'" result is not a Promise.'); | ||
} | ||
promise.then( function(result) { [email protected]::run(*)(); } , | ||
function(error) { [email protected]::run(*)(); } ); | ||
function(error) { console.error(error); [email protected]::run(*)(); } ); | ||
} | ||
catch(error) { | ||
console.error(error); | ||
[email protected]::run(*)(); | ||
} | ||
}-*/; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
*/ | ||
package com.vaadin.client.flow.binding; | ||
|
||
|
||
import jsinterop.annotations.JsFunction; | ||
|
||
import com.google.gwt.core.client.JavaScriptObject; | ||
|
@@ -28,6 +27,7 @@ | |
import com.vaadin.client.flow.collection.JsMap; | ||
import com.vaadin.client.flow.util.NativeFunction; | ||
import com.vaadin.flow.internal.nodefeature.NodeFeatures; | ||
import com.vaadin.flow.shared.JsonConstants; | ||
|
||
import elemental.dom.Element; | ||
import elemental.dom.Node; | ||
|
@@ -46,6 +46,8 @@ public final class ServerEventObject extends JavaScriptObject { | |
private static final String NODE_ID = "nodeId"; | ||
private static final String EVENT_PREFIX = "event"; | ||
|
||
private static final String PROMISE_CALLBACK_NAME = JsonConstants.RPC_PROMISE_CALLBACK_NAME; | ||
|
||
/** | ||
* Callback interface for an event data expression parsed using new | ||
* Function() in JavaScript. | ||
|
@@ -76,6 +78,31 @@ private interface ServerEventDataExpression { | |
protected ServerEventObject() { | ||
} | ||
|
||
private native void initPromiseHandler() | ||
/*-{ | ||
var name = @ServerEventObject::PROMISE_CALLBACK_NAME | ||
// Use defineProperty to make it non-enumerable | ||
Object.defineProperty(this, name, { | ||
value: function(promiseId, success, value) { | ||
var promise = this[name].promises[promiseId]; | ||
// undefined if client-side node was recreated after execution was scheduled | ||
if (promise !== undefined) { | ||
delete this[name].promises[promiseId]; | ||
if (success) { | ||
// Resolve | ||
promise[0](value); | ||
} else { | ||
// Reject | ||
promise[1](Error("Something went wrong. Check server-side logs for more information.")); | ||
} | ||
} | ||
} | ||
}); | ||
this[name].promises = []; | ||
}-*/; | ||
|
||
/** | ||
* Defines a method with the given name to be a callback to the server for | ||
* the given state node. | ||
|
@@ -88,8 +115,13 @@ protected ServerEventObject() { | |
* @param node | ||
* the node to use as an identifier when sending an event to the | ||
* server | ||
* @param returnPromise | ||
* <code>true</code> if the handler should return a promise that | ||
* will reflect the server-side result; <code>false</code> to not | ||
* return any value | ||
*/ | ||
public native void defineMethod(String methodName, StateNode node) | ||
public native void defineMethod(String methodName, StateNode node, | ||
boolean returnPromise) | ||
/*-{ | ||
this[methodName] = $entry(function(eventParameter) { | ||
var prototype = Object.getPrototypeOf(this); | ||
|
@@ -102,7 +134,24 @@ public native void defineMethod(String methodName, StateNode node) | |
if(args === null) { | ||
args = Array.prototype.slice.call(arguments); | ||
} | ||
[email protected]::sendTemplateEventToServer(*)(node, methodName, args); | ||
var returnValue; | ||
var promiseId = -1; | ||
if (returnPromise) { | ||
var promises = this[@ServerEventObject::PROMISE_CALLBACK_NAME].promises; | ||
promiseId = promises.length; | ||
returnValue = new Promise(function(resolve, reject) { | ||
// Store each callback for later use | ||
promises[promiseId] = [resolve, reject]; | ||
}); | ||
} | ||
[email protected]::sendTemplateEventToServer(*)(node, methodName, args, promiseId); | ||
return returnValue; | ||
}); | ||
}-*/; | ||
|
||
|
@@ -230,6 +279,7 @@ public static ServerEventObject get(Element element) { | |
.crazyJsoCast(WidgetUtil.getJsProperty(element, "$server")); | ||
if (serverObject == null) { | ||
serverObject = (ServerEventObject) JavaScriptObject.createObject(); | ||
serverObject.initPromiseHandler(); | ||
WidgetUtil.setJsProperty(element, "$server", serverObject); | ||
} | ||
return serverObject; | ||
|
Oops, something went wrong.