@aakashsuryawanshi/ng-idle 中文文档教程

发布于 3年前 浏览 25 项目主页 更新于 3年前

Introduction

构建状态Coverage Status

用于响应 Angular 应用程序中空闲用户的模块。 这是 ng-idle 模块的重写; 但是,如果您使用的是 Angular 1,则必须使用该模块。

MAINTAINERS WANTED

Angular 社区需要你! 我正在寻找新的开发人员或团队来接管此模块的维护工作。 这些是任何有兴趣的候选人都应该考虑的责任:

  • Now: Complete beta process (major remaining item is to make it compatible with SSR)
  • Now: Update demo and add API documentation
  • Ongoing: Bug fixes
  • Ongoing: New releases for new versions of Angular
  • Later: Refactor to simplify API and reduce package size
  • Later: Add support for non-browser environments?

理想情况下,候选人:

  • Has experience building applications in Angular 5+
  • Is an active Angular developer and tuned into the Angular release schedule
  • Loves open source and the Angular community
  • Is committed to releasing modular and lightweight (as possible) packages
  • Has working understanding of DOM events, JavaScript timers and intervals, Web Storage API, and cookies
  • Understands testing using Karma and Jasmine, and is committed to a high percentage of code coverage
  • Has working understanding of the contributing guide, is willing to accept contributions from others, and can use Github and related tools effectively
  • Has time to triage and answer tickets, or delegate to others
  • Has basic understanding of NPM for releasing packages

如果您有兴趣,请联系我们!

License

作者为 Mike Grabski @moribvndvs

有关许可详细信息,请参阅LICENSE

Demo

访问 https://moribvndvs.github.io/ng2-idle 查看带有快速入门说明的简单示例。

Quick start

@ng-idle 通过 npm 发布。 对于最新支持的 Angular 版本,您可以使用以下命令安装该包:

npm install --save @ng-idle/core

将包集成和配置到您的应用程序中还需要几个步骤。 请访问 @ng-idle-example 获取源代码和如何开始的说明。

Design Considerations

该模块的主要应用是检测用户何时空闲。 它还可以用于警告用户即将发生超时,然后让他们超时。 此模块的核心是 Idle 服务,它会根据您的配置尽最大努力检测用户何时处于活动或空闲状态,并将该信息传递给您的应用程序,以便它可以做出适当的响应。

Modularization

可以通过 npm@ng-idle/core 包中找到核心功能。

用于扩展功能的附加模块:

  • @ng-idle/keepalive (see below)

Extensible Keepalive Integration

在用于会话管理的常见用例中,您可能需要定期向服务器发出用户仍处于登录状态和活动状态的信号。 如果您需要该功能,@ng-idle 可以可选地@ng-idle/keepalive 集成。 @ng-idle 将指示 @ng-idle/keepalive 在用户处于活动状态时执行 ping,并在用户空闲或超时时停止。 当用户恢复活动或重置空闲状态时,它会立即 ping 然后恢复 ping。 请注意,keepalive 集成是可选的,您必须单独安装和配置@ng-idle/keepalive 才能获得此功能。 您可以通过扩展 KeepaliveSvc 并在您的应用程序中将其配置为 KeepaliveSvc 类的提供者来实现您自己的。

Extensible Interrupts

中断是任何输入源(通常来自用户,但也可能是其他选项卡或事件之类的东西),可用于向 Idle 发出信号,表明应该中断或重置空闲手表。 与 ng-idle 不同,这些源不是硬编码的; 您可以扩展 InterruptSource 或任何内置源以满足您的目的。 此功能对于处理可能困扰您的特定用例的输入噪音也很有用。 它还可以用于定位页面上的特定元素而不是整个文档或窗口。 此包中内置了以下源:

  • InterruptSource (abstract): A base type you can implement to make your own source.
  • EventTargetInterruptSource: Any object that implements EventTarget, such as an HTMLElement or Window. Takes in the object that is the source and a space delimited string containing the events that cause an interrupt.
  • DocumentInterruptSource: Looks for events (in a space delimited string) that bubble up to the document.documentElement (html node).
  • WindowInterruptSource: Looks for events (in a space delimited string) that bubble up to the Window.
  • StorageInterruptSource: Looks only for the Storage event of Window object. Obligatory for LocalStorageExpiry.

注意:您必须在初始化应用程序时自行配置源。 默认情况下,没有配置中断。 您可以通过导入 DEFAULT_INTERRUPTSOURCES 并将该引用传递给 Idle.setInterrupts(DEFAULT_INTERRUPTSOURCES); 来使用类似于 ng-idle 默认值的配置。

Extensible Expiry

ng-idle 移植的另一个特性是能够在一些存储中存储到期值,运行相同应用程序的多个选项卡或窗口可以写入这些存储。 通常,这个存储是 localStorage,但也可以是 cookie 或任何你想要的。 这个过期和过期存储的目的是双重的:首先,如果窗口休眠或暂停时间超过配置的超时时间,则防止窗口不超时。 其次,它可用于使一个选项卡或窗口中的活动防止同一应用程序中的其他选项卡或窗口超时。

