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 🙏

© 2026 – Pkg Stats / Ryan Hefner

adaptive-session-manager

v1.0.1

Published

A highly optimized, framework-agnostic session manager with a dynamic evaluation loop and lazy reset architecture.

Downloads

378

Readme

🛡️ Adaptive Session Manager

A highly optimized, framework-agnostic TypeScript engine for managing secure frontend sessions without sacrificing UI performance.

Unlike traditional session managers that rely on heavy polling or constant main-thread event tracking, this package utilizes a Dynamic Evaluation Loop and a unique Lazy Reset Architecture to save CPU cycles and prevent memory leaks.

Perfect for Angular, React, Vue, and Vanilla JS applications.


✨ Key Features

  • 🚀 Lazy Reset Architecture: Shuts down background polling during the final minutes of a session, switching to a single, lightweight event listener to save CPU overhead.
  • ⚡ Framework Agnostic: Pure TypeScript. Works seamlessly across any modern web framework.
  • 🧹 Memory Safe: Automatically cleans up all event listeners and timeouts when a session terminates.
  • ⏱️ Event-Driven Timeline: Adapts its evaluation checks based on how close the user is to the session expiration (Safe Zone -> Warning Zone -> Critical Zone).

📦 Installation

npm install adaptive-session-manager

🧠 How It Works (The Timeline)

The manager divides your session into dynamic zones. It checks activity less frequently when time is abundant, and more frequently as time runs out, culminating in the "Lazy Reset" critical zone.

0 min                  5 min                 10 min                13 min         15 min
 ├──────────────────────┼──────────────────────┼──────────────────────┼──────────────┤
 │      ZONE 1          │      ZONE 2          │      ZONE 3          │    ZONE 4    │
 │   (Safe Zone)        │   (Warning Zone)     │    (Urgent Zone)     │  (Critical)  │
 ├──────────────────────┼──────────────────────┼──────────────────────┼──────────────┤
 │ Check once at 5m     │ Check every 2.5m     │ Check every 1m       │ Lazy Reset   │
 │                      │                      │                      │ (On Activity)│

💻 Usage Examples

1. Angular Implementation

Pro-tip: Run the session tracking outside the Angular zone to prevent mousemove or scroll events from triggering constant UI re-rendering.

import { Injectable, NgZone } from '@angular/core';
import { AdaptiveSessionManager } from 'adaptive-session-manager';

@Injectable({ providedIn: 'root' })
export class SessionTrackingService {
  private manager: AdaptiveSessionManager;

  constructor(private ngZone: NgZone) {
    this.ngZone.runOutsideAngular(() => {
      this.manager = new AdaptiveSessionManager({
        sessionLengthMs: 15 * 60 * 1000, // 15 Minutes
        onExtendSession: () => this.pingBackendToExtendToken(),
        onLogout: () => this.handleSecureLogout()
      });
      
      this.manager.start();
    });
  }

  private pingBackendToExtendToken() {
    // Call your API to refresh the JWT
    console.log("Token extended!");
  }

  private handleSecureLogout() {
    this.ngZone.run(() => {
      // Clear localStorage, hit backend logout endpoint, and redirect
      console.log("User logged out securely.");
    });
  }
}

2. React Implementation

Use inside a useEffect at the root of your application (e.g., App.tsx or an AuthProvider).

import { useEffect } from 'react';
import { AdaptiveSessionManager } from 'adaptive-session-manager';

export function useSessionTracker() {
  useEffect(() => {
    const manager = new AdaptiveSessionManager({
      sessionLengthMs: 15 * 60 * 1000, // 15 Minutes
      onExtendSession: async () => {
        // e.g., await api.post('/refresh-token');
      },
      onLogout: () => {
        localStorage.clear();
        window.location.href = '/login';
      }
    });

    manager.start();

    // Cleanup on unmount
    return () => manager.stop();
  }, []);
}

3. Vanilla JavaScript

import { AdaptiveSessionManager } from 'adaptive-session-manager';

const sessionManager = new AdaptiveSessionManager({
  sessionLengthMs: 900000, // 15 minutes in ms
  activityEvents: ['click', 'scroll', 'keypress'], // Optional: override default events
  onExtendSession: () => {
    fetch('/api/extend-session', { method: 'POST' });
  },
  onLogout: () => {
    alert('Session expired due to inactivity.');
    window.location.href = '/logout';
  }
});

sessionManager.start();

⚙️ Configuration API

| Property | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | sessionLengthMs | number | Yes | undefined | Total length of the session in milliseconds. | | onExtendSession | function | Yes | undefined | Callback fired when the user interacts and the backend token needs extending. Can return a Promise. | | onLogout | function | Yes | undefined | Callback fired when time runs out completely. Put your frontend clearing logic here. | | activityEvents | string[] | No | ['click', 'keypress', 'scroll', 'touchstart'] | Array of DOM events that indicate user activity. |


🤝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

  1. Clone the repo
  2. Run npm install
  3. Run npm test to execute the Vitest suite

📄 License

MIT