Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
725102d02c | |||
7c73579c9f | |||
a1996ea1bd | |||
dc92ae7140 | |||
3e862f233c | |||
73f7360f8c | |||
2c74de8aef | |||
78498626ea | |||
b2299b149a | |||
885b2432a8 |
9348
package-lock.json
generated
9348
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smarttime",
|
"name": "@pushrocks/smarttime",
|
||||||
"private": false,
|
"private": false,
|
||||||
"version": "3.0.19",
|
"version": "3.0.24",
|
||||||
"description": "handle time in smart ways",
|
"description": "handle time in smart ways",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
@ -9,23 +9,24 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest ./test/)",
|
"test": "(tstest ./test/)",
|
||||||
"build": "(tsbuild && tsbundle npm)"
|
"build": "(tsbuild --web && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.11",
|
"@gitzone/tsbuild": "^2.1.11",
|
||||||
"@gitzone/tsbundle": "^1.0.69",
|
"@gitzone/tsbundle": "^1.0.72",
|
||||||
"@gitzone/tsrun": "^1.2.6",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.24",
|
"@gitzone/tstest": "^1.0.41",
|
||||||
"@pushrocks/tapbundle": "^3.0.9",
|
"@pushrocks/tapbundle": "^3.2.9",
|
||||||
"@types/node": "^14.0.5",
|
"@types/node": "^14.0.22",
|
||||||
"tslint": "^6.1.2",
|
"tslint": "^6.1.2",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.9",
|
"@pushrocks/lik": "^4.0.13",
|
||||||
|
"@pushrocks/smartdelay": "^2.0.10",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"croner": "^1.1.23",
|
"croner": "^1.1.23",
|
||||||
"dayjs": "^1.8.27",
|
"dayjs": "^1.8.29",
|
||||||
"is-nan": "^1.3.0"
|
"is-nan": "^1.3.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
@ -39,5 +40,8 @@
|
|||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
22
readme.md
22
readme.md
@ -8,13 +8,20 @@ handle time in smart ways
|
|||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smarttime/)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smarttime/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smarttime/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smarttime/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smarttime)
|
-- | --
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smarttime)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://prettier.io/)
|
Snyk | [](https://lossless.cloud)
|
||||||
|
TypeScript Support | [](https://lossless.cloud)
|
||||||
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
Code Style | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@ -44,7 +51,6 @@ import { ExtendedDate } from '@pushrocks/smarttime'
|
|||||||
const myDate: Date = ExtendedDate.fromEuropeanDate('8.6.2018')
|
const myDate: Date = ExtendedDate.fromEuropeanDate('8.6.2018')
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
|
|
||||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
27
test/test.cronmanager.browser.ts
Normal file
27
test/test.cronmanager.browser.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { tap, expect } from '@pushrocks/tapbundle';
|
||||||
|
|
||||||
|
import * as smarttime from '../ts/index';
|
||||||
|
|
||||||
|
let testCronManager: smarttime.CronManager;
|
||||||
|
|
||||||
|
tap.test('should create a valid instance of cronmanager', async () => {
|
||||||
|
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('*/2 * * * * *', () => {
|
||||||
|
if (counter === 10) {
|
||||||
|
done.resolve();
|
||||||
|
}
|
||||||
|
counter++;
|
||||||
|
console.log(`hey ${counter}`);
|
||||||
|
});
|
||||||
|
testCronManager.start();
|
||||||
|
await done.promise;
|
||||||
|
testCronManager.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
@ -9,7 +9,7 @@ tap.test('should create a valid instance of cronmanager', async () => {
|
|||||||
expect(testCronManager).to.be.instanceOf(smarttime.CronManager);
|
expect(testCronManager).to.be.instanceOf(smarttime.CronManager);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should create a valid cronJon', async tools => {
|
tap.test('should create a valid cronJon', async (tools) => {
|
||||||
const done = tools.defer();
|
const done = tools.defer();
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
testCronManager.addCronjob('*/2 * * * * *', () => {
|
testCronManager.addCronjob('*/2 * * * * *', () => {
|
||||||
|
@ -5,7 +5,7 @@ import * as smarttime from '../ts/index';
|
|||||||
// Test TimeStamp class
|
// Test TimeStamp class
|
||||||
let testTimeStamp: smarttime.TimeStamp;
|
let testTimeStamp: smarttime.TimeStamp;
|
||||||
let testTimeStamp2: smarttime.TimeStamp;
|
let testTimeStamp2: smarttime.TimeStamp;
|
||||||
tap.test('should create valid testTimeStamp instance', async tools => {
|
tap.test('should create valid testTimeStamp instance', async (tools) => {
|
||||||
testTimeStamp = new smarttime.TimeStamp();
|
testTimeStamp = new smarttime.TimeStamp();
|
||||||
await tools.delayFor(2);
|
await tools.delayFor(2);
|
||||||
testTimeStamp2 = new smarttime.TimeStamp();
|
testTimeStamp2 = new smarttime.TimeStamp();
|
||||||
@ -23,7 +23,7 @@ tap.test('should have valid linuxtime', async () => {
|
|||||||
let testHrtMeasurement: smarttime.HrtMeasurement;
|
let testHrtMeasurement: smarttime.HrtMeasurement;
|
||||||
|
|
||||||
// Test HrtMeasurement
|
// Test HrtMeasurement
|
||||||
tap.test('should create valid HrtMeasurements', async tools => {
|
tap.test('should create valid HrtMeasurements', async (tools) => {
|
||||||
testHrtMeasurement = new smarttime.HrtMeasurement();
|
testHrtMeasurement = new smarttime.HrtMeasurement();
|
||||||
testHrtMeasurement.start();
|
testHrtMeasurement.start();
|
||||||
await tools.delayFor(20);
|
await tools.delayFor(20);
|
||||||
@ -41,7 +41,7 @@ tap.test('should combine units', async () => {
|
|||||||
weeks: 2,
|
weeks: 2,
|
||||||
days: 2,
|
days: 2,
|
||||||
hours: 2,
|
hours: 2,
|
||||||
minutes: 2
|
minutes: 2,
|
||||||
});
|
});
|
||||||
// tslint:disable-next-line:no-console
|
// tslint:disable-next-line:no-console
|
||||||
console.log(computedTime);
|
console.log(computedTime);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export * from './smarttime.classes.cronmanager';
|
export * from './smarttime.classes.cronmanager';
|
||||||
|
export * from './smarttime.classes.cronjob';
|
||||||
export * from './smarttime.classes.extendeddate';
|
export * from './smarttime.classes.extendeddate';
|
||||||
export * from './smarttime.classes.hrtmeasurement';
|
export * from './smarttime.classes.hrtmeasurement';
|
||||||
export * from './smarttime.classes.interval';
|
export * from './smarttime.classes.interval';
|
||||||
|
@ -5,45 +5,59 @@ export class CronManager {
|
|||||||
public executionTimeout: plugins.smartdelay.Timeout<void>;
|
public executionTimeout: plugins.smartdelay.Timeout<void>;
|
||||||
|
|
||||||
public status: 'started' | 'stopped' = 'stopped';
|
public status: 'started' | 'stopped' = 'stopped';
|
||||||
public cronjobs: CronJob[] = [];
|
public cronjobs = new plugins.lik.ObjectMap<CronJob>();
|
||||||
|
|
||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
|
public addCronjob(cronIdentifierArg: string, cronFunctionArg: () => any) {
|
||||||
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
const newCronJob = new CronJob(this, cronIdentifierArg, cronFunctionArg);
|
||||||
this.cronjobs.push(newCronJob);
|
this.cronjobs.add(newCronJob);
|
||||||
if (this.status === 'started') {
|
if (this.status === 'started') {
|
||||||
newCronJob.start();
|
newCronJob.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return newCronJob;
|
||||||
|
}
|
||||||
|
|
||||||
|
public removeCronjob(cronjobArg: CronJob) {
|
||||||
|
cronjobArg.stop();
|
||||||
|
this.cronjobs.remove(cronjobArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* starts the cronjob
|
* starts the cronjob
|
||||||
*/
|
*/
|
||||||
public start() {
|
public start() {
|
||||||
this.status = 'started';
|
if (this.status !== 'started') {
|
||||||
for (const cronJob of this.cronjobs) {
|
this.status = 'started';
|
||||||
cronJob.start();
|
for (const cronJob of this.cronjobs.getArray()) {
|
||||||
}
|
cronJob.start();
|
||||||
this.executionTimeout = new plugins.smartdelay.Timeout(0);
|
}
|
||||||
|
this.executionTimeout = new plugins.smartdelay.Timeout(0);
|
||||||
|
|
||||||
// recursion
|
// recursion
|
||||||
const runCheckExecution = () => {
|
const runCheckExecution = () => {
|
||||||
console.log(`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`);
|
console.log(
|
||||||
this.executionTimeout.promise.then(() => {
|
`Next CronJob scheduled in ${this.executionTimeout.getTimeLeft()} milliseconds`
|
||||||
let timeToNextOverallExecution: number;
|
);
|
||||||
for (const cronJob of this.cronjobs) {
|
this.executionTimeout.promise.then(() => {
|
||||||
const timeToNextJobExecution = cronJob.checkExecution();
|
let timeToNextOverallExecution: number;
|
||||||
if (timeToNextJobExecution < timeToNextOverallExecution || !timeToNextOverallExecution) {
|
for (const cronJob of this.cronjobs.getArray()) {
|
||||||
timeToNextOverallExecution = timeToNextJobExecution;
|
const timeToNextJobExecution = cronJob.checkExecution();
|
||||||
|
if (
|
||||||
|
timeToNextJobExecution < timeToNextOverallExecution ||
|
||||||
|
!timeToNextOverallExecution
|
||||||
|
) {
|
||||||
|
timeToNextOverallExecution = timeToNextJobExecution;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution);
|
||||||
this.executionTimeout = new plugins.smartdelay.Timeout(timeToNextOverallExecution);
|
runCheckExecution();
|
||||||
runCheckExecution();
|
});
|
||||||
});
|
};
|
||||||
};
|
|
||||||
|
|
||||||
runCheckExecution();
|
runCheckExecution();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -52,7 +66,7 @@ export class CronManager {
|
|||||||
public stop() {
|
public stop() {
|
||||||
this.status = 'stopped';
|
this.status = 'stopped';
|
||||||
this.executionTimeout.cancel();
|
this.executionTimeout.cancel();
|
||||||
for (const cron of this.cronjobs) {
|
for (const cron of this.cronjobs.getArray()) {
|
||||||
cron.stop();
|
cron.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ export class ExtendedDate extends Date {
|
|||||||
'September',
|
'September',
|
||||||
'October',
|
'October',
|
||||||
'November',
|
'November',
|
||||||
'December'
|
'December',
|
||||||
];
|
];
|
||||||
const daysArray = [
|
const daysArray = [
|
||||||
'Monday',
|
'Monday',
|
||||||
@ -107,7 +107,7 @@ export class ExtendedDate extends Date {
|
|||||||
'Thursday',
|
'Thursday',
|
||||||
'Friday',
|
'Friday',
|
||||||
'Saturday',
|
'Saturday',
|
||||||
'Sunday'
|
'Sunday',
|
||||||
];
|
];
|
||||||
return {
|
return {
|
||||||
year: this.getFullYear(),
|
year: this.getFullYear(),
|
||||||
@ -118,7 +118,7 @@ export class ExtendedDate extends Date {
|
|||||||
day: this.getDate(),
|
day: this.getDate(),
|
||||||
dayString: ('0' + this.getDate()).slice(-2),
|
dayString: ('0' + this.getDate()).slice(-2),
|
||||||
dayOfTheWeek: this.getDay(),
|
dayOfTheWeek: this.getDay(),
|
||||||
dayOfTheWeekName: daysArray[this.getDay()]
|
dayOfTheWeekName: daysArray[this.getDay()],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
|
import * as lik from '@pushrocks/lik';
|
||||||
import * as smartdelay from '@pushrocks/smartdelay';
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
|
||||||
export { smartdelay, smartpromise };
|
export { lik, smartdelay, smartpromise };
|
||||||
|
|
||||||
// third parties
|
// third parties
|
||||||
import croner from 'croner';
|
import croner from 'croner';
|
||||||
|
@ -16,7 +16,7 @@ export let units = {
|
|||||||
},
|
},
|
||||||
minutes: (timesArg = 1) => {
|
minutes: (timesArg = 1) => {
|
||||||
return timesArg * 60000;
|
return timesArg * 60000;
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IUnitCombinationArg {
|
export interface IUnitCombinationArg {
|
||||||
@ -30,7 +30,7 @@ export interface IUnitCombinationArg {
|
|||||||
|
|
||||||
export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
|
export let getMilliSecondsFromUnits = (combinationArg: IUnitCombinationArg) => {
|
||||||
let timeInMilliseconds = 0;
|
let timeInMilliseconds = 0;
|
||||||
let addMilliSeconds = milliSecondsArg => {
|
let addMilliSeconds = (milliSecondsArg) => {
|
||||||
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
|
timeInMilliseconds = timeInMilliseconds + milliSecondsArg;
|
||||||
};
|
};
|
||||||
if (combinationArg.years) {
|
if (combinationArg.years) {
|
||||||
|
Reference in New Issue
Block a user