Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add utils/delay #547

Open
wants to merge 41 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3f0cadf
Add README.md
Planeshifter Oct 13, 2022
04655c8
Add method and note
Planeshifter Oct 13, 2022
9a75051
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Oct 13, 2022
87112d6
Add README.md
Planeshifter Oct 13, 2022
144f3ac
Add method and note
Planeshifter Oct 13, 2022
fe146fb
Scaffold utils/delay package files
stdlib-bot Oct 13, 2022
391fc17
Apply suggestions from code review
kgryte Oct 15, 2022
9f2d294
Add empty line
kgryte Oct 15, 2022
a1b2f5b
Fix missing line
kgryte Oct 15, 2022
0dc9b83
Update copy
kgryte Oct 15, 2022
717fa7b
Add link
kgryte Oct 15, 2022
8b92d5e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Oct 20, 2022
c3844f9
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 1, 2022
4eda8e9
Add links and duration string notes
Planeshifter Nov 1, 2022
700d40f
Add note and CLI section
Planeshifter Nov 1, 2022
c10b642
Update copy
kgryte Nov 2, 2022
9d38f2b
Remove stray link
kgryte Nov 2, 2022
c39c6e6
Update copy
kgryte Nov 2, 2022
41ff87f
Update copy
kgryte Nov 2, 2022
1be78fc
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 2, 2022
98b62fa
Merge remote-tracking branch 'upstream/scaffold-547/utils/delay' into…
Planeshifter Nov 2, 2022
d12b782
Merge branch 'utils/delay' of https://github.com/Planeshifter/stdlib …
Planeshifter Nov 2, 2022
15d3103
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 21, 2023
cada44d
Update lib/node_modules/@stdlib/utils/delay/lib/main.js
Planeshifter Feb 21, 2023
c27c475
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 21, 2023
e4aaa60
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 21, 2023
e789233
Update lib/node_modules/@stdlib/utils/delay/test/test.js
Planeshifter May 29, 2023
6d40619
Update lib/node_modules/@stdlib/utils/delay/docs/repl.txt
Planeshifter May 29, 2023
041fea8
Update lib/node_modules/@stdlib/utils/delay/docs/types/index.d.ts
Planeshifter May 29, 2023
c06e0aa
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Dec 10, 2023
3ec6a90
fix: address feedback from code review
Planeshifter Dec 10, 2023
a70cb93
test: comment out tests for now
Planeshifter Dec 10, 2023
9625289
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Dec 10, 2023
aa562df
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 24, 2024
0d927c5
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 24, 2024
19fa81a
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 24, 2024
44fafb5
chore: update copyright years
stdlib-bot Feb 24, 2024
f494d7f
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Feb 28, 2024
ddd0741
Merge branch 'develop' into utils/delay
stdlib-bot Nov 16, 2024
a2eae7e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 17, 2024
1e84184
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter Nov 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 289 additions & 0 deletions lib/node_modules/@stdlib/utils/delay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# delay

> Invoke a function after a specified duration.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var delay = require( '@stdlib/utils/delay' );
```

#### delay( fcn, duration\[, ...args] )

Invokes a function after a specified duration.

```javascript
function beep() {
console.log( 'beep' );
}

// Invoke a function after 3 seconds:
var t = delay( beep, 3000 );
// returns <Object>
```

The `duration` parameter may be specified in milliseconds or as a [duration string](#notes-duration-string) (e.g., `'5s'`, `'10m3s'`, `'2h'`, etc).

```javascript
function beep() {
console.log( 'beep' );
}

// Invoke a function after 3 seconds:
var t = delay( beep, '3s' );
// returns <Object>
```

The function supports providing additional arguments to pass through to the delayed function upon invocation. The delayed function receives additional arguments in the same order in which they are provided to `delay`.

```javascript
function add( x, y ) {
var sum = x + y;
console.log( 'sum: %d', sum );
}

var t = delay( add, 3000, 2, 3 );
// returns <Object>
```

The function returns a `timeout` object having properties and methods as documented below.
kgryte marked this conversation as resolved.
Show resolved Hide resolved

#### timeout.clear()

Clears a pending invocation of a function.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );
t.clear();
```

#### timeout.invoke()

