All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.davfx.ninio.script.OldJsonScriptRunner Maven / Gradle / Ivy

package com.davfx.ninio.script;

import com.davfx.ninio.common.Failable;
import com.davfx.util.PrependIterable;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParser;

@Deprecated
public final class OldJsonScriptRunner implements ScriptRunner {
	private final ScriptRunner wrappee;

	public OldJsonScriptRunner(ScriptRunner wrappee) {
		this.wrappee = wrappee;
	}

	@Override
	public void eval(Iterable script, Failable fail, final AsyncScriptFunction asyncFunction, final SyncScriptFunction syncFunction) {
		String underlyingFunctionVar = '\'' + OldJsonScriptRunner.class.getCanonicalName() + '\'';
		script = new PrependIterable("this[" + underlyingFunctionVar + "] = " + ExecutorScriptRunner.CALL_FUNCTION_NAME + ";"
				+ ExecutorScriptRunner.CALL_FUNCTION_NAME + " = function(parameter, callback) {"
					+ "return JSON.parse(this[" + underlyingFunctionVar + "](JSON.stringify(parameter), (callback == undefined) ? undefined : function(p) { callback(JSON.parse(p)); }));"
				+ "};", script);
		wrappee.eval(script, fail, new AsyncScriptFunction() {
			@Override
			public void call(String request, final AsyncScriptFunction.Callback callback) {
				asyncFunction.call((request == null) ? null : new JsonParser().parse(request), new AsyncScriptFunction.Callback() {
					@Override
					public void handle(JsonElement response) {
						if (response == null) {
							callback.handle(null);
							return;
						}
						if (response.equals(JsonNull.INSTANCE)) {
							callback.handle(null);
							return;
						}
						callback.handle(response.toString());
					}
					/*
					@Override
					public void close() {
						callback.close();
					}
					*/
				});
			}
		}, new SyncScriptFunction() {
			@Override
			public String call(String request) {
				JsonElement r = syncFunction.call((request == null) ? null : new JsonParser().parse(request));
				if (r == null) {
					return null;
				}
				if (r.equals(JsonNull.INSTANCE)) {
					return null;
				}
				return r.toString();
			}
		});
	}
	
	@Override
	public void close() {
		wrappee.close();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy