Skip to content

Commit

Permalink
Feature/web lock leader election (#1137)
Browse files Browse the repository at this point in the history
* ADD use web lock api if possible

* FIX cleanup

* FIX stuff

* FIx

* FIX export

* FIX stuff

* FIX ci

* FIX homepage

* FIX

* FIx

* REFACTOR hasLeader()

* FIX tests
  • Loading branch information
pubkey authored Mar 22, 2023
1 parent aa40046 commit 063a2cb
Show file tree
Hide file tree
Showing 21 changed files with 2,426 additions and 1,971 deletions.
10 changes: 7 additions & 3 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ Depending in which environment this is used, a proper method is automatically se
This module also comes with a leader-election which can be used to elect a leader between different BroadcastChannels.
For example if you have a stable connection from the frontend to your server, you can use the LeaderElection to save server-side performance by only connecting once, even if the user has opened your website in multiple tabs.

In the background it will use the [Web Locks API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API) if possible and fall back to a message-based election algorithm if WebLocks is not available.


In this example the leader is marked with the crown ♛:
![leader-election.gif](../docs/files/leader-election.gif)

Expand All @@ -223,14 +226,15 @@ import { createLeaderElection } from 'broadcast-channel';
const elector = createLeaderElection(channel);
elector.awaitLeadership().then(()=> {
console.log('this tab is now leader');
})
});
```

Check if there is a leader at this point in time. `hasLeader` is true when there is a leader. It becomes false, if the leader is dead. Then it becomes true again when a new leader is elected.
Check if there is a leader at this point in time. `hasLeader()` returns true when there is a leader. It returns false, if the leader is dead. Then it returns true again when a new leader is elected.

```js
const elector = createLeaderElection(channel);
console.log('leader exists: ' + elector.hasLeader);
const hasLeader = await elector.hasLeader();
console.log('leader exists: ' + hasLeader);
```

If more than one tab is becoming leader adjust `LeaderElectionOptions` configuration.
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
# This workflow contains a single job called "build"
base:
# The type of runner that the job will run on
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down Expand Up @@ -86,10 +86,10 @@ jobs:

# run the node test in an own task, so we can use a node-version matrix.
test-node:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
matrix:
node: ['16.13.1', '17.3.0', '18.12.1']
node: ['17.3.0', '18.12.1']
steps:
- uses: actions/checkout@v3
- name: Setup Node.js environment
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/prevent-commit-to-generated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ on:
pull_request:
paths:
- 'dist/**'
- 'docs/**'

jobs:
warn-user:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
- name: Warn User to not commit generated files
run: bash -c 'echo "You have commited generated files (from the dist or docs folder), this is not allowed. You can only commit files that you have manually edited" && exit 1'
run: bash -c 'echo "You have commited generated files (from the dist folder), this is not allowed. You can only commit files that you have manually edited" && exit 1'
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

## X.X.X (comming soon)

- Use [Web Locks API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API) for leader election if possible.
- `LeaderElector.hasLeader` is now a function that returns a `Promise<boolean>`.

## 4.20.1 (6 January 2023)

- FIX exports order
Expand Down
17 changes: 4 additions & 13 deletions config/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ const configuration = {
console.log('availableBrowser:');
console.dir(availableBrowser);
const browsers = availableBrowser
.filter(b => !['PhantomJS', 'FirefoxAurora', 'FirefoxNightly'].includes(b))
.map(b => {
if (process.env.TRAVIS && b === 'Chrome') return 'Chrome_travis_ci';
else return b;
});
.filter(b => !['PhantomJS', 'FirefoxAurora', 'FirefoxNightly'].includes(b));
return browsers;
}
},
Expand Down Expand Up @@ -58,10 +54,9 @@ const configuration = {
envPreprocessor: [
'GITHUB_ACTIONS',
],

client: {
mocha: {
bail: false,
bail: true,
timeout: 12000
},
captureConsole: true
Expand All @@ -75,14 +70,10 @@ const configuration = {
flags: ['--no-sandbox']
}
},
singleRun: true
singleRun: true,
concurrency: 1
};

if (process.env.TRAVIS) {
configuration.browsers = ['Chrome_travis_ci'];
configuration.concurrency = 1;
}

module.exports = function (config) {
config.set(configuration);
};
Loading

0 comments on commit 063a2cb

Please sign in to comment.