<commit_before>package com.vaadin.components.grid.data;

import static com.google.gwt.query.client.GQuery.$$;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.vaadin.components.grid.GridComponent;

/**
 * Datasource where requestRows() is delegated to a js native function
 *
 * @author manolo
 */
public class GridJsFuncDataSource extends GridDataSource {
    private JavaScriptObject f;

    public GridJsFuncDataSource(JavaScriptObject jso, int rows, GridComponent grid) {
        super(grid);
        assert JsUtils.isFunction(jso);
        f = jso;
        size = rows;
    }

    @Override
    protected void requestRows(
            final int firstRowIndex,
            final int numberOfRows,
            final com.vaadin.client.data.AbstractRemoteDataSource.RequestRowsCallback<JsArrayMixed> callback) {
        JsArrayMixed o = exec(f, firstRowIndex, numberOfRows,
                new AsyncCallback<JsArrayMixed>() {
                    public void onFailure(Throwable caught) {
                    }

                    public void onSuccess(JsArrayMixed result) {
                        setRowData(firstRowIndex, result);
                    }
                });
        if (o != null) {
            setRowData(firstRowIndex, o);
        }
    }

    @Override
    public Object getRowKey(JsArrayMixed row) {
        return JsUtils.JSON2String(row);
    }

    private native JsArrayMixed exec(JavaScriptObject f, int idx,
            int count, AsyncCallback<JsArrayMixed> cb)
   /*-{
       return f(idx, count, function(r) {
           cb.@com.google.gwt.user.client.rpc.AsyncCallback::onSuccess(*)(r);
       });
   }-*/;
}<commit_msg>Refactor execution of js datasource functions<commit_after>package com.vaadin.components.grid.data;

import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.GQuery.console;
import static com.google.gwt.query.client.GQuery.window;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayMixed;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.js.JsUtils;
import com.vaadin.components.grid.GridComponent;

/**
 * Datasource where requestRows() is delegated to a js native function
 */
public class GridJsFuncDataSource extends GridDataSource {

    private JavaScriptObject jsFunction;

    public GridJsFuncDataSource(JavaScriptObject jso, int rows, GridComponent grid) {
        super(grid);
        assert JsUtils.isFunction(jso);
        jsFunction = jso;
        size = rows;
    }

    @Override
    protected void requestRows(
            final int firstRowIndex,
            final int numberOfRows,
            final com.vaadin.client.data.AbstractRemoteDataSource.RequestRowsCallback<JsArrayMixed> callback) {

        JavaScriptObject jsCallback = JsUtils.wrapFunction(new Function() {
            public void f() {
                JsArrayMixed result = arguments(0);
                setRowData(firstRowIndex, result);
            }
        });

        // When we call the native function, it could return the array of rows directly
        // or via the provided callback function.
        JsArrayMixed result = JsUtils.jsni(jsFunction, "call", jsFunction, firstRowIndex, numberOfRows, jsCallback);
        if (result != null) {
            setRowData(firstRowIndex, result);
        }
    }

    @Override
    public Object getRowKey(JsArrayMixed row) {
        return JsUtils.JSON2String(row);
    }
}
{'fraction_non_alphanumeric': 0.09293373659609569, 'fraction_numerical': 0.0008248556502612042, 'mean_word_length': 2.6673387096774195, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1121385', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1012, 'n_words': 230}