|
| 1 | +/* |
| 2 | + * Copyright 2000-2025 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.flow.dom; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +import com.vaadin.flow.component.Component; |
| 21 | +import com.vaadin.flow.component.ComponentUtil; |
| 22 | +import com.vaadin.flow.component.UI; |
| 23 | +import com.vaadin.flow.component.UIDetachedException; |
| 24 | +import com.vaadin.flow.function.SerializableBiConsumer; |
| 25 | +import com.vaadin.flow.server.ErrorEvent; |
| 26 | +import com.vaadin.flow.shared.Registration; |
| 27 | +import com.vaadin.signals.Signal; |
| 28 | +import com.vaadin.signals.SignalEnvironment; |
| 29 | +import com.vaadin.signals.impl.Effect; |
| 30 | + |
| 31 | +/** |
| 32 | + * The utility class that provides helper methods for using Signal effects in a |
| 33 | + * context of a given element's life-cycle. |
| 34 | + * <p> |
| 35 | + * It ultimately creates a Signal effect, i.e. a call to |
| 36 | + * {@link Signal#effect(Runnable)}, that is automatically enabled when an |
| 37 | + * element is attached and disabled when the element is detached. Additionally, |
| 38 | + * it provides methods to bind signals to element according to a given value |
| 39 | + * setting function. |
| 40 | + * |
| 41 | + * @since 25.0 |
| 42 | + */ |
| 43 | +public final class ElementEffect { |
| 44 | + private final Runnable effectFunction; |
| 45 | + private boolean closed = false; |
| 46 | + private Effect effect = null; |
| 47 | + private Registration detachRegistration; |
| 48 | + |
| 49 | + public ElementEffect(Element owner, Runnable effectFunction) { |
| 50 | + Objects.requireNonNull(owner, "Owner element cannot be null"); |
| 51 | + Objects.requireNonNull(effectFunction, |
| 52 | + "Effect function cannot be null"); |
| 53 | + this.effectFunction = effectFunction; |
| 54 | + owner.addAttachListener(attach -> { |
| 55 | + enableEffect(attach.getSource()); |
| 56 | + |
| 57 | + detachRegistration = owner.addDetachListener(detach -> { |
| 58 | + disableEffect(); |
| 59 | + detachRegistration.remove(); |
| 60 | + detachRegistration = null; |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + if (owner.getNode().isAttached()) { |
| 65 | + enableEffect(owner); |
| 66 | + |
| 67 | + detachRegistration = owner.addDetachListener(detach -> { |
| 68 | + disableEffect(); |
| 69 | + detachRegistration.remove(); |
| 70 | + detachRegistration = null; |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates a Signal effect that is owned by a given element. The effect is |
| 77 | + * enabled when the element is attached and automatically disabled when it |
| 78 | + * is detached. |
| 79 | + * <p> |
| 80 | + * Example of usage: |
| 81 | + * |
| 82 | + * <pre> |
| 83 | + * Registration effect = ElementEffect.effect(myElement, () -> { |
| 84 | + * Notification.show("Element is attached and signal value is " |
| 85 | + * + someSignal.value()); |
| 86 | + * }); |
| 87 | + * effect.remove(); // to remove the effect when no longer needed |
| 88 | + * </pre> |
| 89 | + * |
| 90 | + * @see Signal#effect(Runnable) |
| 91 | + * @param owner |
| 92 | + * the owner element for which the effect is applied, must not be |
| 93 | + * <code>null</code> |
| 94 | + * @param effectFunction |
| 95 | + * the effect function to be executed when any dependency is |
| 96 | + * changed, must not be <code>null</code> |
| 97 | + * @return a {@link Registration} that can be used to remove the effect |
| 98 | + * function |
| 99 | + */ |
| 100 | + public static Registration effect(Element owner, Runnable effectFunction) { |
| 101 | + ElementEffect effect = new ElementEffect(owner, effectFunction); |
| 102 | + return effect::close; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Binds a <code>signal</code>'s value to a given owner element in a way |
| 107 | + * defined in <code>setter</code> function and creates a Signal effect |
| 108 | + * function executing the setter whenever the signal value changes. |
| 109 | + * <p> |
| 110 | + * Example of usage: |
| 111 | + * |
| 112 | + * <pre> |
| 113 | + * Element mySpan = new Element("span"); |
| 114 | + * Registration effect = ElementEffect.bind(mySpan, stringSignal, |
| 115 | + * Element::setText); |
| 116 | + * effect.remove(); // to remove the effect when no longer needed |
| 117 | + * |
| 118 | + * ElementEffect.bind(mySpan, stringSignal.map(value -> !value.isEmpty()), |
| 119 | + * Element::setVisible); |
| 120 | + * </pre> |
| 121 | + * |
| 122 | + * @see Signal#effect(Runnable) |
| 123 | + * @param owner |
| 124 | + * the owner element for which the effect is applied, must not be |
| 125 | + * <code>null</code> |
| 126 | + * @param signal |
| 127 | + * the signal whose value is to be bound to the element, must not |
| 128 | + * be <code>null</code> |
| 129 | + * @param setter |
| 130 | + * the setter function that defines how the signal value is |
| 131 | + * applied to the element, must not be <code>null</code> |
| 132 | + * @return a {@link Registration} that can be used to remove the effect |
| 133 | + * function |
| 134 | + * @param <T> |
| 135 | + * the type of the signal value |
| 136 | + */ |
| 137 | + public static <T> Registration bind(Element owner, Signal<T> signal, |
| 138 | + SerializableBiConsumer<Element, T> setter) { |
| 139 | + return effect(owner, () -> { |
| 140 | + setter.accept(owner, signal.value()); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + private void enableEffect(Element owner) { |
| 145 | + if (closed) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + Component parentComponent = ComponentUtil.findParentComponent(owner) |
| 150 | + .get(); |
| 151 | + UI ui = parentComponent.getUI().get(); |
| 152 | + |
| 153 | + Runnable errorHandlingEffectFunction = () -> { |
| 154 | + try { |
| 155 | + effectFunction.run(); |
| 156 | + } catch (Exception e) { |
| 157 | + ui.getSession().getErrorHandler() |
| 158 | + .error(new ErrorEvent(e, owner.getNode())); |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + assert effect == null; |
| 163 | + effect = new Effect(errorHandlingEffectFunction, command -> { |
| 164 | + if (UI.getCurrent() == ui) { |
| 165 | + // Run immediately if on the same UI |
| 166 | + command.run(); |
| 167 | + } else { |
| 168 | + SignalEnvironment.getDefaultEffectDispatcher().execute(() -> { |
| 169 | + try { |
| 170 | + // Guard against detach while waiting for lock |
| 171 | + if (effect != null) { |
| 172 | + ui.access(command::run); |
| 173 | + } |
| 174 | + } catch (UIDetachedException e) { |
| 175 | + // Effect was concurrently disabled -> nothing do to |
| 176 | + } |
| 177 | + }); |
| 178 | + } |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + private void disableEffect() { |
| 183 | + if (effect != null) { |
| 184 | + effect.dispose(); |
| 185 | + effect = null; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + public void close() { |
| 190 | + disableEffect(); |
| 191 | + closed = true; |
| 192 | + } |
| 193 | +} |
0 commit comments