npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unique-tabid-es5

v2.0.1

Published

This package helps you maintain Unique IDs per tab, takes care of differentiating duplicated tabs/sessions, and also new tabs vs old tabs. This works by using SessionStorage and BroadcastChannel Communication.

Downloads

14

Readme

unique-tabid

This package helps you maintain Unique IDs per tab by taking care of differentiating duplicated tabs/sessions as well. This package can also be leveraged to let you know if a newly loaded tab was duplicated or not, and also get the TabId of the parent tab (the tab from which this was duplicated)

What browser APIs we use

We make use of the following

  • SessionStorage is a Key-Value browser storage that is maintained per tab, per URL. But the only catch here is that when the tab is duplicated, the SessionStorage of the old tab is copied over to the new tab.
  • BroadcastChannel is an event/event-listener based communication system between tabs of the same URL.

How this works

As soon as a tab is loaded/refreshed, we first check if the SessionStorage contains a TabId.

  • If we do not have a TabID already, we create a fresh one, and store it in SessionStorage.
  • If we have a TabID already, it means that either we have refreshed the same tab (which means we should retain the TabID), or that we have loaded the duplicated tab, with the TabID of the older tab inherited (which means we have to generate a new TabID).

So whenever a page is loaded/refreshed, we look into SessionStorage and if it returns a valid TabID, we use BroadcastChannel to send a "SEARCH" message to all other tabs, and they would respond with a "FOUND" message if they have the same TabID; None of the tabs would respond if they do not have the TabID we are looking for.

The Catch

So in this second scenario where a new tab was loaded without duplication, we would not receive any response from the other tabs... So how long to wait till we assume that no duplication has occured...? For now we have kept this as a property that you can configure, with the default set at 1 second.

This means that when we use a debugger and pause execution at the right (or wrong) time, we could mess up this operation.

How to use

The default export is a class. Instantiate the class and set the appropriate properties.

npm install --save unique-tabid
// Import
import UniqueTabId from "unique-tabid";

// Instantiate the class and pass some string that identifies your app
const uniqueTabId = new UniqueTabId("testapp");

// Set properties

// Override the wait time for communication
uniqueTabId.WAIT_TIMEOUT = 2000;

// Mandatory: a function that is to be called to generate a unique TabID.
// In this example, I have used the famous uniqid package from NPM
uniqueTabId.uniqIdFunc = uniqid;

// This is the callback that is called when we have the tabId ready, after all the communications
uniqueTabId.tabIdCallback = ({ tabId, isNewTab, parentTabId }) => {};
// If used in React, you can call a State Change like shown in the functional React example below.
const [text, setText] = useState("");
uniqueTabId.tabIdCallback = ({ tabId, isNewTab, parentTabId }) =>
  setText(
    `TabId: ${tabId}; New Tab: ${isNewTab}; Duplicated: ${
      parentTabId !== null ? `Yes. ParentTabId = ${parentTabId}` : "No"
    }`
  );

// IMPORTANT: You need to absolutely call the following method at every tab initialization. This is the main process.
uniqueTabId.initTab();
// If used in React, you can call this in the componentDidMount or like in the following functional React equivalent.
useEffect(() => uniqueTabId.initTab(), []);

You can also have a look at the example React App at https://github.com/haricane8133/unique-tabid-client. This was created using create-react-app. Have a look at the changes from the second commit.

Thoughts

I couldn't take part in #hacktoberfest2022. So would this package make it up instead?

It would be awesome to have contributions coming in to remove 'The Catch' mentioned above.

About this fork

This fork just changes the Typescript build target to ES5 and adjusts the used language features for better compatibility with older codebases.