I am attempting to do a singleton pattern in javascript. I am getting error message “Uncaught TypeError: route_socket.getInstance is not a function”. I was wondering what am I doing wrong. I can copy and paste the pattern from a training guide and it works. But when I am trying it here, it gives the error. So I know the pattern works.
var route_socket = require([
"esri/WebMap",
"esri/views/MapView",
"esri/layers/RouteLayer",
"esri/rest/support/Stop"
],
function(WebMap, MapView, RouteLayer, Stop) { //singleton design pattern
var instance;
class RouteSocket {#
apiKey;
constructor() {
this.#apiKey = token.getAttribute("token");
this.routeUrl = "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
this.churches = [];
this.stompClient = null;
this.view = null;
connect();
this.origin = new Stop({
geometry: {
x: OX,
y: OY
},
name: "Origin"
})
this.destination = new Stop({
geometry: {
x: DX,
y: DY
},
name: "Destination"
})
addRandomChurch();
addRandomChurch();
addRandomChurch();
}
addRandomChurch() {
var randomX = ((OX - DX) * Math.random()) + OX;
var randomY = ((OY - DY) * Math.random()) + OY;
var ChurchStop = { //Create a point
x: randomX,
y: randomY,
name: "Church"
};
this.churchAdded(ChurchStop);
}
setView(view) {
this.view = view;
console.log("We have set the view");
//check for churches already found and map them if they are there
this.churches.forEach(this.addRoute(church));
}
connect() {
var socket = new SockJS('/ws');
stompClient = Stomp.over(socket);
stompClient.connect({}, this.onConnected, this.onError);
}
async addRoute(church) {
let stops = [
this.origin,
church,
this.destination
];
let routeLayer = new RouteLayer({
stops
});
view.map.add(routeLayer);
await routeLayer.load();
const results = await routeLayer.solve({
apiKey
});
routeLayer.update(results);
// await view.goTo(routeLayer.routeInfo.geometry);
}
onConnected() {
stompClient.subscribe('/topic/map/' + hobnobID.getAttribute("hobnobID"), this.onMessageReceived);
synchroniseDataMapSocket.establishSocket(this);
}
onMessageReceived(payload) {
var message = JSON.parse(payload.body);
var ChurchStop = { //Create a point
x: message.X,
y: message.Y,
name: "Church"
};
this.churchAdded(ChurchStop);
}
churchAdded(ChurchStop) {
if (!this.view) {
churches.add(ChurchStop);
return;
} else {
this.addRoute(ChurchStop);
}
//else create stop on map
}
};
function createInstance() {
var object = new RouteSocket();
return object;
}
return {
getInstance: function() {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
});
route_socket.getInstance();
I am really grateful for any help I may receive. Thank you very much