Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
4d0fb3eb30 | |||
3f0b230d23 | |||
0c0b08e2be | |||
dd722146f4 | |||
c968e156ae | |||
b5509711e7 | |||
f8540b8076 | |||
30d18db4af | |||
388649659c | |||
e8ff289a9a | |||
a5deb25bfb | |||
ce2d1b278b | |||
aa16e5d5bb | |||
f221e9c0fa | |||
9935396f2d | |||
1bc379daa5 | |||
f9b81fc801 |
19
.gitignore
vendored
19
.gitignore
vendored
@ -1,5 +1,22 @@
|
|||||||
.nogit/
|
.nogit/
|
||||||
node_modules/
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
public/
|
public/
|
||||||
pages/
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# caches
|
||||||
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
@ -1,4 +1,4 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
@ -78,19 +78,11 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm install
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume "$PWD":/code
|
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
@ -109,10 +101,10 @@ pages:
|
|||||||
image: hosttoday/ht-docker-node:npmci
|
image: hosttoday/ht-docker-node:npmci
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
},
|
},
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
|
3445
package-lock.json
generated
3445
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
44
package.json
44
package.json
@ -1,30 +1,42 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.6",
|
"version": "3.0.14",
|
||||||
"description": "handle timeformats in smart ways",
|
"description": "handle time in smart ways",
|
||||||
"main": "dist/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest ./test/)",
|
"test": "(tstest ./test/)",
|
||||||
"build": "(tsbuild)"
|
"build": "(tsbuild && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.8",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"@gitzone/tsrun": "^1.2.5",
|
"@gitzone/tsbundle": "^1.0.69",
|
||||||
"@gitzone/tstest": "^1.0.20",
|
"@gitzone/tsrun": "^1.2.6",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@gitzone/tstest": "^1.0.24",
|
||||||
"@types/node": "^11.13.2",
|
"@pushrocks/tapbundle": "^3.0.9",
|
||||||
"tslint": "^5.15.0",
|
"@types/node": "^14.0.5",
|
||||||
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/smartdelay": "^2.0.6",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@types/cron": "^1.7.0",
|
"@types/cron": "^1.7.1",
|
||||||
"@types/luxon": "^1.12.0",
|
"@types/luxon": "^1.15.1",
|
||||||
"cron": "^1.7.0",
|
"cron-parser": "^2.14.0",
|
||||||
"luxon": "^1.12.1"
|
"luxon": "^1.16.0"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/*",
|
||||||
|
"ts_web/*",
|
||||||
|
"dist/*",
|
||||||
|
"dist_web/*",
|
||||||
|
"assets/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
15
readme.md
15
readme.md
@ -22,9 +22,20 @@ Use TypeScript for best in class instellisense.
|
|||||||
|
|
||||||
Smarttime offers smart ways to deal with time.
|
Smarttime offers smart ways to deal with time.
|
||||||
|
|
||||||
### class ExtendedDate
|
### class CronManager
|
||||||
|
This class provides scheduling of functions with a cron syntax
|
||||||
|
|
||||||
This class offers static functions to create zone speicific JavaScript dates from European formated time strings.
|
```typescript
|
||||||
|
import { CronManager } from '@pushrocks/smarrtime';
|
||||||
|
const cronManagerInstance = new CronManager();
|
||||||
|
cronManagerInstance.addConrjob('* * * * * *', async () => {
|
||||||
|
console.log('hello'); // will log 'hello' to console once every second;
|
||||||
|
})
|
||||||
|
cronManagerInstance.start();
|
||||||
|
```
|
||||||
|
|
||||||
|
### class ExtendedDate
|
||||||
|
This class offers static functions to create zone specific JavaScript dates from European formated time strings.
|
||||||
|
|
||||||
```TypeScript
|
```TypeScript
|
||||||
import { ExtendedDate } from '@pushrocks/smarttime'
|
import { ExtendedDate } from '@pushrocks/smarttime'
|
||||||
|
@ -6,6 +6,22 @@ let testCronManager: smarttime.CronManager;
|
|||||||
|
|
||||||
tap.test('should create a valid instance of cronmanager', async () => {
|
tap.test('should create a valid instance of cronmanager', async () => {
|
||||||
testCronManager = new smarttime.CronManager();
|
testCronManager = new smarttime.CronManager();
|
||||||
|
expect(testCronManager).to.be.instanceOf(smarttime.CronManager);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a valid cronJon', async tools => {
|
||||||
|
const done = tools.defer();
|
||||||
|
let counter = 0;
|
||||||
|
testCronManager.addCronjob('* * * * * *', () => {
|
||||||
|
if (counter === 10) {
|
||||||
|
done.resolve();
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
console.log(`hey ${counter}`);
|
||||||
|
});
|
||||||
|
testCronManager.start();
|
||||||
|
await done.promise;
|
||||||
|
testCronManager.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -16,4 +16,9 @@ tap.test('should create a date and time with European Format', async () => {
|
|||||||
console.log(extendedDate);
|
console.log(extendedDate);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should create a European date string', async () => {
|
||||||
|
const extendedDate = smarttime.ExtendedDate.fromHyphedDate('2018-02-13');
|
||||||
|
expect(extendedDate.exportToEuropeanDate()).to.equal('13.02.2018');
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
@ -1,7 +1,7 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
export * from './smarttime.classes.cronmanager';
|
export * from './smarttime.classes.cronmanager';
|
||||||
export * from './smarttime.classes.date';
|
export * from './smarttime.classes.extendeddate';
|
||||||
export * from './smarttime.classes.hrtmeasurement';
|
export * from './smarttime.classes.hrtmeasurement';
|
||||||
export * from './smarttime.classes.timer';
|
export * from './smarttime.classes.timer';
|
||||||
export * from './smarttime.classes.timestamp';
|
export * from './smarttime.classes.timestamp';
|
||||||
|
45
ts/smarttime.classes.cronjob.ts
Normal file
45
ts/smarttime.classes.cronjob.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
import { CronManager } from './smarttime.classes.cronmanager';
|
||||||
|
|
||||||
|
export type TJobFunction = (() => void) | (() => Promise<any>);
|
||||||
|
|
||||||
|
export class CronJob {
|
||||||
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
|
public cronExpression: string;
|
||||||
|
public jobFunction: TJobFunction;
|
||||||
|
|
||||||
|
private cronInterval= plugins.cronParser.parseExpression('* * * * * *');
|
||||||
|
private nextExecutionUnix: number = 0;
|
||||||
|
|
||||||
|
constructor(cronManager: CronManager, cronExpressionArg: string, jobFunction: TJobFunction ) {
|
||||||
|
this.cronExpression = cronExpressionArg;
|
||||||
|
this.jobFunction = jobFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* checks wether the cronjob needs to be executed
|
||||||
|
*/
|
||||||
|
public checkExecution() {
|
||||||
|
if (this.nextExecutionUnix === 0) {
|
||||||
|
this.nextExecutionUnix = this.cronInterval.next().toDate().getTime();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (Date.now() > this.nextExecutionUnix) {
|
||||||
|
this.jobFunction();
|
||||||
|
this.nextExecutionUnix = this.cronInterval.next().toDate().getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.cronInterval = this.getCronInterval();
|
||||||
|
this.status = 'started';
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop() {
|
||||||
|
this.status = 'stopped';
|
||||||
|
}
|
||||||
|
|
||||||
|
private getCronInterval () {
|
||||||
|
return plugins.cronParser.parseExpression(this.cronExpression);
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,49 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
import * as plugins from './smarttime.plugins';
|
||||||
|
import { CronJob } from './smarttime.classes.cronjob';
|
||||||
|
import { Timer } from './smarttime.classes.timer';
|
||||||
|
import { Interval } from './smarttime.classes.interval';
|
||||||
|
|
||||||
export class CronManager {}
|
export class CronManager {
|
||||||
|
public cronInterval = new Interval(1000);
|
||||||
|
|
||||||
|
public status: 'started' | 'stopped' = 'stopped';
|
||||||
|
public cronjobs: CronJob[] = [];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.cronInterval.addIntervalJob(() => {
|
||||||
|
for (const cronJob of this.cronjobs) {
|
||||||
|
cronJob.checkExecution();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
|
||||||
|
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
||||||
|
this.cronjobs.push(newCronJob);
|
||||||
|
if (this.status === 'started') {
|
||||||
|
newCronJob.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* starts the cronjob
|
||||||
|
*/
|
||||||
|
public start() {
|
||||||
|
this.status = 'started';
|
||||||
|
for (const cron of this.cronjobs) {
|
||||||
|
cron.start();
|
||||||
|
}
|
||||||
|
this.cronInterval.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stops all cronjobs
|
||||||
|
*/
|
||||||
|
public stop() {
|
||||||
|
this.status = 'stopped';
|
||||||
|
for (const cron of this.cronjobs) {
|
||||||
|
cron.stop();
|
||||||
|
}
|
||||||
|
this.cronInterval.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,40 +0,0 @@
|
|||||||
import * as plugins from './smarttime.plugins';
|
|
||||||
|
|
||||||
export type TAvailableZone = 'Europe/Berlin';
|
|
||||||
|
|
||||||
export class ExtendedDate extends Date {
|
|
||||||
public static fromEuropeanDate(europeanDate: string) {
|
|
||||||
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
|
||||||
const luxonDate = plugins.luxon.DateTime.utc(
|
|
||||||
parseFloat(dateArray[3]), // year
|
|
||||||
parseFloat(dateArray[2]), // month
|
|
||||||
parseFloat(dateArray[1]) // day
|
|
||||||
);
|
|
||||||
const unixMilli = luxonDate.toMillis();
|
|
||||||
return new ExtendedDate(unixMilli);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** */
|
|
||||||
public static fromEuropeanDateAndTime(
|
|
||||||
europeanDateArg: string,
|
|
||||||
timeArg: string,
|
|
||||||
zoneArg: TAvailableZone
|
|
||||||
) {
|
|
||||||
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
|
|
||||||
const sliceDate = (dateString: string) => {
|
|
||||||
return `0${dateString}`.slice(-2);
|
|
||||||
};
|
|
||||||
const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate(
|
|
||||||
dateArray[1]
|
|
||||||
)}T${timeArg}`;
|
|
||||||
const luxonDate = plugins.luxon.DateTime.fromISO(dateTimeString, {
|
|
||||||
zone: zoneArg
|
|
||||||
});
|
|
||||||
const unixMilli = luxonDate.toMillis();
|
|
||||||
return new ExtendedDate(unixMilli);
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(unixMilli: number) {
|
|
||||||
super(unixMilli);
|
|
||||||
}
|
|
||||||
}
|
|
127
ts/smarttime.classes.extendeddate.ts
Normal file
127
ts/smarttime.classes.extendeddate.ts
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export type TAvailableZone = 'Europe/Berlin';
|
||||||
|
|
||||||
|
export interface IDateUnits {
|
||||||
|
year: number;
|
||||||
|
yearString: string;
|
||||||
|
month: number;
|
||||||
|
monthString: string;
|
||||||
|
monthName: string;
|
||||||
|
day: number;
|
||||||
|
dayString: string;
|
||||||
|
dayOfTheWeek: number;
|
||||||
|
dayOfTheWeekName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ExtendedDate extends Date {
|
||||||
|
// STATIC factories
|
||||||
|
public static fromMillis(milliSeconds: number) {
|
||||||
|
return new ExtendedDate(milliSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static fromDate(dateArg: Date) {
|
||||||
|
return new ExtendedDate(dateArg.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static fromEuropeanDate(europeanDate: string) {
|
||||||
|
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
|
||||||
|
const luxonDate = plugins.luxon.DateTime.utc(
|
||||||
|
parseFloat(dateArray[3]), // year
|
||||||
|
parseFloat(dateArray[2]), // month
|
||||||
|
parseFloat(dateArray[1]) // day
|
||||||
|
);
|
||||||
|
const unixMilli = luxonDate.toMillis();
|
||||||
|
return new ExtendedDate(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* creates an Extended date from a hypedDate like "2018-03-28"
|
||||||
|
* @param dateString
|
||||||
|
*/
|
||||||
|
public static fromHyphedDate(dateString: string) {
|
||||||
|
// guards
|
||||||
|
// implementation
|
||||||
|
const dateMillis = new Date(dateString).getTime();
|
||||||
|
return new ExtendedDate(dateMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as .fromEuropeanDate(), but accepts additional timeArg and zoneArg
|
||||||
|
*/
|
||||||
|
public static fromEuropeanDateAndTime(
|
||||||
|
europeanDateArg: string,
|
||||||
|
timeArg: string = '12:00:00',
|
||||||
|
zoneArg: TAvailableZone = 'Europe/Berlin'
|
||||||
|
) {
|
||||||
|
// guards
|
||||||
|
|
||||||
|
// implementation
|
||||||
|
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
|
||||||
|
const sliceDate = (dateString: string) => {
|
||||||
|
return `0${dateString}`.slice(-2);
|
||||||
|
};
|
||||||
|
const dateTimeString = `${dateArray[3]}-${sliceDate(dateArray[2])}-${sliceDate(
|
||||||
|
dateArray[1]
|
||||||
|
)}T${timeArg}`;
|
||||||
|
const luxonDate = plugins.luxon.DateTime.fromISO(dateTimeString, {
|
||||||
|
zone: zoneArg
|
||||||
|
});
|
||||||
|
const unixMilli = luxonDate.toMillis();
|
||||||
|
return new ExtendedDate(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public timezone: TAvailableZone;
|
||||||
|
|
||||||
|
|
||||||
|
constructor(unixMilli: number) {
|
||||||
|
super(unixMilli);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
public exportToEuropeanDate() {
|
||||||
|
const units = this.exportToUnits();
|
||||||
|
return `${units.dayString}.${units.monthString}.${units.yearString}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* exports units
|
||||||
|
*/
|
||||||
|
public exportToUnits(): IDateUnits {
|
||||||
|
const monthsArray = [
|
||||||
|
'January',
|
||||||
|
'February',
|
||||||
|
'March',
|
||||||
|
'April',
|
||||||
|
'May',
|
||||||
|
'June',
|
||||||
|
'July',
|
||||||
|
'August',
|
||||||
|
'September',
|
||||||
|
'October',
|
||||||
|
'November',
|
||||||
|
'December'
|
||||||
|
];
|
||||||
|
const daysArray = [
|
||||||
|
'Monday',
|
||||||
|
'Tuesday',
|
||||||
|
'Wednesday',
|
||||||
|
'Thursday',
|
||||||
|
'Friday',
|
||||||
|
'Saturday',
|
||||||
|
'Sunday'
|
||||||
|
];
|
||||||
|
return {
|
||||||
|
year: this.getFullYear(),
|
||||||
|
yearString: `${this.getFullYear()}`,
|
||||||
|
month: this.getMonth() + 1,
|
||||||
|
monthString: ("0" + (this.getMonth() + 1)).slice(-2),
|
||||||
|
monthName: monthsArray[this.getMonth()],
|
||||||
|
day: this.getDate(),
|
||||||
|
dayString: ("0" + this.getDate()).slice(-2),
|
||||||
|
dayOfTheWeek: this.getDay(),
|
||||||
|
dayOfTheWeekName: daysArray[this.getDay()]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,9 @@
|
|||||||
import * as process from 'process';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* easy high resolution time measurement
|
* easy high resolution time measurement
|
||||||
*/
|
*/
|
||||||
export class HrtMeasurement {
|
export class HrtMeasurement {
|
||||||
nanoSeconds: number = null;
|
public nanoSeconds: number = null;
|
||||||
milliSeconds: number = null;
|
public milliSeconds: number = null;
|
||||||
private _hrTimeStart = null;
|
private _hrTimeStart = null;
|
||||||
private _hrTimeStopDiff = null;
|
private _hrTimeStopDiff = null;
|
||||||
private _started: boolean = false;
|
private _started: boolean = false;
|
||||||
@ -13,7 +11,7 @@ export class HrtMeasurement {
|
|||||||
/**
|
/**
|
||||||
* start the measurement
|
* start the measurement
|
||||||
*/
|
*/
|
||||||
start() {
|
public start() {
|
||||||
this._started = true;
|
this._started = true;
|
||||||
this._hrTimeStart = process.hrtime();
|
this._hrTimeStart = process.hrtime();
|
||||||
}
|
}
|
||||||
@ -21,7 +19,7 @@ export class HrtMeasurement {
|
|||||||
/**
|
/**
|
||||||
* stop the measurement
|
* stop the measurement
|
||||||
*/
|
*/
|
||||||
stop() {
|
public stop() {
|
||||||
if (this._started === false) {
|
if (this._started === false) {
|
||||||
console.log("Hasn't started yet");
|
console.log("Hasn't started yet");
|
||||||
return;
|
return;
|
||||||
@ -35,7 +33,7 @@ export class HrtMeasurement {
|
|||||||
/**
|
/**
|
||||||
* reset the measurement
|
* reset the measurement
|
||||||
*/
|
*/
|
||||||
reset() {
|
public reset() {
|
||||||
this.nanoSeconds = null;
|
this.nanoSeconds = null;
|
||||||
this.milliSeconds = null;
|
this.milliSeconds = null;
|
||||||
this._hrTimeStart = null;
|
this._hrTimeStart = null;
|
||||||
|
40
ts/smarttime.classes.interval.ts
Normal file
40
ts/smarttime.classes.interval.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import * as plugins from './smarttime.plugins';
|
||||||
|
|
||||||
|
export class Interval {
|
||||||
|
public status: 'started' | 'stopped' | 'initial' = 'initial';
|
||||||
|
private statusAuthorization: any = null;
|
||||||
|
|
||||||
|
public intervalMilliseconds: number;
|
||||||
|
public intervalJobs: Array<() => any> = [];
|
||||||
|
constructor(intervalMillisencondsArg: number) {
|
||||||
|
this.intervalMilliseconds = intervalMillisencondsArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public start() {
|
||||||
|
this.status = 'started';
|
||||||
|
const statusAuth = new Date();
|
||||||
|
this.statusAuthorization = statusAuth;
|
||||||
|
const runInterval = async () => {
|
||||||
|
while (this.status === 'started' && this.statusAuthorization === statusAuth) {
|
||||||
|
await plugins.smartdelay.delayFor(this.intervalMilliseconds);
|
||||||
|
this.executeIntervalJobs();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
runInterval();
|
||||||
|
}
|
||||||
|
|
||||||
|
public stop () {
|
||||||
|
this.status = 'stopped';
|
||||||
|
this.statusAuthorization = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addIntervalJob(funcArg: () => any) {
|
||||||
|
this.intervalJobs.push(funcArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
private executeIntervalJobs () {
|
||||||
|
for (const funcArg of this.intervalJobs) {
|
||||||
|
funcArg();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,11 @@
|
|||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export { smartpromise };
|
export { smartdelay, smartpromise };
|
||||||
|
|
||||||
// third parties
|
// third parties
|
||||||
|
import cronParser from 'cron-parser';
|
||||||
import * as luxon from 'luxon';
|
import * as luxon from 'luxon';
|
||||||
import * as cron from 'cron';
|
|
||||||
|
|
||||||
export { luxon, cron };
|
export { cronParser, luxon };
|
||||||
|
Reference in New Issue
Block a user