Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package react4j.core;
import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jsinterop.annotations.JsFunction;
import jsinterop.base.Js;
import jsinterop.base.JsPropertyMap;
import static org.realityforge.braincheck.Guards.*;
/**
* The base java class that mirrors the react component.
*
* @param
the type of props that this component supports.
* @param the type of state that this component maintains.
*/
public abstract class Component
{
/**
* Callback function for updating state.
* Useful if the state update is based on current state.
*/
@JsFunction
public interface SetStateCallback
{
/**
* Callback used to update state.
* Result is merged into existing state.
* Return null to abort state update.
*
* @param previousState the preiovus state.
* @param currentProps the current props.
* @return the state to shallow merge or null to abort state update.
*/
@Nullable
S onSetState( @Nullable S previousState, @Nullable P currentProps );
}
@Nonnull
private ComponentPhase _phase = ComponentPhase.INITIALIZING;
@Nonnull
private ComponentState _state = ComponentState.UNKNOWN;
@Nullable
private NativeComponent
_nativeComponent;
/**
* Set the phase of the component. Only used for invariant checking.
*/
final void setPhase( @Nonnull final ComponentPhase phase )
{
invariant( ReactConfig::checkComponentStateInvariants,
() -> "Component.setComponentPhase() invoked on " + this +
" when ReactConfig.checkComponentStateInvariants() is false" );
_phase = Objects.requireNonNull( phase );
}
/**
* Set the state of the component. Only used for invariant checking.
*/
final void setState( @Nonnull final ComponentState state )
{
invariant( ReactConfig::checkComponentStateInvariants,
() -> "Component.setState() invoked on " + this +
" when ReactConfig.checkComponentStateInvariants() is false" );
_state = Objects.requireNonNull( state );
}
final void bindComponent( @Nonnull final NativeComponent
nativeComponent )
{
_nativeComponent = Objects.requireNonNull( nativeComponent );
}
/**
* Set the initial state of the component.
* This should only be invoked when the component is initializing.
* Calling this at any other time is an error.
*
* @param state the state.
*/
protected final void setInitialState( @Nonnull final S state )
{
if ( ReactConfig.checkComponentStateInvariants() )
{
apiInvariant( () -> ComponentPhase.INITIALIZING == _phase,
() -> "Attempted to invoke setInitialState on " + this + " when component is " +
"not in INITIALIZING phase but in phase " + _phase + " and state " + _state );
}
component().setInitialState( state );
}
/**
* Return the native react component.
*/
@Nonnull
private NativeComponent
component()
{
invariant( () -> null != _nativeComponent,
() -> "Invoked component() on " + this + " before a component has been bound." );
assert null != _nativeComponent;
return _nativeComponent;
}
/**
* Return true if a native component has been bound to this component.
* This should be true when {@link #componentWillMount()} is invoked and will
* be false after {@link #componentWillUnmount()} has completed.
*
* @return true if a native component has been bound to this component.
*/
protected final boolean isComponentBound()
{
return null != _nativeComponent;
}
/**
* Return the component state from the native component.
* This may be null if initial state was never set.
*
* @return the component state.
*/
protected S state()
{
return component().state();
}
/**
* Return the component props from the native component.
* This may be null if no probs were supplied.
*
* @return the component state.
*/
protected P props()
{
return component().props();
}
/**
* Return the map of refs from native components.
*
* @return the map of refs from native components.
*/
@Unsupported( "It is unclear what use case there is for getting all refs so this may be removed in the future" )
@Nonnull
protected final JsPropertyMap