1

I'm moving a (heavy) file-parser to a webworker. Webworkers can only return serialized json. Hence, I cant return function that gives me object that inherit from a specific class:

getCollection: function getCollection(cls) {
  if (!cls) {
    return this.myObjects;
  }
  let collection = cls.toUpperCase();

  let arr = [];

  for (let i = 0; i < this.nrOfObjects; i++) {
    if(classes[collection]){
      if (this.myObjects[i] instanceof classes[collection]) {
        arr.push(this.myObjects[i]);
      }
    }
  }

  return arr;
}

So for example getCollection('product') would return all instances that inherits from the class product

When I move the parsers to the webworker, I can't return this function. Is there anyway I can add the inheritance to my constructor in my classes instead?

export class Column extends BuildingElment {
  constructor(GlobalId, OwnerHistory, Name, Description) {
    super(GlobalId, OwnerHistory, Name);
    this.description = Description
    this.nameOfClass = 'Column';
    this.inheritance = ['Root', 'Product', 'BuildingElement'] // Can this be somehow automatically generated??
  }
}

Or if there is any other way I can work with webworkers and still get the inheritance.

Joe
  • 4,274
  • 32
  • 95
  • 175
  • What distinguishes these classes other than their class name? Filter the instances by that. – Bergi Jan 31 '17 at 20:39
  • I don't have anything to filter on. I need quick way to get all instances that inherits from a class and from the webworker I can only get serialized data. – Joe Jan 31 '17 at 20:43
  • If they don't differ, why are they using different classes in the first place? – Bergi Jan 31 '17 at 20:47
  • Well of course they differ. Do you mean I should check the arguments? E.g getCollectionWithArgs('GlobalId', 'OwnerHistory', 'Name') ? Could do that but it's not pretty. I rather do getCollection('Product') – Joe Jan 31 '17 at 20:50
  • Yes, that's one possibility. If you like it more pretty, have a mapping from `'Product'` to `['GlobalId', 'OwnerHistory', 'Name']` and use that. It's all about duck typing :-) – Bergi Jan 31 '17 at 21:00
  • Hm. It's 750 classes. Quite time-consuming and error prone to do it manually. I was hopping for some neat built in function in javascript that returns the inherited classes. – Joe Jan 31 '17 at 21:38
  • 1
    Actually, [there is](http://stackoverflow.com/q/31644662/1048572), but class names shouldn't be trusted or thought of as unique in general. – Bergi Jan 31 '17 at 22:32

0 Answers0