3

I am working on a web application (vue built with webpack) in which two different web workers are running. One is handling incoming data the other one is handling outgoing data.

In both workers reading and writing operations need to get executed.

Since each worker runs on a separate thread calling them like this

  // in worker 1
  const dbObj1 = new Dexie('my-db');
  dbObj1.version(1).stores({...});

  // in worker 2
  const dbObj2 = new Dexie('my-db');
  dbObj1.version(1).stores({...});

is very likely to create race conditions.

Using a singleton database object wouldn't work either because of the separate threads.

Is there any way I can have access to the dexie database in both workers, without running into problems?

LongHike
  • 4,016
  • 4
  • 37
  • 76
  • First question, is it **necessary** or better yet, is it **essential** that you have two workers running in parallel while trying to interact & access the same data store? It's a good question nevertheless, I'm intrigued as to what others will say, I have my own opinions, but I'd need to know more about the specific problem before I could justify sharing my ideas. :) – JO3-W3B-D3V Feb 20 '19 at 15:58
  • 1
    @JO3-W3B-D3V It's not absolutely essential from a technical point of view. However if it was possible to separate the incoming and outgoing functionality at the worker level, i.e. not to couple them tightly, it would allow for recomposition with other workers, since there is some varation in what's needed. If push comes to shove I can work around that, but if I could have separate workers – in fact even more than two – that would make for the cleanest solution. – LongHike Feb 20 '19 at 16:18
  • 1
    Well, I'm not entirely sure about how you'd like to solve this problem, but **personally**, I'd go for a simplistic solution, thus using some wrapper in the main execution thread such as [this](https://github.com/JO3-W3B-D3V/MultiThread). PS I'm not suggesting that you use it at all, that's something I made for fun.. But back to the point, I'd just run thread `x` once thread `y` has reached a `'complete'` stage, which in theory would prevent you having to worry about race condition, but I'd imagine this wouldn't make for a suitable implementation? :) – JO3-W3B-D3V Feb 20 '19 at 16:51
  • 1
    I like that. I'll give it a shot. – LongHike Feb 20 '19 at 17:03
  • Awesome, I hope it solves your problems! :) – JO3-W3B-D3V Feb 20 '19 at 17:19

0 Answers0