
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
ember-master-tab
Advanced tools
A library that provides a service which helps running a function on only one tab of an Ember application.
This addon provides a service
that allows you to run code on a single tab of your ember
application. You might find this useful if your app has functionality that for some reason
does not make sense or it is wasteful/redundant to have it run on every tab that is currently
open. For example, you might be continuously pulling some state from your server API that
you are saving on localStorage
which you then use to update your UI through event listeners.
npm install ember-master-tab
You can clone this repository and have a look at the dummy app to see it in action.
run(func1, options = {}).else(func2)
func1
: If this is the master tab, run this function.options
(optional):force
(optional, default: false
): If true
, run func1
irregardless of this being the master tab or not.func2
: If this is not the master tab, run this instead.// services/server-time-run.js
import Ember from 'ember';
export default Ember.Service.extend({
masterTab: Ember.inject.service(),
currentTime: null,
init() {
this._super(...arguments);
window.addEventListener('storage', e => { // only slave tabs will receive this event
if (e.key === 'current-time-run') {
this.set('currentTime', e.newValue);
}
});
this._updateTime();
},
_updateTime() {
Ember.run.later(() => {
this.updateTime();
this._updateTime();
}, 900);
},
updateTime(force = false) {
this.get('masterTab')
.run(() => {
Ember.$.getJSON('/api/current-time').then(data => { // will only run on the master tab
const currentTime = data.currentTime;
this.set('currentTime', currentTime);
localStorage['current-time-run'] = currentTime;
});
}, { force })
.else(() => {
// Master tab is handling it.
});
}
});
Notes:
else()
is optional.run()
takes a second optional boolean
parameter. If true
it will
make the function run irregardless of this being the master tab or not
for that call on that tab and the function passed to else()
will not
run. Considering the previous example, this would be useful if a
controller calls this.get('serverTimeRun').updateTime(true)
directly
on any tab.lock(lockName, func1, options = {}).wait(func2)
lockName
: Name of the lock.func1
: Function which returns a Promise
that will run only if this is the master tab.
Once the promise resolve
s or reject
s, the lock will be freed.options
(optional):force
(optional, default: false
): If true
, run func1
irregardless of this being the master tab or not.waitNext
(optional, default: true
): If true
and there is currently no lock present, wait a maximum of waitNextDelay
until the lock has been obtained and released.waitNextDelay
(optional, default: 1000): If waitNext
is true
, wait this amount of milliseconds.func2
: If this is not the master tab, run this instead once the lock has been freed.// services/server-time-lock.js
import Ember from 'ember';
export default Ember.Service.extend({
masterTab: Ember.inject.service(),
currentTime: null,
init() {
this._super(...arguments);
this._updateTime();
},
_updateTime() {
Ember.run.later(() => {
this.updateTime();
this._updateTime();
}, 900);
},
updateTime(force = false) {
this.get('masterTab')
.lock('server-time', () => {
return Ember.$.getJSON('/api/current-time').then(data => { // will only run on the master tab
const currentTime = data.currentTime;
this.set('currentTime', currentTime);
return currentTime; // will be passed to slave tabs
});
}, { force })
.wait(currentTime => { // will only run on slave tabs; currentTime is the result from the master tab
this.set('currentTime', currentTime);
});
}
});
Notes:
wait()
is optional. It can take a second callback which runs if the
promise failed.wait()
will execute once that promise
resolves/rejects. Otherwise, they will run immediately. These callbacks
only run on "slave" tabs, generally.wait()
callbacks will be the last value returned
by the lock()
promise.options.force
is true
it will
make the function run irregardless of this being the master tab or not for
that call on that tab. It sets a lock and the callbacks passed to wait()
will not run. If the master tab encounters a lock during this, it will instead
run the wait()
callbacks. Considering the previous example, this would
be useful if a controller calls this.get('serverTimeLock').updateTime(true)
directly on any tab.localStorage
whatever the promise returns.
This value will be passed to the appropriate callback given to wait()
.
Note that localStorage
only stores strings. So make sure whatever
your promise returns can easily be converted to something usable in
your wait()
callbacks.isMasterTab
event
Whenever a tab is promoted to master status, the masterTab
service will emit an isMasterTab
event.
So, following the theme of the previous examples, you could also work with EventSource
objects
(or WebSocket
, etc.) like this:
// services/server-time-sse.js
import Ember from 'ember';
export default Ember.Service.extend({
masterTab: Ember.inject.service(),
currentTime: null,
init() {
this._super(...arguments);
if (this.get('masterTab.isMasterTab')) {
this.setup();
}
this.get('masterTab').on('isMasterTab', isMaster => {
if (isMaster) {
this.setup();
}
});
window.addEventListener('storage', e => {
if (e.key === 'current-time-sse') {
this.set('currentTime', e.newValue);
}
});
},
setup() {
const sse = new EventSource('/sse');
sse.onmessage = e => {
this.set('currentTime', e.data);
window.localStorage['current-time-sse'] = e.data;
};
this.get('masterTab').on('isMasterTab', isMaster => {
if (!isMaster) {
sse.close();
}
});
}
});
Notes:
Ember Master Tab is released under the MIT Licencse.
FAQs
A library that provides a service which helps running a function on only one tab of an Ember application.
The npm package ember-master-tab receives a total of 55 weekly downloads. As such, ember-master-tab popularity was classified as not popular.
We found that ember-master-tab demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.