Skip to content
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

Make ArrayUpdater backwards compatible #4427

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import java.io.Serializable;
import java.util.List;

import com.vaadin.flow.internal.JsonCodec;

import elemental.json.JsonValue;

/**
Expand Down Expand Up @@ -47,19 +45,6 @@ public interface Update extends Serializable {
*/
void clear(int start, int length);

/**
* Clears {@code length} elements in array from the {@code start}
* position.
*
* @param start
* the start index
* @param length
* the number of elements to clear
* @param parentKey
* Parent item key that cleared range affects
*/
void clear(int start, int length, String parentKey);

/**
* Sets the {@code items} at the {@code start} position.
*
Expand All @@ -70,59 +55,13 @@ public interface Update extends Serializable {
*/
void set(int start, List<JsonValue> items);

/**
* Sets the {@code items} at the {@code start} position.
*
* @param start
* the start index
* @param items
* the items to set
* @param parentKey
* Parent item key where given items belongs to
*/
void set(int start, List<JsonValue> items, String parentKey);

/**
* Commits changes for the given {@code updateId}.
*
* @param updateId
* the update identifier of the commit
*/
void commit(int updateId);

/**
* Commits changes for the given {@code updateId} and parent key.
*
* @param updateId
* the update identifier of the commit for the target
* parentKey
* @param parentKey
* target parent key
* @param levelSize
* Total number of direct child items for the given parent
* key
*/
void commit(int updateId, String parentKey, int levelSize);

/**
* Commits enqueued function calls added via
* {@link #enqueue(String, Serializable...)}.
*/
void commit();

/**
* Enqueue function call with the given arguments.
*
* @see JsonCodec JsonCodec for supported argument types
* @param name
* the name of the function to call, may contain dots to
* indicate a function on a property.
* @param arguments
* the arguments to pass to the function. Must be of a type
* supported by the communication mechanism, as defined by
* {@link JsonCodec}
*/
void enqueue(String name, Serializable... arguments);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2000-2018 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow.data.provider.hierarchy;

import java.io.Serializable;
import java.util.List;

import com.vaadin.flow.data.provider.ArrayUpdater;
import com.vaadin.flow.internal.JsonCodec;

import elemental.json.JsonValue;

