first off to give some context of my situation...I've got a library containing a few models etc for my application, then an electron application which adds some UI, the electron app also loads a custom JS file which allows for adding additional logic.
On my Workspace model I'm using the singleton pattern (I know, I know) to store an instance of the workspace. When the app starts up, it's loading the workspace, then calls document.head.appendChild to load my custom script.
In the custom script, I'm requiring that same model library to get my Workspace class. However, Workspace.instance is returning null despite it definitely being not null before.
By contrast, if before loading the script I do global.workspace = workspace, then the script can access the instance fine. So can anyone tell me what's going on? As far as I understood, static properties and methods have essentially global scope, though this seems to suggest otherwise.
In my library:
export default class Workspace {
static instance: Workspace = null;
startupData: any;
constructor(startupData: any) {
this.startupData = startupData;
Workspace.instance = this;
}
}
In my app:
import Workspace from 'mymodels';
const workspace = new Workspace(startupData);
global.workspace = workspace;
console.log(Workspace.instance); //Returns my workspace instance
const script = document.createElement('script');
script.onload = showUI;
script.src = 'file:///' + workspace.startupData.logic;
document.head.appendChild(script);
In my custom script:
const mymodels = require('mymodels');
console.log(mymodels.Workspace.instance); //Returns null
console.log(global.workspace); //Returns my workspace instance