Clears a pending invocation and invokes a function immediately.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );
t.invoke();
```

#### timeout.duration

Read-only value specifying the duration (in milliseconds) before invoking a delayed function.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );

var duration = t.duration;
kgryte marked this conversation as resolved.
Show resolved Hide resolved
// returns 3000
```

#### timeout.status

Read-only integer value indicating the status of a delayed function.

```javascript
function beep() {
console.log( 'beep' );
}
var t = delay( beep, 3000 );

var status = t.status;
kgryte marked this conversation as resolved.
Show resolved Hide resolved
// returns 0

t.clear();
status = t.status;
// returns 2
```

kgryte marked this conversation as resolved.
Show resolved Hide resolved
A status may be one of the following values:

- `0`: function invocation is pending.
- `1`: function invocation is complete.
- `2`: function invocation has been cleared.
kgryte marked this conversation as resolved.
Show resolved Hide resolved

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- In contrast to [`setTimeout`][mdn-settimeout] which returns a timeout identifier and requires passing this identifier to [`clearTimeout`][mdn-cleartimeout] in order to clear a pending invocation, the `delay` function returns a `timeout` object with additional properties and methods.

- Browsers internally limit the maximum duration of a timeout to 32-bit signed integers (approximately 24.8 days). If a duration exceeds this limit, the function will invoke a provided function after the maximum duration.

- A duration string is a string containing a sequence of time units. A time unit is a nonnegative integer followed by a unit identifier. The following unit identifiers are supported:

- `d`: days
- `h`: hours
- `m`: minutes
- `s`: seconds
- `ms`: milliseconds

For example, the string `1m3s10ms` is a duration string containing three time units: `1m` (1 minute), `3s` (3 seconds), and `10ms` (10 milliseconds). The string `60m` is a duration string containing a single time unit: `60m` (60 minutes).

- Duration strings are case insensitive. For example, the string `1M3S10MS` is equivalent to `1m3s10ms`.

kgryte marked this conversation as resolved.
Show resolved Hide resolved
</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var delay = require( '@stdlib/utils/delay' );

function beep() {
console.log( 'beep' );
}
function boop() {
console.log( 'boop' );
}
function baz() {
console.log( 'baz' );
}

var t1 = delay( beep, 3000 );
var t2 = delay( boop, 1000 );
var t3 = delay( baz, 4000 );

t2.clear();

// Wait 3 seconds...
// => beep

// Wait 1 more second...
// => baz
```

</section>

<!-- /.examples -->

* * *

<section class="cli">

## CLI

<section class="usage">

### Usage

```text
Usage: delay [options] <duration>

Options:

-h, --help Print this message.
-V, --version Print the package version.
```

</section>

<!-- /.usage -->

<section class="notes">

### Notes

- The process sleeps for a specified duration before exiting.
- The `duration` is specified in seconds or as a [duration string](#notes-duration-string) (e.g., `'5s'`, `'10m3s'`, `'2h'`, etc).

</section>

<!-- /.notes -->

<section class="examples">

### Examples

```bash
$ delay 5
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

kgryte marked this conversation as resolved.
Show resolved Hide resolved
[mdn-settimeout]: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout

[mdn-cleartimeout]: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout

</section>

<!-- /.links -->
52 changes: 52 additions & 0 deletions lib/node_modules/@stdlib/utils/delay/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var delay = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var t;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
t = delay( beep, 1000 );
if ( typeof t.status !== 'number' ) {
b.fail( 'should return a timeout object' );
}
}
b.toc();
if ( !isBoolean( t.status ) ) {
b.fail( 'should return a timeout object' );
}
b.pass( 'benchmark finished' );
b.end();

function beep() {
console.log( 'beep' );
}
});
37 changes: 37 additions & 0 deletions lib/node_modules/@stdlib/utils/delay/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

{{alias}}( fcn, wait[, ...args] )
Invokes a function after a specified number of milliseconds.

Parameters
----------
fcn: Function
Function to invoke.

wait: integer
Milliseconds to wait before invoking a function.

args: ...any
Additional arguments with which to invoke the function.

Returns
-------
out: Object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can document methods and properties here.

Function invocation timeout object.

Examples
--------
> function beep() { console.log( 'beep' ); };
> {{alias}}( beep, 3000 )
<timeout>
> var t = {{alias}}( beep, 3000 )
<timeout>
> t.clear()
> t.invoke()
beep
> t.duration
3000
> t.status
0

See Also
--------
Loading
Loading