@frshaw1/angular-visibility-api
Version:
An Angular and RxJS enabled event publisher for the browser's visibility API
61 lines • 2.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var visibility_service_1 = require("./visibility.service");
var injector = core_1.Injector.create({
providers: [{ provide: visibility_service_1.VisibilityService, deps: [] }]
});
var visibility = injector.get(visibility_service_1.VisibilityService);
/**
* A TypeScript method decorator that will call the annotated method each time the browser visibility is updated.
* The method should accept a single parameter which will correspond the current browser visibility.
*
* Each annotated method will be called immediately at runtime.
*
* @return {(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<Function>) => void}
* @constructor
*/
function OnVisibilityChange() {
return function (target, propertyKey, descriptor) {
var _this = this;
visibility.onVisibilityChange().subscribe(function (visibility) {
descriptor.value.apply(_this, [visibility]);
});
};
}
exports.OnVisibilityChange = OnVisibilityChange;
/**
* A TypeScript method decorator that will allow the annotated method to only be called if the browser visibility is
* equal to the specified configuration value upon invocation of the method.
*
* Usage:
*
* ```
* @WhenVisibilityIs(true)
* onlyCallWhenVisible() {
* ...
* //implementation of method that should only be called if the user is
* //currently interacting with the application's browser window
* }
* ```
*
* @param {boolean} specifiedVisibility
* @return {(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<Function>) => any}
* @constructor
*/
function WhenVisibilityIs(specifiedVisibility) {
return function (target, propertyKey, descriptor) {
var method = descriptor.value;
descriptor.value = function () {
var _this = this;
var _arguments = arguments;
visibility.onVisibilityChange().subscribe(function (isVisible) {
if (isVisible === specifiedVisibility) {
method.apply(_this, _arguments);
}
}).unsubscribe();
};
};
}
exports.WhenVisibilityIs = WhenVisibilityIs;
//# sourceMappingURL=visibility.decorators.js.map