Browser SDK - Unloading FaceTec SDK Resources (Single-Page Web Applications)
Designed for Single-Page Web Applications, the unload() API ensures that the Browser SDK's runtime elements are completely unloaded after the SDK has finished running. The unload() API may not be necessary for Multi-Page Web Applications, as these applications automatically unload resources during their page-to-page navigation.
Calling the unload() API
The unload() API is called by using FaceTec.unload(callbackFunction).
Some browser elements that this API unloads and removes are listed below.
- Styles added to the DOM.
- DOM elements added to the DOM.
- Worker(s) loaded by the FaceTec Browser SDK.
The Unload API can be used by calling FaceTecSDK.unload(callbackFunction). It should only be called when:
- The Browser SDK is fully loaded and not initializing.
- The Browser SDK is not performing a session.
FaceTecSDK.unload(callbackFunction) will only be invoked if the Browser SDK determines the operation is safe.
If the Browser SDK is busy, then FaceTecSDK.unload(callbackFunction)will not be started, and the callback function will not be executed.
Deallocating the FaceTec Browser SDK
To remove the Browser SDK from memory, first invoke the unload() API, and afterwards, set FaceTecSDK = undefined. This allows the Browser's garbage collector to remove the SDK's resources from memory. Once removed, the developer must manually reload and restart the initialization process to reuse the Browser SDK.
Example: How to use in SampleAppController
Unload the Browser SDK
FaceTecSDK.unload(() => {
SampleAppUtilities.displayStatus("SDK Unloaded - " +
FaceTecSDK.getFriendlyDescriptionForFaceTecSDKStatus(FaceTecSDK.getStatus()));
});
Reload the Browser SDK (after unloaded)
Config.initializeFromAutogeneratedConfig(FaceTecSDK, function(initializedSuccessfully) {
if(initializedSuccessfully) {
SampleAppUtilities.displayStatus("SDK Reloaded - " +
FaceTecSDK.getFriendlyDescriptionForFaceTecSDKStatus(FaceTecSDK.getStatus()));
}
else {
SampleAppUtilities.displayStatus("SDK Reload failed - " +
FaceTecSDK.getFriendlyDescriptionForFaceTecSDKStatus(FaceTecSDK.getStatus()));
}
});Completely remove the Browser SDK
FaceTecSDK.unload(() => {
// This portion depends on how the SDK was loaded.
// Regardless, set the local variable to undefined.
FaceTecSDK = undefined;
// The below assumes the SDK was loaded as a part of the window object.
delete window.FaceTecSDK;
SampleAppUtilities.displayStatus("Browser SDK unloaded");
});Manually reload the Browser SDK (one of the possible methods)
const script = document.createElement("script");
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
script.onload = () => {
SampleAppUtilities.displayStatus("Browser SDK Loaded but not initialized");
// Reinitialize the SDK here
};
script.src = "../../core-sdk/FaceTecSDK.js/FaceTecSDK.js";