node-opcua-utils
Advanced tools
Comparing version 2.0.0-alpha.8 to 2.0.0-alpha.9
/// <reference types="node" /> | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export declare function buffer_ellipsis(buffer: Buffer, start?: number, end?: number): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
function buffer_ellipsis(buffer, start, end) { | ||
@@ -4,0 +7,0 @@ start = start || 0; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:no-console | ||
const chalk_1 = require("chalk"); | ||
const buffer_ellipsis_1 = require("./buffer_ellipsis"); | ||
const chalk_1 = require("chalk"); | ||
function compare_buffers(buf1, buf2, max_length) { | ||
@@ -6,0 +10,0 @@ max_length = max_length || buf2.length; |
@@ -0,1 +1,4 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export declare function constructFilename(filename: string): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
function constructFilename(filename) { | ||
throw new Error("constructFilename has been deprecated ! use node-opcua-nodesets.constructFilename or ...."); | ||
//xx var dirname = $_$.dirname; | ||
//xx var file = path.join(dirname, filename); | ||
//xx return file; | ||
// xx var dirname = $_$.dirname; | ||
// xx var file = path.join(dirname, filename); | ||
// xx return file; | ||
} | ||
exports.constructFilename = constructFilename; | ||
//# sourceMappingURL=construct_filename.js.map |
@@ -0,1 +1,4 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export declare let get_clock_tick: () => number; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
if (process.hrtime) { | ||
const tick_origin = process.hrtime()[0]; | ||
const tickOrigin = process.hrtime()[0]; | ||
// clock it as a double in millisecond | ||
// so we can measure very tiny time intervals | ||
exports.get_clock_tick = function get_clock_tick() { | ||
exports.get_clock_tick = () => { | ||
const hrt = process.hrtime(); | ||
const r = (hrt[0] - tick_origin) * 1000.0 + Math.ceil(hrt[1] / 1e6 * 1000) / 1000; | ||
const r = (hrt[0] - tickOrigin) * 1000.0 + Math.ceil(hrt[1] / 1e6 * 1000) / 1000; | ||
return r; | ||
@@ -14,6 +14,4 @@ }; | ||
else { | ||
exports.get_clock_tick = function get_clock_tick() { | ||
return Date.now(); | ||
}; | ||
exports.get_clock_tick = () => Date.now(); | ||
} | ||
//# sourceMappingURL=get_clock_tick.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm; | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
// tslint:disable-next-line:ban-types | ||
function getFunctionParameterNames(func) { | ||
const fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
let result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES); | ||
if (result === null) | ||
if (result === null) { | ||
result = []; | ||
} | ||
return result; | ||
@@ -11,0 +16,0 @@ } |
/** | ||
* @module opcua.utils | ||
*/ | ||
/** | ||
* set a flag | ||
@@ -6,0 +3,0 @@ * @method set_flag |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module opcua.utils | ||
* @module node-opcua-utils | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// tslint:disable:no-bitwise | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
@@ -13,4 +14,5 @@ const path = require("path"); | ||
function set_flag(value, mask) { | ||
if (mask.value) | ||
if (mask.value) { | ||
mask = mask.value; | ||
} | ||
node_opcua_assert_1.assert(!mask.hasOwnProperty("value")); | ||
@@ -26,4 +28,5 @@ node_opcua_assert_1.assert(mask !== undefined); | ||
function check_flag(value, mask) { | ||
if (mask.value) | ||
if (mask.value) { | ||
mask = mask.value; | ||
} | ||
node_opcua_assert_1.assert(!mask.hasOwnProperty("value")); | ||
@@ -30,0 +33,0 @@ return ((value & mask) === mask); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
const fs_1 = require("fs"); | ||
@@ -9,5 +12,9 @@ const os = require("os"); | ||
this.write("// --------- This code has been automatically generated !!! " + new Date().toISOString()); | ||
this.write("/**"); | ||
this.write(" * @module node-opcua-types"); | ||
this.write(" */"); | ||
} | ||
write(...arg) { | ||
let str = ""; | ||
// tslint:disable:prefer-for-of | ||
for (let i = 0; i < arguments.length; i++) { | ||
@@ -14,0 +21,0 @@ str += arguments[i]; |
/** | ||
* @module node-opcua-utils | ||
*/ | ||
/** | ||
* @method getObjectClassName | ||
@@ -3,0 +6,0 @@ * @param obj |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
/** | ||
* @method getObjectClassName | ||
@@ -5,0 +8,0 @@ * @param obj |
@@ -0,1 +1,4 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export declare function replaceBufferWithHexDump(obj: any): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
function replaceBufferWithHexDump(obj) { | ||
@@ -4,0 +7,0 @@ for (const p in obj) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:ban-types | ||
const chalk_1 = require("chalk"); | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
const chalk_1 = require("chalk"); | ||
function setDeprecated(constructor, methodName, helpString) { | ||
@@ -12,3 +15,4 @@ const oldMethod = constructor.prototype[methodName]; | ||
if (counter % 1000 === 0) { | ||
console.log(chalk_1.default.green("Warning !"), "", chalk_1.default.green(chalk_1.default.bold(constructor.name + "#" + methodName), " is now deprecated")); | ||
// tslint:disable:no-console | ||
console.log(chalk_1.default.green("Warning !"), chalk_1.default.green(chalk_1.default.bold(constructor.name + "#" + methodName), " is now deprecated")); | ||
console.log(" ", helpString); | ||
@@ -15,0 +19,0 @@ } |
@@ -0,2 +1,5 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export declare function capitalizeFirstLetter(str: string): string; | ||
export declare function lowerFirstLetter(str: string): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:variable-name | ||
function capitalizeFirstLetter(str) { | ||
@@ -4,0 +8,0 @@ if (str == null) { |
/// <reference types="node" /> | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
import { EventEmitter } from "events"; | ||
@@ -7,3 +10,3 @@ export interface IWatchdogData2 { | ||
timeout: number; | ||
last_seen: number; | ||
lastSeen: number; | ||
visitCount: number; | ||
@@ -19,9 +22,14 @@ } | ||
export declare class WatchDog extends EventEmitter { | ||
/** | ||
* returns the number of subscribers using the WatchDog object. | ||
* @property subscriberCount | ||
* @type {number} | ||
*/ | ||
readonly subscriberCount: number; | ||
private readonly _watchdogDataMap; | ||
private _counter; | ||
private _current_time; | ||
private _currentTime; | ||
private _timer; | ||
private readonly _visit_subscriber_b; | ||
private readonly _visitSubscriberB; | ||
constructor(); | ||
private _visit_subscriber; | ||
/** | ||
@@ -44,10 +52,5 @@ * add a subscriber to the WatchDog. | ||
shutdown(): void; | ||
/** | ||
* returns the number of subscribers using the WatchDog object. | ||
* @property subscriberCount | ||
* @type {number} | ||
*/ | ||
readonly subscriberCount: number; | ||
private _visit_subscriber; | ||
private _start_timer; | ||
private _stop_timer; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _ = require("underscore"); | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
const events_1 = require("events"); | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
const _ = require("underscore"); | ||
function has_expired(watchDogData, currentTime) { | ||
const elapsed_time = currentTime - watchDogData.last_seen; | ||
return elapsed_time > watchDogData.timeout; | ||
const elapsedTime = currentTime - watchDogData.lastSeen; | ||
return elapsedTime > watchDogData.timeout; | ||
} | ||
@@ -14,8 +17,16 @@ function keepAliveFunc() { | ||
node_opcua_assert_1.assert(_.isNumber(self._watchDogData.key)); | ||
self._watchDogData.last_seen = Date.now(); | ||
self._watchDogData.lastSeen = Date.now(); | ||
if (self.onClientSeen) { | ||
self.onClientSeen(new Date(self._watchDogData.last_seen)); | ||
self.onClientSeen(new Date(self._watchDogData.lastSeen)); | ||
} | ||
} | ||
class WatchDog extends events_1.EventEmitter { | ||
/** | ||
* returns the number of subscribers using the WatchDog object. | ||
* @property subscriberCount | ||
* @type {number} | ||
*/ | ||
get subscriberCount() { | ||
return Object.keys(this._watchdogDataMap).length; | ||
} | ||
constructor() { | ||
@@ -25,23 +36,6 @@ super(); | ||
this._counter = 0; | ||
this._current_time = Date.now(); | ||
this._visit_subscriber_b = this._visit_subscriber.bind(this); | ||
this._currentTime = Date.now(); | ||
this._visitSubscriberB = this._visit_subscriber.bind(this); | ||
this._timer = null; // as NodeJS.Timer; | ||
} | ||
_visit_subscriber() { | ||
const self = this; | ||
self._current_time = Date.now(); | ||
const expired_subscribers = _.filter(self._watchdogDataMap, function (watchDogData) { | ||
watchDogData.visitCount += 1; | ||
return has_expired(watchDogData, self._current_time); | ||
}); | ||
//xx console.log("_visit_subscriber", _.map(expired_subscribers, _.property("key"))); | ||
if (expired_subscribers.length) { | ||
self.emit("timeout", expired_subscribers); | ||
} | ||
expired_subscribers.forEach((watchDogData) => { | ||
self.removeSubscriber(watchDogData.subscriber); | ||
watchDogData.subscriber.watchdogReset(); | ||
}); | ||
//xx self._current_time = Date.now(); | ||
} | ||
/** | ||
@@ -63,3 +57,3 @@ * add a subscriber to the WatchDog. | ||
const self = this; | ||
self._current_time = Date.now(); | ||
self._currentTime = Date.now(); | ||
timeout = timeout || 1000; | ||
@@ -73,6 +67,6 @@ node_opcua_assert_1.assert(_.isNumber(timeout), " invalid timeout "); | ||
subscriber._watchDogData = { | ||
key: key, | ||
subscriber: subscriber, | ||
timeout: timeout, | ||
last_seen: self._current_time, | ||
key, | ||
lastSeen: self._currentTime, | ||
subscriber, | ||
timeout, | ||
visitCount: 0 | ||
@@ -82,3 +76,3 @@ }; | ||
if (subscriber.onClientSeen) { | ||
subscriber.onClientSeen(new Date(subscriber._watchDogData.last_seen)); | ||
subscriber.onClientSeen(new Date(subscriber._watchDogData.lastSeen)); | ||
} | ||
@@ -106,3 +100,3 @@ subscriber.keepAlive = keepAliveFunc.bind(subscriber); | ||
// delete timer when the last subscriber comes out | ||
//xx console.log("xxxx WatchDog.prototype.removeSubscriber ",this.subscriberCount ); | ||
// xx console.log("xxxx WatchDog.prototype.removeSubscriber ",this.subscriberCount ); | ||
if (this.subscriberCount === 0) { | ||
@@ -115,13 +109,22 @@ this._stop_timer(); | ||
} | ||
/** | ||
* returns the number of subscribers using the WatchDog object. | ||
* @property subscriberCount | ||
* @type {number} | ||
*/ | ||
get subscriberCount() { | ||
return Object.keys(this._watchdogDataMap).length; | ||
_visit_subscriber() { | ||
const self = this; | ||
self._currentTime = Date.now(); | ||
const expiredSubscribers = _.filter(self._watchdogDataMap, (watchDogData) => { | ||
watchDogData.visitCount += 1; | ||
return has_expired(watchDogData, self._currentTime); | ||
}); | ||
// xx console.log("_visit_subscriber", _.map(expired_subscribers, _.property("key"))); | ||
if (expiredSubscribers.length) { | ||
self.emit("timeout", expiredSubscribers); | ||
} | ||
expiredSubscribers.forEach((watchDogData) => { | ||
self.removeSubscriber(watchDogData.subscriber); | ||
watchDogData.subscriber.watchdogReset(); | ||
}); | ||
// xx self._current_time = Date.now(); | ||
} | ||
_start_timer() { | ||
node_opcua_assert_1.assert(this._timer === null, " setInterval already called ?"); | ||
this._timer = setInterval(this._visit_subscriber_b, 1000); | ||
this._timer = setInterval(this._visitSubscriberB, 1000); | ||
} | ||
@@ -128,0 +131,0 @@ _stop_timer() { |
{ | ||
"name": "node-opcua-utils", | ||
"version": "2.0.0-alpha.8", | ||
"version": "2.0.0-alpha.9", | ||
"description": "pure nodejs OPCUA SDK - module -utils", | ||
@@ -13,11 +13,11 @@ "main": "./dist/index.js", | ||
"dependencies": { | ||
"node-opcua-assert": "^2.0.0-alpha.8", | ||
"node-opcua-assert": "^2.0.0-alpha.9", | ||
"underscore": "^1.9.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^10.12.15", | ||
"@types/node": "^10.12.18", | ||
"@types/underscore": "^1.8.9", | ||
"chalk": "^2.4.1", | ||
"should": "13.2.3", | ||
"sinon": "^7.1.1" | ||
"sinon": "^7.2.2" | ||
}, | ||
@@ -39,3 +39,3 @@ "author": "Etienne Rossignon", | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "7bbf38d5a3cd29cb6aed6ca38078f4d89d3f956a" | ||
"gitHead": "cd7120d1840593757f0f3c9e53be4e85883dc0e6" | ||
} |
@@ -0,1 +1,4 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export function buffer_ellipsis(buffer: Buffer, start?: number, end?: number): string { | ||
@@ -2,0 +5,0 @@ start = start || 0; |
@@ -0,3 +1,7 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:no-console | ||
import chalk from "chalk"; | ||
import { buffer_ellipsis } from "./buffer_ellipsis"; | ||
import chalk from "chalk"; | ||
@@ -4,0 +8,0 @@ export function compare_buffers(buf1: Buffer, buf2: Buffer, max_length?: number): void { |
@@ -0,6 +1,9 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export function constructFilename(filename: string) { | ||
throw new Error("constructFilename has been deprecated ! use node-opcua-nodesets.constructFilename or ...."); | ||
//xx var dirname = $_$.dirname; | ||
//xx var file = path.join(dirname, filename); | ||
//xx return file; | ||
// xx var dirname = $_$.dirname; | ||
// xx var file = path.join(dirname, filename); | ||
// xx return file; | ||
} |
@@ -0,16 +1,17 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export let get_clock_tick: () => number; | ||
if (process.hrtime) { | ||
const tick_origin = process.hrtime()[0]; | ||
const tickOrigin = process.hrtime()[0]; | ||
// clock it as a double in millisecond | ||
// so we can measure very tiny time intervals | ||
get_clock_tick = function get_clock_tick() { | ||
get_clock_tick = () => { | ||
const hrt = process.hrtime(); | ||
const r = (hrt[0] - tick_origin) * 1000.0 + Math.ceil(hrt[1] / 1e6 * 1000) / 1000; | ||
const r = (hrt[0] - tickOrigin) * 1000.0 + Math.ceil(hrt[1] / 1e6 * 1000) / 1000; | ||
return r; | ||
}; | ||
} else { | ||
get_clock_tick = function get_clock_tick() { | ||
return Date.now(); | ||
}; | ||
get_clock_tick = () => Date.now(); | ||
} |
@@ -0,9 +1,13 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm; | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
// tslint:disable-next-line:ban-types | ||
export function getFunctionParameterNames(func: Function) { | ||
const fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
let result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES); | ||
if (result === null) result = []; | ||
if (result === null) { result = []; } | ||
return result; | ||
} |
/** | ||
* @module opcua.utils | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:no-bitwise | ||
import { assert } from "node-opcua-assert"; | ||
const path = require("path"); | ||
import * as path from "path"; | ||
@@ -13,3 +13,5 @@ /** | ||
export function set_flag(value: number, mask: number| {value: number}): number { | ||
if ((mask as any).value) mask = (mask as any).value; | ||
if ((mask as any).value) { | ||
mask = (mask as any).value; | ||
} | ||
assert(!mask.hasOwnProperty("value")); | ||
@@ -24,3 +26,5 @@ assert(mask !== undefined); | ||
export function check_flag(value: number, mask: number| {value: number}): boolean { | ||
if ((mask as any).value) mask = (mask as any).value; | ||
if ((mask as any).value) { | ||
mask = (mask as any).value; | ||
} | ||
assert(!mask.hasOwnProperty("value")); | ||
@@ -27,0 +31,0 @@ return ((value & (mask as number)) === (mask as number)); |
@@ -0,1 +1,4 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
import { writeFileSync } from "fs"; | ||
@@ -11,6 +14,11 @@ import * as os from "os"; | ||
this.write("// --------- This code has been automatically generated !!! " + new Date().toISOString()); | ||
this.write("/**"); | ||
this.write(" * @module node-opcua-types"); | ||
this.write(" */"); | ||
} | ||
write(...arg: string[]): void { | ||
public write(...arg: string[]): void { | ||
let str = ""; | ||
// tslint:disable:prefer-for-of | ||
for (let i = 0; i < arguments.length; i++) { | ||
@@ -22,11 +30,11 @@ str += arguments[i]; | ||
toString(): string { | ||
public toString(): string { | ||
return this._line.join(os.EOL); | ||
} | ||
save(filename: string): void { | ||
public save(filename: string): void { | ||
writeFileSync(filename, this.toString(), "ascii"); | ||
} | ||
saveFormat(filename: string, formatter: (code: string) => string): void { | ||
public saveFormat(filename: string, formatter: (code: string) => string): void { | ||
const code = formatter(this.toString()); | ||
@@ -33,0 +41,0 @@ writeFileSync(filename, code, "ascii"); |
/** | ||
* @module node-opcua-utils | ||
*/ | ||
/** | ||
* @method getObjectClassName | ||
@@ -3,0 +6,0 @@ * @param obj |
@@ -0,1 +1,4 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
export function replaceBufferWithHexDump(obj: any): any { | ||
@@ -2,0 +5,0 @@ for (const p in obj) { |
@@ -0,14 +1,20 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:ban-types | ||
import chalk from "chalk"; | ||
import { assert } from "node-opcua-assert"; | ||
import chalk from "chalk"; | ||
export function setDeprecated(constructor: Function, methodName: string, helpString: string): void { | ||
const oldMethod = constructor.prototype[methodName]; | ||
assert(oldMethod instanceof Function, "expecting a valid " + methodName + "method on class " + constructor.constructor.name); | ||
assert(oldMethod instanceof Function, | ||
"expecting a valid " + methodName + "method on class " + constructor.constructor.name); | ||
let counter = 0; | ||
constructor.prototype[methodName] = function () { | ||
constructor.prototype[methodName] = function() { | ||
if (counter % 1000 === 0) { | ||
console.log(chalk.green("Warning !"), "", chalk.green(chalk.bold(constructor.name + "#" + methodName), " is now deprecated")); | ||
// tslint:disable:no-console | ||
console.log(chalk.green("Warning !"), | ||
chalk.green(chalk.bold(constructor.name + "#" + methodName), " is now deprecated")); | ||
console.log(" ", helpString); | ||
@@ -15,0 +21,0 @@ } |
@@ -0,1 +1,5 @@ | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
// tslint:disable:variable-name | ||
export function capitalizeFirstLetter(str: string): string { | ||
@@ -2,0 +6,0 @@ if (str == null) { |
@@ -1,4 +0,7 @@ | ||
import * as _ from "underscore"; | ||
/** | ||
* @module node-opcua-utils | ||
*/ | ||
import { EventEmitter } from "events"; | ||
import { assert } from "node-opcua-assert"; | ||
import * as _ from "underscore"; | ||
@@ -9,3 +12,3 @@ export interface IWatchdogData2 { | ||
timeout: number; | ||
last_seen: number; | ||
lastSeen: number; | ||
visitCount: number; | ||
@@ -22,4 +25,4 @@ } | ||
function has_expired(watchDogData: IWatchdogData2, currentTime: number) { | ||
const elapsed_time = currentTime - watchDogData.last_seen; | ||
return elapsed_time > watchDogData.timeout; | ||
const elapsedTime = currentTime - watchDogData.lastSeen; | ||
return elapsedTime > watchDogData.timeout; | ||
} | ||
@@ -31,5 +34,5 @@ | ||
assert(_.isNumber(self._watchDogData.key)); | ||
self._watchDogData.last_seen = Date.now(); | ||
self._watchDogData.lastSeen = Date.now(); | ||
if (self.onClientSeen) { | ||
self.onClientSeen(new Date(self._watchDogData.last_seen)); | ||
self.onClientSeen(new Date(self._watchDogData.lastSeen)); | ||
} | ||
@@ -39,7 +42,15 @@ } | ||
export class WatchDog extends EventEmitter { | ||
/** | ||
* returns the number of subscribers using the WatchDog object. | ||
* @property subscriberCount | ||
* @type {number} | ||
*/ | ||
get subscriberCount(): number { | ||
return Object.keys(this._watchdogDataMap).length; | ||
} | ||
private readonly _watchdogDataMap: { [id: number]: IWatchdogData2 }; | ||
private _counter: number; | ||
private _current_time: number; | ||
private _currentTime: number; | ||
private _timer: NodeJS.Timer | null; | ||
private readonly _visit_subscriber_b: (...args: any[]) => void; | ||
private readonly _visitSubscriberB: (...args: any[]) => void; | ||
@@ -51,28 +62,7 @@ constructor() { | ||
this._counter = 0; | ||
this._current_time = Date.now(); | ||
this._visit_subscriber_b = this._visit_subscriber.bind(this); | ||
this._currentTime = Date.now(); | ||
this._visitSubscriberB = this._visit_subscriber.bind(this); | ||
this._timer = null; // as NodeJS.Timer; | ||
} | ||
private _visit_subscriber() { | ||
const self = this; | ||
self._current_time = Date.now(); | ||
const expired_subscribers = _.filter(self._watchdogDataMap, function(watchDogData: IWatchdogData2) { | ||
watchDogData.visitCount += 1; | ||
return has_expired(watchDogData, self._current_time); | ||
}); | ||
//xx console.log("_visit_subscriber", _.map(expired_subscribers, _.property("key"))); | ||
if (expired_subscribers.length) { | ||
self.emit("timeout", expired_subscribers); | ||
} | ||
expired_subscribers.forEach((watchDogData: IWatchdogData2) => { | ||
self.removeSubscriber(watchDogData.subscriber); | ||
watchDogData.subscriber.watchdogReset(); | ||
}); | ||
//xx self._current_time = Date.now(); | ||
} | ||
/** | ||
@@ -92,5 +82,5 @@ * add a subscriber to the WatchDog. | ||
*/ | ||
addSubscriber(subscriber: ISubscriber, timeout: number) { | ||
public addSubscriber(subscriber: ISubscriber, timeout: number) { | ||
const self = this; | ||
self._current_time = Date.now(); | ||
self._currentTime = Date.now(); | ||
timeout = timeout || 1000; | ||
@@ -106,6 +96,6 @@ assert(_.isNumber(timeout), " invalid timeout "); | ||
subscriber._watchDogData = { | ||
key: key, | ||
subscriber: subscriber, | ||
timeout: timeout, | ||
last_seen: self._current_time, | ||
key, | ||
lastSeen: self._currentTime, | ||
subscriber, | ||
timeout, | ||
visitCount: 0 | ||
@@ -117,3 +107,3 @@ } as IWatchdogData2; | ||
if (subscriber.onClientSeen) { | ||
subscriber.onClientSeen(new Date(subscriber._watchDogData.last_seen)); | ||
subscriber.onClientSeen(new Date(subscriber._watchDogData.lastSeen)); | ||
} | ||
@@ -131,3 +121,3 @@ subscriber.keepAlive = keepAliveFunc.bind(subscriber); | ||
removeSubscriber(subscriber: ISubscriber) { | ||
public removeSubscriber(subscriber: ISubscriber) { | ||
if (!subscriber._watchDog) { | ||
@@ -147,3 +137,3 @@ return; // already removed !!! | ||
// delete timer when the last subscriber comes out | ||
//xx console.log("xxxx WatchDog.prototype.removeSubscriber ",this.subscriberCount ); | ||
// xx console.log("xxxx WatchDog.prototype.removeSubscriber ",this.subscriberCount ); | ||
if (this.subscriberCount === 0) { | ||
@@ -154,3 +144,3 @@ this._stop_timer(); | ||
shutdown(): void { | ||
public shutdown(): void { | ||
assert( | ||
@@ -161,9 +151,22 @@ this._timer === null && Object.keys(this._watchdogDataMap).length === 0, | ||
} | ||
/** | ||
* returns the number of subscribers using the WatchDog object. | ||
* @property subscriberCount | ||
* @type {number} | ||
*/ | ||
get subscriberCount(): number { | ||
return Object.keys(this._watchdogDataMap).length; | ||
private _visit_subscriber() { | ||
const self = this; | ||
self._currentTime = Date.now(); | ||
const expiredSubscribers = _.filter(self._watchdogDataMap, (watchDogData: IWatchdogData2) => { | ||
watchDogData.visitCount += 1; | ||
return has_expired(watchDogData, self._currentTime); | ||
}); | ||
// xx console.log("_visit_subscriber", _.map(expired_subscribers, _.property("key"))); | ||
if (expiredSubscribers.length) { | ||
self.emit("timeout", expiredSubscribers); | ||
} | ||
expiredSubscribers.forEach((watchDogData: IWatchdogData2) => { | ||
self.removeSubscriber(watchDogData.subscriber); | ||
watchDogData.subscriber.watchdogReset(); | ||
}); | ||
// xx self._current_time = Date.now(); | ||
} | ||
@@ -173,3 +176,3 @@ | ||
assert(this._timer === null, " setInterval already called ?"); | ||
this._timer = setInterval(this._visit_subscriber_b, 1000) as NodeJS.Timer; | ||
this._timer = setInterval(this._visitSubscriberB, 1000) as NodeJS.Timer; | ||
} | ||
@@ -176,0 +179,0 @@ private _stop_timer(): void { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
49053
51
950