默认情况下,提供了一个 LocalStorageExpiry 类型,它只会跟踪 localStorage 中的过期时间。 它将实现上述所有目的。 如果您不想支持多个选项卡或窗口,您可以使用 SimpleExpiry。 换句话说,SimpleExpiry 不协调选项卡或窗口之间的最后一个活动。 如果你想将到期值存储在另一个存储中,比如 cookie,你需要使用或创建一个支持它的实现。 您可以通过扩展 IdleExpirySimpleExpiry 并将其配置为 IdleExpiry 类的提供者来创建您自己的。

Multiple Idle Instance Support

Angular 中的依赖注入器支持分层注入策略。 这允许您在您需要的任何范围内创建一个 Idle 实例,并且可以有多个实例。 这允许你们两个有两个单独的手表,例如,在页面上的两个不同元素上。
如果您使用默认过期时间 (LocalStorageExpiry),则需要使用 Idle.setIdleName('yourIdleName') 为每个空闲定义一个名称,否则相同的密钥将在 localStorage 中使用,此功能将无法按预期工作。

Example Use Case

例如,考虑一个电子邮件应用程序。 为了提高安全性,应用程序可能希望确定用户何时处于非活动状态并将他们注销,如果他们仍在计算机前并且只是分心,则让他们有机会延长他们的会话。 此外,为了更好的安全性,服务器可能会向用户的会话颁发一个安全令牌,该令牌会在 5 分钟不活动后过期。 用户可能需要比这更多的时间来输入他们的电子邮件并发送它。 如果您在积极使用该软件时发现您已注销,那将是令人沮丧的!

@ng-idle/core 可以检测到用户正在点击、打字、触摸、滚动等,并知道用户仍然处于活动状态。 它可以与 @ng-idle/keepalive 一起工作,每隔几分钟 ping 服务器以保持登录状态。在这种情况下,只要用户正在做某事,他们就会保持登录状态。如果他们离开电脑一步,我们可以呈现一个警告对话框,然后在倒计时后,将它们注销。

Developing

该项目是使用 .nvmrc 文件中的 NodeJS 版本开发的。 使用旧版本可能会遇到问题。 尝试使用 NVM 或类似工具来同时管理不同版本的 Node。 如果使用 NVM,您可以执行 nvm install 下载并切换到正确的版本。

克隆存储库后,使用 npm 安装所有包:

npm install

您现在可以一次构建并运行所有测试并覆盖。

 npm test

如果您有 npm run ng testng test ,您还可以在对项目进行更改时连续运行测试code>@angular/cli 全局安装。

npm run ng test core
...
npm run ng test keepalive

注意:Keepalive 依赖于 Core。 如果您正在运行上述连续测试,则需要先npm buildnpm run ng build core,然后再对 Core 进行更改。 但是,npm test 将一次性构建所有模块并运行测试。

Contributing

请参阅贡献指南

Introduction

Build StatusCoverage Status

A module for responding to idle users in Angular applications. This is a rewrite of the ng-idle module; however if you are using Angular 1, you must use that module.

MAINTAINERS WANTED

The Angular community needs you! I'm looking for a new developer or team to take over maintenance of this module. These are the responsibilities any interested candidates should consider:

  • Now: Complete beta process (major remaining item is to make it compatible with SSR)
  • Now: Update demo and add API documentation
  • Ongoing: Bug fixes
  • Ongoing: New releases for new versions of Angular
  • Later: Refactor to simplify API and reduce package size
  • Later: Add support for non-browser environments?

Ideally, a candidate:

  • Has experience building applications in Angular 5+
  • Is an active Angular developer and tuned into the Angular release schedule
  • Loves open source and the Angular community
  • Is committed to releasing modular and lightweight (as possible) packages
  • Has working understanding of DOM events, JavaScript timers and intervals, Web Storage API, and cookies
  • Understands testing using Karma and Jasmine, and is committed to a high percentage of code coverage
  • Has working understanding of the contributing guide, is willing to accept contributions from others, and can use Github and related tools effectively
  • Has time to triage and answer tickets, or delegate to others
  • Has basic understanding of NPM for releasing packages

Please get in touch if you are interested!

License

Authored by Mike Grabski @moribvndvs

See LICENSE for licensing details.

Demo

Visit https://moribvndvs.github.io/ng2-idle to view a simple example with quick start instructions.

Quick start

@ng-idle is shipped via npm. You can install the package using the following command for the latest supported version of Angular:

npm install --save @ng-idle/core

Integrating and configuring the package into your application requires a few more steps. Please visit @ng-idle-example for source and instructions on how to get going.

Design Considerations

