|
2 | 2 | * #%L |
3 | 3 | * Lite Renderer Add-On |
4 | 4 | * %% |
5 | | - * Copyright (C) 2024 Flowing Code |
| 5 | + * Copyright (C) 2024 - 2026 Flowing Code |
6 | 6 | * %% |
7 | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | 8 | * you may not use this file except in compliance with the License. |
|
19 | 19 | */ |
20 | 20 | package com.flowingcode.vaadin.addons.litetemplate; |
21 | 21 |
|
| 22 | +import com.flowingcode.vaadin.jsonmigration.JsonMigration; |
22 | 23 | import com.vaadin.flow.component.ClickEvent; |
23 | 24 | import com.vaadin.flow.component.Component; |
24 | 25 | import com.vaadin.flow.component.button.Button; |
25 | 26 | import com.vaadin.flow.data.renderer.LitRenderer; |
26 | 27 | import com.vaadin.flow.dom.Element; |
27 | 28 | import com.vaadin.flow.function.SerializableBiConsumer; |
28 | 29 | import com.vaadin.flow.function.ValueProvider; |
| 30 | +import com.vaadin.flow.server.Version; |
29 | 31 | import elemental.json.JsonArray; |
30 | 32 | import elemental.json.JsonBoolean; |
31 | 33 | import elemental.json.JsonNumber; |
32 | 34 | import elemental.json.JsonString; |
33 | 35 | import elemental.json.JsonValue; |
| 36 | +import java.lang.invoke.MethodHandle; |
| 37 | +import java.lang.invoke.MethodHandles; |
| 38 | +import java.lang.invoke.MethodType; |
34 | 39 | import java.util.HashMap; |
35 | 40 | import java.util.List; |
36 | 41 | import java.util.Map; |
37 | 42 | import java.util.function.Consumer; |
38 | 43 | import lombok.NonNull; |
| 44 | +import lombok.SneakyThrows; |
39 | 45 |
|
40 | 46 | final class LitRendererBuilder<SOURCE> { |
41 | 47 |
|
@@ -86,13 +92,38 @@ private LitRenderer<SOURCE> build( |
86 | 92 |
|
87 | 93 | String templateExpression = sb.toString() + "\n"; |
88 | 94 | var renderer = LitRenderer.<SOURCE>of(templateExpression); |
89 | | - functions.forEach((n, v) -> renderer.withFunction(n, v)); |
| 95 | + functions.forEach((n, v) -> withFunction(renderer, n, v)); |
90 | 96 | properties.forEach((n, v) -> renderer.withProperty(n, v)); |
91 | 97 |
|
92 | 98 | setTemplateExpression.accept(templateExpression); |
93 | 99 | return renderer; |
94 | 100 | } |
95 | 101 |
|
| 102 | + @SneakyThrows |
| 103 | + private LitRenderer<SOURCE> withFunction(LitRenderer<SOURCE> renderer, String name, |
| 104 | + SerializableBiConsumer<SOURCE, JsonArray> handler) { |
| 105 | + SerializableBiConsumer<SOURCE, ?> c = handler; |
| 106 | + if (Version.getMajorVersion() >= 25) { |
| 107 | + c = (source, array) -> handler.accept(source, |
| 108 | + (JsonArray) JsonMigration.convertToJsonValue(array)); |
| 109 | + } |
| 110 | + return (LitRenderer<SOURCE>) LitRenderer_withFunction.invokeExact(renderer, name, c); |
| 111 | + } |
| 112 | + |
| 113 | + private static final MethodHandle LitRenderer_withFunction = lookup_withFunction(); |
| 114 | + |
| 115 | + @SneakyThrows |
| 116 | + private static MethodHandle lookup_withFunction() { |
| 117 | + MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 118 | + MethodType methodType = MethodType.methodType( |
| 119 | + LitRenderer.class, |
| 120 | + String.class, |
| 121 | + SerializableBiConsumer.class |
| 122 | + ); |
| 123 | + |
| 124 | + return lookup.findVirtual(LitRenderer.class, "withFunction", methodType); |
| 125 | + } |
| 126 | + |
96 | 127 | private String addFunction(SerializableBiConsumer<SOURCE, JsonArray> handler) { |
97 | 128 | String name = "function" + functions.size(); |
98 | 129 | functions.put(name, handler); |
|
0 commit comments