<commit_before>/**
 * Copyright © 2018 Viro Media. All rights reserved.
 */
package com.viromedia.bridge.component;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.viro.core.PortalScene;
import com.viro.core.Texture;
import com.viro.core.internal.Image;
import com.viromedia.bridge.component.node.VRTNode;
import com.viromedia.bridge.component.node.VRTScene;
import com.viromedia.bridge.utility.HdrImageDownloader;
import com.viromedia.bridge.utility.ViroEvents;

public class VRTLightingEnvironment extends VRTNode {
    private ReadableMap mSourceMap;
    private Image mLatestImage;
    private Texture mLatestTexture;
    private boolean mImageNeedsDownload;
    private IBLImageDownloadListener mHdrImageDownloadListener;
    private PortalScene mTargetedPortalScene = null;
    private boolean mHasSetScene;

    public VRTLightingEnvironment(ReactApplicationContext context) {
        super(context);
        mImageNeedsDownload = false;
        mHasSetScene = false;
    }

    public void setSource(ReadableMap source) {
        mSourceMap = source;
        mImageNeedsDownload = true;
    }

    @Override
    public void onPropsSet() {
        super.onPropsSet();
        if (!mImageNeedsDownload || mSourceMap == null || !mHasSetScene) {
            return;
        }

        if (mHdrImageDownloadListener != null){
            mHdrImageDownloadListener.invalidate();
        }

        imageDownloadDidStart();

        mHdrImageDownloadListener = new IBLImageDownloadListener();
        HdrImageDownloader.getHdrTextureAsync(mSourceMap, mHdrImageDownloadListener, getContext());
        mImageNeedsDownload = false;
    }

    @Override
    public void onTearDown() {
        if (mTargetedPortalScene != null) {
            mTargetedPortalScene.setLightingEnvironment(null);
            mTargetedPortalScene = null;
        }

        super.onTearDown();
        if (mHdrImageDownloadListener != null) {
            mHdrImageDownloadListener.invalidate();
            mHdrImageDownloadListener = null;
        }

        if (mLatestImage != null) {
            mLatestImage.destroy();
            mLatestImage = null;
        }

        if (mLatestTexture != null) {
            mLatestTexture.dispose();
            mLatestTexture = null;
        }
    }

    @Override
    public void setScene(VRTScene scene) {
        super.setScene(scene);
        mHasSetScene = true;
        this.onPropsSet();
    }
<commit_msg>VIRO-2961: Reload With Light Environment causes crash<commit_after>/**
 * Copyright © 2018 Viro Media. All rights reserved.
 */
package com.viromedia.bridge.component;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.viro.core.PortalScene;
import com.viro.core.Texture;
import com.viro.core.internal.Image;
import com.viromedia.bridge.component.node.VRTNode;
import com.viromedia.bridge.component.node.VRTScene;
import com.viromedia.bridge.utility.HdrImageDownloader;
import com.viromedia.bridge.utility.ViroEvents;

public class VRTLightingEnvironment extends VRTNode {
    private ReadableMap mSourceMap;
    private Image mLatestImage;
    private Texture mLatestTexture;
    private boolean mImageNeedsDownload;
    private IBLImageDownloadListener mHdrImageDownloadListener;
    private PortalScene mTargetedPortalScene = null;
    private boolean mHasSetScene;

    public VRTLightingEnvironment(ReactApplicationContext context) {
        super(context);
        mImageNeedsDownload = false;
        mHasSetScene = false;
    }

    public void setSource(ReadableMap source) {
        mSourceMap = source;
        mImageNeedsDownload = true;
    }

    @Override
    public void onPropsSet() {
        super.onPropsSet();
        if (!mImageNeedsDownload || mSourceMap == null || !mHasSetScene) {
            return;
        }

        if (mHdrImageDownloadListener != null){
            mHdrImageDownloadListener.invalidate();
        }

        imageDownloadDidStart();

        mHdrImageDownloadListener = new IBLImageDownloadListener();
        HdrImageDownloader.getHdrTextureAsync(mSourceMap, mHdrImageDownloadListener, getContext());
        mImageNeedsDownload = false;
    }

    @Override
    public void onTearDown() {
        if (mTargetedPortalScene != null && mTargetedPortalScene.getNativeRef() != 0L) {
            mTargetedPortalScene.setLightingEnvironment(null);
            mTargetedPortalScene = null;
        }

        super.onTearDown();
        if (mHdrImageDownloadListener != null) {
            mHdrImageDownloadListener.invalidate();
            mHdrImageDownloadListener = null;
        }

        if (mLatestImage != null) {
            mLatestImage.destroy();
            mLatestImage = null;
        }

        if (mLatestTexture != null) {
            mLatestTexture.dispose();
            mLatestTexture = null;
        }
    }

    @Override
    public void setScene(VRTScene scene) {
        super.setScene(scene);
        mHasSetScene = true;
        this.onPropsSet();
    }

{'fraction_non_alphanumeric': 0.0846611633710885, 'fraction_numerical': 0.00249568055288923, 'mean_word_length': 2.902621722846442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3877712', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1490, 'n_words': 280}