0

Can I have some synchronisation mechanism (e.g. locks) in a Firefox extension's JavaScript code?

I added code according to https://developer.mozilla.org/en-US/docs/Code_snippets/On_page_load to make my extension do something each time a page is loaded, and I want to control the sequence of functions that are run inside my extension's JavaScript code.

Is there some way to achieve this using some synchronisation mechanism (e.g. locks) in a Firefox extension's JavaScript code?

Just as a wild guess, do I need to use web workers for this?

Tianyang Li
  • 1,605
  • 3
  • 26
  • 40

2 Answers2

1

You can use async library for your flow-control

Dmitry Matveev
  • 4,820
  • 26
  • 39
0

The way I'm doing it right now is to use Dekker's algorithm where the shared variables are put inside a JavaScript code module.

Right now I only need to synchronize 2 things, for synchronizing more things some solutions for software mutual exclusion can be used.

This kind of busy wait can make Firefox get stuck (maybe due to how Firefox handles processes and threads?), so I do not advise using anything like this.

A better solution would be to use events, as shown here on Mozilla's website.

Maybe there are other options. There are some interesting answers at What is the JavaScript version of sleep()?.

Community
  • 1
  • 1
Tianyang Li
  • 1,605
  • 3
  • 26
  • 40