The primary application of this module is to detect when users are idle. It can also be used to warn users of an impending timeout, and then time them out. The core of this module is the Idle service which does its best - based on your configuration - to detect when a user is active or idle and pass that information on to your application so it can respond appropriately.

Modularization

The core functionality can be found in the @ng-idle/core package via npm.

Additional modules to extend functionality:

  • @ng-idle/keepalive (see below)

Extensible Keepalive Integration

In a common use case where it is used for session management, you may need to signal to the server periodically that the user is still logged in and active. If you need that functionality, @ng-idle can optionally integrate with @ng-idle/keepalive. @ng-idle will instruct @ng-idle/keepalive to ping while the user is active, and stop once they go idle or time out. When the user resumes activity or the idle state is reset, it will ping immediately and then resume pinging. Please note that keepalive integration is optional, and you must install and configure @ng-idle/keepalive separately to get this functionality. You can implement your own by extending KeepaliveSvc and configuring it as a provider in your application for the KeepaliveSvc class.

Extensible Interrupts

An interrupt is any source of input (typically from the user, but could be things like other tabs or an event) that can be used to signal to Idle that the idle watch should be interrupted or reset. Unlike ng-idle, these sources are not hardcoded; you can extend InterruptSource or any of the built-in sources to suit your purposes. This feature is also useful to handle input noise that may plague your particular use case. It can also be used to target specific elements on a page rather than the whole document or window. The following sources come built into this package:

  • InterruptSource (abstract): A base type you can implement to make your own source.
  • EventTargetInterruptSource: Any object that implements EventTarget, such as an HTMLElement or Window. Takes in the object that is the source and a space delimited string containing the events that cause an interrupt.
  • DocumentInterruptSource: Looks for events (in a space delimited string) that bubble up to the document.documentElement (html node).
  • WindowInterruptSource: Looks for events (in a space delimited string) that bubble up to the Window.
  • StorageInterruptSource: Looks only for the Storage event of Window object. Obligatory for LocalStorageExpiry.

NOTE: You must configure source(s) yourself when you initialize the application. By default, no interrupts are configured. You can use a configuration analogous to the ng-idle default by importing DEFAULT_INTERRUPTSOURCES and passing that reference to Idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);.

Extensible Expiry

Another feature ported from ng-idle is the ability to store an expiry value in some store where multiple tabs or windows running the same application can write to. Commonly, this store is the localStorage, but could be cookies or whatever you want. The purpose of this expiry and the expiry store is twofold: First, to prevent a window from not timing out if it sleeps or pauses longer than the configured timeout period. Second, it can be used so that activity in one tab or window prevents other tabs or windows in the same application from timing out.

By default, a LocalStorageExpiry type is provided, which will just keep track of the expiry in the localStorage. It will fulfill all purposes mentioned above. If you don't want to support multiple tabs or windows, you can use SimpleExpiry. In other words, SimpleExpiry does not coordinate last activity between tabs or windows. If you want to store the expiry value in another store, like cookies, you'll need to use or create an implementation that supports that. You can create your own by extending IdleExpiry or SimpleExpiry and configuring it as a provider for the IdleExpiry class.

Multiple Idle Instance Support

The dependency injector in Angular supports a hierarchical injection strategy. This allows you to create an instance of Idle at whatever scope you need, and there can be more than one instance. This allows you two have two separate watches, for example, on two different elements on the page.
If you use the default expiry (LocalStorageExpiry), you will need to define a name for each idle with Idle.setIdleName('yourIdleName'), otherwise the same key will be used in the localStorage and this feature will not work as expected.

Example Use Case

For example, consider an email application. For increased security, the application may wish to determine when the user is inactive and log them out, giving them a chance to extend their session if they are still at the computer and just got distracted. Additionally, for even better security the server may issue the user's session a security token that expires after 5 minutes of inactivity. The user may take much more time than that to type out their email and send it. It would be frustrating to find you are logged out when you were actively using the software!

@ng-idle/core can detect that the user is clicking, typing, touching, scrolling, etc. and know that the user is still active. It can work with @ng-idle/keepalive to ping the server every few minutes to keep them logged in. In this case, as long as the user is doing something, they stay logged in. If they step away from the computer, we can present a warning dialog, and then after a countdown, log them out.

Developing

This project was developed using the NodeJS version found in the .nvmrc file. You may experience problems using older versions. Try NVM or similar to manage different versions of Node concurrently. If using NVM, you can execute nvm install to download and switch to the correct version.

Once you have cloned the repository, install all packages using npm:

npm install

You can now build and run all tests once with coverage.

 npm test

You can also continuously run tests while you make changes to a project by executing npm run ng test <project name> or ng test <project name> if you have @angular/cli installed globally.

npm run ng test core
...
npm run ng test keepalive

Note: Keepalive depends on Core. If you are running the above continuous tests, you'll need to npm build or npm run ng build core first and after making changes to Core. However, npm test will build all modules and run the tests in one shot.

Contributing

See the contributing guide.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文