Race conditions with modelService.open(), suggestions?


#1

I have an async openModel(id) function whose contents include:

console.log('opening', id);
let model = await this._domain.models().open(id);
// check if another version of this model has already been opened.
// if so, close this one and use that one.
if (this._openedModels.hasOwnProperty(id)) {
  console.log(id, 'was already opened. closing');
  await model.close();
  model = this._openedModels[id];
} else {
  console.log('opened model', model.modelId());
}

In my app, this function gets called in parallel 4 times. I was assuming that in this case, 4 separate model instances would get returned, so I should close the three duplicates. But it seems as if calling close() closes every instance of this model. Is that accurate?

opening 2989d71003c5bb92
opening 2989d71003c5bb92
opening 2989d71003c5bb92
opening 2989d71003c5bb92
opened model 2989d71003c5bb92
2989d71003c5bb92 was already opened. closing
2989d71003c5bb92 was already opened. closing
2989d71003c5bb92 was already opened. closing

And the last two close() calls end up throwing exceptions: The model has already been closed: 2989d71003c5bb92


#2

OK, poking around the API docs I see a modelService.isOpening() method, which I will start using. Ideally, though, there would be a ModelOpened event or something I could listen to. Does something like that exist? I didn’t see anything here https://docs.convergence.io/js-api/interfaces/imodelevent.html