use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.
the class MessageHandler method processMessage.
/**
* Performs the actual processing of a server message when all dependencies
* have been loaded.
*
* @param valueMap
* the message payload
* @param lock
* the lock object for this response
* @param start
* the time stamp when processing started
*/
private void processMessage(ValueMap valueMap, Object lock, Date start) {
assert getServerId(valueMap) == -1 || getServerId(valueMap) == lastSeenServerSyncId;
try {
double processUidlStart = Duration.currentTimeMillis();
JsonObject json = valueMap.cast();
if (json.hasKey("templates")) {
TemplateRegistry templates = registry.getTemplateRegistry();
JsonObject templatesJson = json.getObject("templates");
templates.importFromJson(templatesJson);
}
if (json.hasKey("constants")) {
ConstantPool constantPool = registry.getConstantPool();
JsonObject constants = json.getObject("constants");
constantPool.importFromJson(constants);
}
if (json.hasKey("changes")) {
processChanges(json);
}
if (json.hasKey(JsonConstants.UIDL_KEY_EXECUTE)) {
// Invoke JS only after all tree changes have been
// propagated
Reactive.addPostFlushListener(() -> registry.getExecuteJavaScriptProcessor().execute(json.getArray(JsonConstants.UIDL_KEY_EXECUTE)));
}
Console.log("handleUIDLMessage: " + (Duration.currentTimeMillis() - processUidlStart) + " ms");
ValueMap meta = valueMap.getValueMap("meta");
if (meta != null) {
Profiler.enter("Error handling");
if (meta.containsKey(JsonConstants.META_SESSION_EXPIRED)) {
if (nextResponseSessionExpiredHandler != null) {
nextResponseSessionExpiredHandler.execute();
} else {
registry.getSystemErrorHandler().handleSessionExpiredError(null);
registry.getUILifecycle().setState(UIState.TERMINATED);
}
} else if (meta.containsKey("appError")) {
ValueMap error = meta.getValueMap("appError");
registry.getSystemErrorHandler().handleUnrecoverableError(error.getString("caption"), error.getString("message"), error.getString("details"), error.getString("url"));
registry.getUILifecycle().setState(UIState.TERMINATED);
}
Profiler.leave("Error handling");
}
nextResponseSessionExpiredHandler = null;
Reactive.flush();
lastProcessingTime = (int) ((new Date().getTime()) - start.getTime());
totalProcessingTime += lastProcessingTime;
if (!initialMessageHandled) {
initialMessageHandled = true;
double fetchStart = getFetchStartTime();
if (fetchStart != 0) {
int time = (int) (Duration.currentTimeMillis() - fetchStart);
Console.log("First response processed " + time + " ms after fetchStart");
}
bootstrapTime = calculateBootstrapTime();
if (Profiler.isEnabled() && bootstrapTime != -1) {
Profiler.logBootstrapTimings();
}
}
} finally {
Console.log(" Processing time was " + String.valueOf(lastProcessingTime) + "ms");
endRequestIfResponse(valueMap);
resumeResponseHandling(lock);
if (Profiler.isEnabled()) {
Scheduler.get().scheduleDeferred(() -> {
Profiler.logTimings();
Profiler.reset();
});
}
}
}
use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.
the class GwtTemplateBinderTest method setModelType.
private static void setModelType(StateNode node, JsonObject modelTypeJson) {
String constantId = "modelType " + modelTypeCount++;
JsonObject constantsJson = Json.createObject();
constantsJson.put(constantId, modelTypeJson);
ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
constantPool.importFromJson(constantsJson);
node.getMap(NodeFeatures.TEMPLATE).getProperty(NodeProperties.MODEL_DESCRIPTOR).setValue(constantId);
}
use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.
the class MessageHandler method processMessage.
/**
* Performs the actual processing of a server message when all dependencies
* have been loaded.
*
* @param valueMap
* the message payload
* @param lock
* the lock object for this response
* @param start
* the time stamp when processing started
*/
private void processMessage(ValueMap valueMap, Object lock, double start) {
assert getServerId(valueMap) == -1 || getServerId(valueMap) == lastSeenServerSyncId;
try {
double processUidlStart = Duration.currentTimeMillis();
JsonObject json = valueMap.cast();
if (json.hasKey("constants")) {
ConstantPool constantPool = registry.getConstantPool();
JsonObject constants = json.getObject("constants");
constantPool.importFromJson(constants);
}
if (json.hasKey("changes")) {
processChanges(json);
}
if (json.hasKey(JsonConstants.UIDL_KEY_EXECUTE)) {
// Invoke JS only after all tree changes have been
// propagated and after post flush listeners added during
// message processing (so add one more post flush listener which
// is called after all added post listeners).
Reactive.addPostFlushListener(() -> Reactive.addPostFlushListener(() -> registry.getExecuteJavaScriptProcessor().execute(json.getArray(JsonConstants.UIDL_KEY_EXECUTE))));
}
Console.log("handleUIDLMessage: " + (Duration.currentTimeMillis() - processUidlStart) + " ms");
Reactive.flush();
ValueMap meta = valueMap.getValueMap("meta");
if (meta != null) {
Profiler.enter("Error handling");
final UIState uiState = registry.getUILifecycle().getState();
if (meta.containsKey(JsonConstants.META_SESSION_EXPIRED)) {
if (nextResponseSessionExpiredHandler != null) {
nextResponseSessionExpiredHandler.execute();
} else if (uiState != UIState.TERMINATED) {
registry.getSystemErrorHandler().handleSessionExpiredError(null);
registry.getUILifecycle().setState(UIState.TERMINATED);
}
} else if (meta.containsKey("appError") && uiState != UIState.TERMINATED) {
ValueMap error = meta.getValueMap("appError");
registry.getSystemErrorHandler().handleUnrecoverableError(error.getString("caption"), error.getString("message"), error.getString("details"), error.getString("url"), error.getString("querySelector"));
registry.getUILifecycle().setState(UIState.TERMINATED);
}
Profiler.leave("Error handling");
}
nextResponseSessionExpiredHandler = null;
lastProcessingTime = (int) (Duration.currentTimeMillis() - start);
totalProcessingTime += lastProcessingTime;
if (!initialMessageHandled) {
initialMessageHandled = true;
double fetchStart = getFetchStartTime();
if (fetchStart != 0) {
int time = (int) (Duration.currentTimeMillis() - fetchStart);
Console.log("First response processed " + time + " ms after fetchStart");
}
bootstrapTime = calculateBootstrapTime();
if (Profiler.isEnabled() && bootstrapTime != -1) {
Profiler.logBootstrapTimings();
}
}
} finally {
Console.log(" Processing time was " + String.valueOf(lastProcessingTime) + "ms");
endRequestIfResponse(valueMap);
resumeResponseHandling(lock);
if (Profiler.isEnabled()) {
Scheduler.get().scheduleDeferred(() -> {
Profiler.logTimings();
Profiler.reset();
});
}
}
}
use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.
the class SimpleElementBindingStrategy method handleDomEvent.
private void handleDomEvent(Event event, Node element, StateNode node) {
assert element instanceof Element : "Cannot handle DOM event for a Node";
String type = event.getType();
NodeMap listenerMap = getDomEventListenerMap(node);
ConstantPool constantPool = node.getTree().getRegistry().getConstantPool();
String expressionConstantKey = (String) listenerMap.getProperty(type).getValue();
assert expressionConstantKey != null;
assert constantPool.has(expressionConstantKey);
JsArray<String> dataExpressions = constantPool.get(expressionConstantKey);
JsonObject eventData;
if (dataExpressions.isEmpty()) {
eventData = null;
} else {
eventData = Json.createObject();
for (int i = 0; i < dataExpressions.length(); i++) {
String expressionString = dataExpressions.get(i);
EventDataExpression expression = getOrCreateExpression(expressionString);
JsonValue expressionValue = expression.evaluate(event, (Element) element);
eventData.put(expressionString, expressionValue);
}
}
node.getTree().sendEventToServer(node, type, eventData);
}
use of com.vaadin.client.flow.ConstantPool in project flow by vaadin.
the class GwtTemplateBinderTest method gwtSetUp.
@Override
protected void gwtSetUp() throws Exception {
super.gwtSetUp();
registry = new Registry() {
{
set(TemplateRegistry.class, new TemplateRegistry());
set(ConstantPool.class, new ConstantPool());
set(ExistingElementMap.class, new ExistingElementMap());
set(InitialPropertiesHandler.class, new InitialPropertiesHandler(this));
set(StateTree.class, new StateTree(this) {
@Override
public void sendTemplateEventToServer(StateNode node, String methodName, JsArray<?> argValues) {
serverMethods.put(methodName, argValues);
serverRpcNodes.put(methodName, node);
}
});
}
};
tree = registry.getStateTree();
/**
* This state node is ALWAYS a template !!!
*/
stateNode = new StateNode(0, tree) {
@Override
public boolean hasFeature(int id) {
if (id == NodeFeatures.TEMPLATE) {
return true;
}
return super.hasFeature(id);
}
};
// Use the most common model structure by default
JsonObject modelType = Json.createObject();
modelType.put(MODEL_KEY, "String");
setModelType(stateNode, modelType);
}
Aggregations