-
-
Notifications
You must be signed in to change notification settings - Fork 828
feat: add utils/delay
#547
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
Open
Planeshifter
wants to merge
43
commits into
stdlib-js:develop
Choose a base branch
from
Planeshifter:utils/delay
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 22 commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
3f0cadf
Add README.md
Planeshifter 04655c8
Add method and note
Planeshifter 9a75051
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 87112d6
Add README.md
Planeshifter 144f3ac
Add method and note
Planeshifter fe146fb
Scaffold utils/delay package files
stdlib-bot 391fc17
Apply suggestions from code review
kgryte 9f2d294
Add empty line
kgryte a1b2f5b
Fix missing line
kgryte 0dc9b83
Update copy
kgryte 717fa7b
Add link
kgryte 8b92d5e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter c3844f9
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 4eda8e9
Add links and duration string notes
Planeshifter 700d40f
Add note and CLI section
Planeshifter c10b642
Update copy
kgryte 9d38f2b
Remove stray link
kgryte c39c6e6
Update copy
kgryte 41ff87f
Update copy
kgryte 1be78fc
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 98b62fa
Merge remote-tracking branch 'upstream/scaffold-547/utils/delay' into…
Planeshifter d12b782
Merge branch 'utils/delay' of https://github.com/Planeshifter/stdlib …
Planeshifter 15d3103
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter cada44d
Update lib/node_modules/@stdlib/utils/delay/lib/main.js
Planeshifter c27c475
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter e4aaa60
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter e789233
Update lib/node_modules/@stdlib/utils/delay/test/test.js
Planeshifter 6d40619
Update lib/node_modules/@stdlib/utils/delay/docs/repl.txt
Planeshifter 041fea8
Update lib/node_modules/@stdlib/utils/delay/docs/types/index.d.ts
Planeshifter c06e0aa
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 3ec6a90
fix: address feedback from code review
Planeshifter a70cb93
test: comment out tests for now
Planeshifter 9625289
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter aa562df
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 0d927c5
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 19fa81a
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 44fafb5
chore: update copyright years
stdlib-bot f494d7f
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter ddd0741
Merge branch 'develop' into utils/delay
stdlib-bot a2eae7e
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 1e84184
Merge branch 'stdlib-js:develop' into utils/delay
Planeshifter 3ad1b47
Merge remote-tracking branch 'upstream/develop' into utils/delay
stdlib-bot 8faa10d
chore: format code in delay examples
Planeshifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
<!-- | ||
|
||
@license Apache-2.0 | ||
|
||
Copyright (c) 2022 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. | ||
|
||
#### 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 --> |
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/utils/delay/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2022 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 fn; | ||
var t; | ||
var i; | ||
|
||
fn = function beep() { | ||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log( 'beep' ); | ||
}; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
t = delay( fn, 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(); | ||
}); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 to invoke the function with. | ||
Planeshifter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Returns | ||
------- | ||
out: Object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
-------- |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.