I have this strange problem I'm trying to pin down for few days. From time to time I'm getting error that cannot call findInRange on undefined
(or something, saying that this.target.pos
is undefined in StorageWrapper class). This can happen every few minutes or sometimes more rarely.
Below is extracted code - in each tick I iterate over all rooms, and create RoomManager for reach room. RoomManager takes storage and passes it into StorageWrapper. StorageWrapper calls findInRange on StructureStorage.pos and this fails.
// In main.js
let managers = rooms.getHandlers(jobBoard);
// in rooms.js
module.exports = {
getHandlers(jobBoard) {
let managers = [];
_.each(Game.rooms, room => {
// ....
managers.push(new roomTypes.regular(room, jobBoard));
// in roomTypes.regular.js
class RoomManager extends utils.Executable {
constructor(room, jobManager) {
super();
this.room = room;
// ...
if(this.room.storage) {
this.storage = new wrappers.StorageWrapper(this, this.room.storage, this.links);
// in wrappers.js
class StorageWrapper extends utils.Executable {
constructor(manager, storage, links) {
super();
this.manager = manager;
this.target = storage;
this.link = _.first(this.target.pos.findInRange(links, 3));
What is even more strange: I've added small guard:
if(!this.target.pos) {console.log('WHY NO POS?', this.target, '::', this.target.pos);}
and this guard from time to time logs to console following:
WHY NO POS ? [structure (storage) #someid] :: [room WaaNbb pos 23,33]
So pos
evaluates sometimes to falsy, yet beeing printed it is not falsy and is valid object. And in such cases, besides pos
beeing falsy, calling findInRange
does not throw errors.
Please advise what I'm doing wrong because I dont know how I should guard all my code agains such cases.