What is last-run?
The 'last-run' npm package is primarily used to record and retrieve the last time a function was executed. This is particularly useful in build tools and task runners where it's important to know when a task was last run to optimize build processes and avoid unnecessary work.
What are last-run's main functionalities?
Recording last execution time
This feature allows you to capture the last time a specific task was executed. In this example, the 'last-run' package is used with Gulp to record the last execution time of 'example' task.
const lastRun = require('last-run');
const gulp = require('gulp');
function exampleTask(done) {
// task operations
done();
}
gulp.task('example', exampleTask);
lastRun.capture('example', Date.now());
Retrieving last execution time
This feature enables retrieving the last recorded execution time of a task. Here, the last execution time of the 'example' task is retrieved and logged.
const lastRun = require('last-run');
const lastExecutionTime = lastRun('example');
console.log('The last execution time of the example task was:', lastExecutionTime);
Other packages similar to last-run
date-fns
While 'date-fns' does not directly track function executions, it provides comprehensive date utilities that can be used to manipulate and format dates, which can be helpful in calculating time differences since the last run. It differs from 'last-run' as it's a general-purpose date utility library rather than specifically designed for tracking task executions.
moment
Similar to 'date-fns', 'moment' is another robust date handling library which can be used in conjunction with task management to track and display times. It does not inherently track the last run times but can be used to format and calculate time since last execution, offering broader functionality in date manipulation compared to 'last-run'.
last-run
Capture and retrieve the last time a function was run.
Usage
var lastRun = require('last-run');
function myFunc() {}
myFunc();
lastRun.capture(myFunc);
lastRun(myFunc);
API
lastRun(fn, [timeResolution]) => [Timestamp]
Takes a function (fn
) and returns a timestamp of the last time the function was captured.
Returns undefined if the function has not been captured.
The timestamp is always given in millisecond but the time resolution can be reduced (rounded down).
The use case is to be able to compare a build time to a file time attribute.
On some file systems, fs.stat
time attributes like mtime
might have one second precision.
lastRun.capture(fn, [timestamp])
Takes a function (fn
) and captures the current timestamp with Date.now()
.
If passed the optional timestamp, captures that time instead of Date.now()
.
The captured timestamp can then be retrieved using the lastRun
function.
lastRun.release(fn)
Takes a function (fn
) and removes the last run timestamp for it.
License
MIT