Skip to content

Commit

Permalink
Merge branch 'AHelper-fix-69-68'
Browse files Browse the repository at this point in the history
  • Loading branch information
moribvndvs committed Dec 17, 2017
2 parents 4118d49 + 8da9e5b commit 602c6b2
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 107 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Introduction
[![Join the chat at https://gitter.im/HackedByChinese/ng2-idle](https://badges.gitter.im/HackedByChinese/ng2-idle.svg)](https://gitter.im/HackedByChinese/ng2-idle?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/HackedByChinese/ng2-idle.svg?branch=master)](https://travis-ci.org/HackedByChinese/ng2-idle) [![Coverage Status](https://coveralls.io/repos/github/HackedByChinese/ng2-idle/badge.svg?branch=master)](https://coveralls.io/github/HackedByChinese/ng2-idle?branch=master)

A module for responding to idle users in Angular 2+ applications. This is a rewrite of the [ng-idle module](https://github.com/HackedByChinese/ng-idle); however if you are using Angular 1, you must use that module.
A module for responding to idle users in Angular 4+ applications. This is a rewrite of the [ng-idle module](https://github.com/HackedByChinese/ng-idle); however if you are using Angular 1, you must use that module.

## License
Authored by **Mike Grabski** @HackedByChinese me@mikegrabski.com
Expand Down Expand Up @@ -51,7 +51,7 @@ Another feature ported from `ng-idle` is the ability to store an expiry value in
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 2+ 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.
The dependency injector in Angular 4+ 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
Expand Down
2 changes: 1 addition & 1 deletion config/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ module.exports = function (options) {
*/
new ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
/angular(\\|\/)core(\\|\/)(@angular|esm5)/,
root('modules') // location of your src
),

Expand Down
3 changes: 2 additions & 1 deletion modules/core/src/idle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ export class Idle implements OnDestroy {
}

private safeClearInterval(handleName: string): void {
if (this[handleName]) {
const handle = this[handleName];
if (handle !== null && typeof handle !== 'undefined') {
clearInterval(this[handleName]);
this[handleName] = null;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/keepalive/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
moduleName: 'ngidle.keepalive',
globals: {
'@angular/core': 'ng.core',
'@angular/http': 'ng.http',
'@angular/common/http': 'ng.http',
'rxjs/Rx': 'Rx',
'@ng-idle/core': 'ng.core'
},
Expand Down
55 changes: 25 additions & 30 deletions modules/keepalive/src/keepalive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// clang-format off
import {async, fakeAsync, inject, TestBed, tick} from '@angular/core/testing';
import {BaseRequestOptions, Http, HttpModule, Request, RequestMethod, Response, ResponseOptions} from '@angular/http';
import {MockBackend} from '@angular/http/testing';
import {HttpRequest, HttpResponse, HttpClient, HttpClientModule} from '@angular/common/http';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';

import {Keepalive} from './keepalive';

describe('keepalive/Keepalive', () => {

beforeEach(
() => {
TestBed.configureTestingModule({imports: [HttpModule],
providers: [BaseRequestOptions, MockBackend, Keepalive, {deps: [MockBackend, BaseRequestOptions],
provide: Http,
useFactory:
(backend, defaultOptions) => { return new Http(backend, defaultOptions); }
}]});
TestBed.configureTestingModule({imports: [HttpClientTestingModule, HttpClientModule],
providers: [Keepalive]});
});

let instance: Keepalive;
let httpMock: HttpTestingController;

beforeEach(inject([Keepalive], keepalive => { instance = keepalive; }));
beforeEach(inject([Keepalive, HttpTestingController], (keepalive, httpTestingController) => {
instance = keepalive;
httpMock = httpTestingController;
}));

describe('runtime config', () => {
it('request() should set and return a Request', () => {
let expected = new Request({method: RequestMethod.Get, url: 'http://test.com'});
let expected = new HttpRequest('GET', 'http://test.com');

let actual = instance.request(expected);

Expand All @@ -34,7 +34,7 @@ describe('keepalive/Keepalive', () => {
let expected = 'http://test.com/2';
let actual = instance.request(expected);

expect(actual.method).toEqual(RequestMethod.Get);
expect(actual.method).toEqual('GET');
expect(actual.url).toEqual(actual.url);
});

Expand Down Expand Up @@ -92,34 +92,29 @@ describe('keepalive/Keepalive', () => {

describe('using an HTTP request', () => {

let request = new Request({method: RequestMethod.Get, url: 'https://test.com'});
let backend: MockBackend;
let request = new HttpRequest('GET', 'https://test.com');

beforeEach(inject([Keepalive, MockBackend], (keepalive, mockBackend) => {
backend = mockBackend;
instance = keepalive;
beforeEach(() => {
instance = TestBed.get(Keepalive);
instance.request(request);
}));
});

afterEach(() => {
// TODO: should be throwing if no expected requests are made
backend.resolveAllConnections();
backend.verifyNoPendingRequests();
httpMock.verify();
});

it('ping() should fire request and emit onPingResponse event', async(() => {
backend.connections.subscribe(connection => {
expect(connection.request.url).toBe(request.url);

connection.mockRespond(new ResponseOptions({status: 200}));
});
it('ping() should fire request and emit onPingResponse event', () => {
let actualResponse: HttpResponse<{}>;

instance.onPingResponse.subscribe((response: Response) => {
expect(response.status).toBe(200);
});
instance.onPingResponse.subscribe((response: HttpResponse<{}>) => {
actualResponse = response;
});

instance.ping();
}));

httpMock.expectOne(request.url).flush(null, {status: 200, statusText: 'OK'});
expect(actualResponse.status).toBe(200);
});
});

describe('on an interval', () => {
Expand Down
24 changes: 14 additions & 10 deletions modules/keepalive/src/keepalive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {EventEmitter, Injectable, OnDestroy} from '@angular/core';
import {Http, Request, RequestMethod, Response} from '@angular/http';
import {HttpClient, HttpRequest, HttpResponse} from '@angular/common/http';
import {KeepaliveSvc} from '@ng-idle/core';


Expand All @@ -8,7 +8,7 @@ import {KeepaliveSvc} from '@ng-idle/core';
*/
@Injectable()
export class Keepalive extends KeepaliveSvc implements OnDestroy {
private pingRequest: Request;
private pingRequest: HttpRequest<any>;
private pingInterval: number = 10 * 60;
private pingHandle: any;

Expand All @@ -20,13 +20,13 @@ export class Keepalive extends KeepaliveSvc implements OnDestroy {
/*
* An event emitted when the service has pinged an HTTP endpoint and received a response.
*/
public onPingResponse: EventEmitter<Response> = new EventEmitter<Response>();
public onPingResponse: EventEmitter<HttpResponse<any>> = new EventEmitter<HttpResponse<any>>();

/*
* Initializes a new instance of Keepalive
* @param http - The HTTP service.
*/
constructor(private http: Http) {
constructor(private http: HttpClient) {
super();
}

Expand All @@ -35,10 +35,10 @@ export class Keepalive extends KeepaliveSvc implements OnDestroy {
* @param url - The URL or Request object to use when pinging.
* @return The current Request used when pinging.
*/
request(url?: string|Request): Request {
request<T>(url?: string | HttpRequest<T>): HttpRequest<T> {
if (typeof url === 'string') {
this.pingRequest = new Request({method: RequestMethod.Get, url: url});
} else if (url instanceof Request) {
this.pingRequest = new HttpRequest<T>('GET', url);
} else if (url instanceof HttpRequest) {
this.pingRequest = url;
} else if (url === null) {
this.pingRequest = null;
Expand Down Expand Up @@ -70,7 +70,7 @@ export class Keepalive extends KeepaliveSvc implements OnDestroy {
ping(): void {
this.onPing.emit(null);
if (this.pingRequest) {
this.http.request(this.pingRequest).subscribe((response: Response) => {
this.http.request(this.pingRequest).subscribe((response: HttpResponse<any>) => {
this.onPingResponse.emit(response);
});
}
Expand All @@ -91,7 +91,7 @@ export class Keepalive extends KeepaliveSvc implements OnDestroy {
* Stops pinging on an interval.
*/
stop(): void {
if (this.pingHandle) {
if (this.hasPingHandle()) {
clearInterval(this.pingHandle);
this.pingHandle = null;
}
Expand All @@ -109,6 +109,10 @@ export class Keepalive extends KeepaliveSvc implements OnDestroy {
* @return True if the service will ping at the specified interval; otherwise, false.
*/
isRunning(): boolean {
return !!this.pingHandle;
return this.hasPingHandle();
}

private hasPingHandle(): boolean {
return this.pingHandle !== null && typeof this.pingHandle !== 'undefined';
}
}
1 change: 1 addition & 0 deletions modules/tsconfig.es5.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"emitDecoratorMetadata": true,
"outDir": "../.tmp/es5",
"baseUrl": "./",
"rootDir": "./",
"sourceMap": true,
"removeComments": false,
"moduleResolution": "node",
Expand Down
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ng-idle-src",
"version": "2.0.0-beta.12",
"private": true,
"description": "A module for responding to idle users in Angular 2+ applications.",
"description": "A module for responding to idle users in Angular 4+ applications.",
"scripts": {
"clean": "npm cache clean && rimraf node_modules && rimraf coverage && rimraf dist && rimraf .tmp",
"clean:dist": "rimraf dist && rimraf .tmp",
Expand Down Expand Up @@ -35,17 +35,17 @@
},
"homepage": "https://github.com/HackedByChinese/ng2-idle.git#readme",
"devDependencies": {
"@angular/compiler": "^2.0.0 || ^4.0.0",
"@angular/compiler-cli": "^2.0.0 || ^4.0.0",
"@angular/platform-browser": "^2.0.0 || ^4.0.0",
"@angular/platform-browser-dynamic": "^2.0.0 || ^4.0.0",
"@angular/compiler": "^4.0.0 || ^5.0.0",
"@angular/compiler-cli": "^4.0.0 || ^5.0.0",
"@angular/platform-browser": "^4.0.0 || ^5.0.0",
"@angular/platform-browser-dynamic": "^4.0.0 || ^5.0.0",
"@types/jasmine": "^2.5.37",
"@types/node": "^6.0.46",
"@types/source-map": "^0.1.29",
"@types/webpack": "^1.12.35",
"awesome-typescript-loader": "^3.0.3",
"conventional-changelog-cli": "^1.2.0",
"core-js": "^2.4.1",
"core-js": "^2.5.1",
"coveralls": "^2.11.15",
"cz-conventional-changelog": "^1.1.6",
"istanbul-instrumenter-loader": "0.2.0",
Expand All @@ -62,19 +62,18 @@
"rimraf": "^2.5.4",
"rollup": "^0.36.3",
"rollup-plugin-uglify": "^1.0.1",
"rxjs": "^5.2.0",
"rxjs": "^5.5.2",
"source-map-loader": "^0.1.5",
"ts-helpers": "^1.1.2",
"tslint": "^5.1.0",
"tslint-loader": "^3.5.2",
"typescript": "2.1.5",
"typescript": "~2.4.2",
"webpack": "2.1.0-beta.25",
"zone.js": "^0.7.8 || ^0.8.4"
"zone.js": "0.8.18"
},
"dependencies": {
"@angular/common": "^2.0.0 || ^4.0.0",
"@angular/core": "^2.0.0 || ^4.0.0",
"@angular/http": "^2.0.0 || ^4.0.0"
"@angular/common": "^4.0.0 || ^5.0.0",
"@angular/core": "^4.0.0 || ^5.0.0"
},
"config": {
"commitizen": {
Expand Down
4 changes: 1 addition & 3 deletions scripts/write-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ modules.map(function(module) {
pkg.version = basePkg.version;
pkg.peerDependencies = Object.assign({}, basePkg.dependencies);

if (module !== 'keepalive') {
delete pkg.peerDependencies["@angular/http"];
} else {
if (module === 'keepalive') {
pkg.peerDependencies['@ng-idle/core'] = '^' + basePkg.version;
}

Expand Down

0 comments on commit 602c6b2

Please sign in to comment.