/**
* Hierarchical array update strategy aware class.
*
* @author Vaadin Ltd
* @since 1.1
*
*/
public interface HierarchicalArrayUpdater extends ArrayUpdater {

/**
* Array updater strategy that is aware of hierarchical changes.
*/
public interface HierarchicalUpdate extends Update {

/**
* Clears {@code length} elements in array from the {@code start}
* position.
*
* @param start
* the start index
* @param length
* the number of elements to clear
* @param parentKey
* Parent item key that cleared range affects
*/
void clear(int start, int length, String parentKey);

/**
* Sets the {@code items} at the {@code start} position.
*
* @param start
* the start index
* @param items
* the items to set
* @param parentKey
* Parent item key where given items belongs to
*/
void set(int start, List<JsonValue> items, String parentKey);

/**
* Commits enqueued function calls added via
* {@link #enqueue(String, Serializable...)}.
*/
void commit();

/**
* Enqueue function call with the given arguments.
*
* @see JsonCodec JsonCodec for supported argument types
* @param name
* the name of the function to call, may contain dots to
* indicate a function on a property.
* @param arguments
* the arguments to pass to the function. Must be of a type
* supported by the communication mechanism, as defined by
* {@link JsonCodec}
*/
void enqueue(String name, Serializable... arguments);

/**
* Commits changes for the given {@code updateId} and parent key.
*
* @param updateId
* the update identifier of the commit for the target
* parentKey
* @param parentKey
* target parent key
* @param levelSize
* Total number of direct child items for the given parent
* key
*/
void commit(int updateId, String parentKey, int levelSize);

}

@Override
HierarchicalUpdate startUpdate(int sizeChange);

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.data.provider;
package com.vaadin.flow.data.provider.hierarchy;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -27,7 +27,9 @@
import java.util.stream.Stream;

import com.vaadin.flow.data.provider.ArrayUpdater.Update;
import com.vaadin.flow.data.provider.hierarchy.HierarchyMapper;
import com.vaadin.flow.data.provider.DataGenerator;
import com.vaadin.flow.data.provider.DataKeyMapper;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate;
import com.vaadin.flow.function.SerializableBiFunction;
import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.internal.Range;
Expand All @@ -37,22 +39,16 @@
import elemental.json.JsonValue;

/**
* CommunicationController controls all Grid's communication to client.
* <p>
* TODO move this class to 'flow-data' module first. Then get rid of the
* {@link Update} in this class by replacing {@link Update#set(int, List)},
* {@link Update#clear(int, int)} and {@link Update#commit(int)} methods with
* {@link Update#set(int, List, String)}, {@link Update#clear(int, int, String)}
* and {@link Update#commit(int, String, int)}.
* HierarchicalCommunicationController controls all the communication to client.
*
* @param <T>
* the target bean type
*/
public class CommunicationController<T> implements Serializable {
public class HierarchicalCommunicationController<T> implements Serializable {

private final DataKeyMapper<T> keyMapper;
private final DataGenerator<T> dataGenerator;
private final SerializableFunction<Integer, Update> startUpdate;
private final SerializableFunction<Integer, HierarchicalUpdate> startUpdate;
private final HierarchyMapper<T, ?> mapper;
private final SerializableBiFunction<String, Range, Stream<T>> fetchItems;

Expand Down Expand Up @@ -100,11 +96,10 @@ public class CommunicationController<T> implements Serializable {
* Function for fetching items for target parent and specified
* range
*/
public CommunicationController(String parentKey,
DataKeyMapper<T> keyMapper,
HierarchyMapper<T, ?> mapper,
public HierarchicalCommunicationController(String parentKey,
DataKeyMapper<T> keyMapper, HierarchyMapper<T, ?> mapper,
DataGenerator<T> dataGenerator,
SerializableFunction<Integer, Update> startUpdate,
SerializableFunction<Integer, HierarchicalUpdate> startUpdate,
SerializableBiFunction<String, Range, Stream<T>> fetchItems) {
this.parentKey = parentKey;
this.keyMapper = keyMapper;
Expand Down Expand Up @@ -135,7 +130,7 @@ public void flush() {
activeStart = effectiveRequested.getStart();

// Phase 2: Collect changes to send
Update update = startUpdate.apply(assumedSize);
HierarchicalUpdate update = startUpdate.apply(assumedSize);
boolean updated = collectChangesToSend(previousActive,
effectiveRequested, update);

Expand All @@ -162,7 +157,7 @@ public void setResendEntireRange(boolean resend) {
}

private boolean collectChangesToSend(final Range previousActive,
final Range effectiveRequested, Update update) {
final Range effectiveRequested, HierarchicalUpdate update) {
boolean updated = false;
if (assumeEmptyClient || resendEntireRange) {
if (!assumeEmptyClient) {
Expand Down Expand Up @@ -199,7 +194,7 @@ private boolean collectChangesToSend(final Range previousActive,
return updated;
}

private void set(Range effectiveRequested, Update update) {
private void set(Range effectiveRequested, HierarchicalUpdate update) {
if (effectiveRequested.isEmpty() || activeKeyOrder.isEmpty()
|| effectiveRequested.getStart() >= assumedSize) {
return;
Expand All @@ -209,12 +204,11 @@ private void set(Range effectiveRequested, Update update) {
getJsonItems(effectiveRequested));
} else {
update.set(effectiveRequested.getStart(),
getJsonItems(effectiveRequested),
parentKey);
getJsonItems(effectiveRequested), parentKey);
}
}

private void clear(int start, int length, Update update) {
private void clear(int start, int length, HierarchicalUpdate update) {
if (length == 0 || start >= assumedSize) {
return;
}
Expand Down Expand Up @@ -259,7 +253,7 @@ private List<String> activate(Range range) {

// XXX Explicitly refresh anything that is updated
List<String> activeKeys = new ArrayList<>(range.length());

fetchItems.apply(parentKey, range).forEach(bean -> {
boolean mapperHasKey = keyMapper.has(bean);
String key = keyMapper.key(bean);
Expand All @@ -273,7 +267,7 @@ private List<String> activate(Range range) {
}

private void passivateInactiveKeys(Set<String> oldActive,
List<String> newActiveKeyOrder, Update update,
List<String> newActiveKeyOrder, HierarchicalUpdate update,
boolean updated) {
/*
* We cannot immediately unregister keys that we have asked the client
Expand All @@ -283,8 +277,8 @@ private void passivateInactiveKeys(Set<String> oldActive,
*/
if (updated) {
int updateId = nextUpdateId++;
if(parentKey == null) {

if (parentKey == null) {
update.commit(updateId);
} else {
update.commit(updateId, parentKey, assumedSize);
Expand Down
Loading