-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
572 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Alex Feinstein | ||
Copyright (c) 2020 NexTools | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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,27 @@ | ||
{ | ||
"name": "tymeout", | ||
"version": "0.0.0", | ||
"description": "Functional setTimeout and setInterval", | ||
"keywords": [ | ||
"setTimeout", | ||
"setInterval", | ||
"promise", | ||
"timeout", | ||
"functional" | ||
], | ||
"engines": { | ||
"node": ">=10.13.0" | ||
}, | ||
"sideEffects": false, | ||
"main": "src/index.ts", | ||
"private": false, | ||
"license": "MIT", | ||
"author": "psxcode <[email protected]> (https://github.com/psxcode)", | ||
"repository": "nextools/tymeout", | ||
"devDependencies": { | ||
"@types/tape": "^4.2.34", | ||
"spyfn": "^1.0.0", | ||
"tape": "^5.0.0-next.5", | ||
"time-test": "^0.1.0" | ||
} | ||
} |
This file contains 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,136 @@ | ||
# Wait | ||
Functional `setTimeout` and `setInterval`. | ||
|
||
### Install | ||
```ts | ||
npm install tymeout | ||
``` | ||
|
||
### `wait` | ||
`(timeGetter?: () => number) => (cb: () => void) => (ms?: number) => () => void` | ||
```ts | ||
import { wait } from 'tymeout' | ||
|
||
// Signature | ||
(timeGetter?: () => number) => // optional timeGetter, defaults to () => 0 | ||
(callback: () => void) => // callback | ||
(ms?: number) => // optional timeout ms, defaults to timeGetter() | ||
() => void // returns cancel function | ||
``` | ||
Usage with `timeGetter`: | ||
```ts | ||
const timeGetter = () => Math.random() * 1000 | ||
|
||
// create waiter function | ||
const waiter = wait(timeGetter)(callback) | ||
|
||
// invoke waiter | ||
const cancel = waiter() // timeout is taken from timeGetter | ||
|
||
// clear timeout | ||
cancel() | ||
``` | ||
Usage with milliseconds: | ||
```ts | ||
// create waiter function, skip timeGetter | ||
const waiter = wait()(callback) | ||
|
||
// invoke waiter | ||
const cancel = waiter(1000) // provide time | ||
|
||
// clear timeout | ||
cancel() | ||
``` | ||
|
||
### `wait-promise` | ||
`(timeGetter?: () => number) => (ms?: number) => Promise<void>` | ||
```ts | ||
import { waitPromise } from 'tymeout' | ||
|
||
// Signature | ||
(timeGetter: () => number) => // optional timeGetter, defaults to () => 0 | ||
(ms = timeGetter()) => // optional timeout ms, defaults to timeGetter() | ||
Promise<void> // returns Promise | ||
``` | ||
Usage with `timeGetter`: | ||
```ts | ||
const timeGetter = () => Math.random() * 1000 | ||
|
||
// create waiter function | ||
const waiter = waitPromise(timeGetter) | ||
|
||
// invoke waiter | ||
await waiter() // timeout is taken from timeGetter | ||
``` | ||
Usage with milliseconds: | ||
```ts | ||
// create waiter function, skip timeGetter | ||
const waiter = waitPromise() | ||
|
||
// invoke waiter | ||
await waiter(1000) // provide time | ||
``` | ||
|
||
### `ping` | ||
`(timeGetter: () => number) => (cb: () => void) => () => () => void` | ||
```ts | ||
import { ping } from 'tymeout' | ||
|
||
// Signature | ||
(timeGetter: () => number) => // timeGetter | ||
(callback: () => void) => // callback | ||
() => // invoke to run | ||
() => void // returns cancel function | ||
``` | ||
Usage: | ||
```ts | ||
const timeGetter = () => Math.random() * 1000 | ||
|
||
// create pinger function | ||
const pinger = ping(timeGetter)(callback) | ||
|
||
// run pinger | ||
const cancel = pinger() // returns cancel function | ||
|
||
// cancel ping | ||
cancel() | ||
``` | ||
|
||
### `wait-time` | ||
`(cb: () => void) => (ms: number) => () => void` | ||
Same as `wait`, but without `timeGetter` | ||
```ts | ||
import { waitTime } from 'tymeout' | ||
|
||
// Signature | ||
(callback: () => void) => // provide callback | ||
(ms: number) => // provide timeout ms | ||
() => void // returns cancel function | ||
``` | ||
Usage: | ||
```ts | ||
// create waiter function | ||
const waiter = waitTime(callback) | ||
|
||
// invoke waiter | ||
const cancel = waiter(1000) // provide time | ||
|
||
// clear timeout | ||
cancel() | ||
``` | ||
|
||
### `wait-time-promise` | ||
`(ms: number) => Promise<void>` | ||
Same as `wait-promise`, but without `timeGetter` | ||
```ts | ||
import { waitTimePromise } from 'tymeout' | ||
|
||
// Signature | ||
(ms: number) => // provide timeout ms | ||
Promise<void> // returns Promise | ||
``` | ||
Usage: | ||
```ts | ||
// invoke waiter | ||
await waitTimePromise(1000) // provide time | ||
``` |
This file contains 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,5 @@ | ||
export { wait } from './wait' | ||
export { waitPromise } from './wait-promise' | ||
export { waitTime } from './wait-time' | ||
export { waitTimePromise } from './wait-time-promise' | ||
export { ping } from './ping' |
This file contains 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,25 @@ | ||
import { waitFactory } from './wait' | ||
import { ClearTimeoutFn, SetTimeoutFn } from './types' | ||
|
||
export const pingFactory = (setTimeout: SetTimeoutFn, clearTimeout: ClearTimeoutFn) => { | ||
const waitFn = waitFactory(setTimeout, clearTimeout) | ||
|
||
return (timeGetter: () => number) => | ||
(cb: () => void) => { | ||
let unsub: () => void | ||
const wait = waitFn(timeGetter)(() => { | ||
cb() | ||
unsub = wait() | ||
}) | ||
|
||
return () => { | ||
unsub = wait() | ||
|
||
return () => { | ||
unsub() | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const ping = pingFactory(setTimeout, clearTimeout) |
This file contains 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,2 @@ | ||
export { waitPromise as wait } from './wait-promise' | ||
export { waitTimePromise as waitTime } from './wait-time-promise' |
This file contains 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,3 @@ | ||
export type TimeoutId = any | ||
export type SetTimeoutFn = (cb: () => void, ms: number) => TimeoutId | ||
export type ClearTimeoutFn = (id: TimeoutId) => void |
This file contains 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,8 @@ | ||
import { SetTimeoutFn } from './types' | ||
|
||
export const waitPromiseFactory = (setTimeout: SetTimeoutFn) => | ||
(timeGetter = () => 0) => | ||
(ms = timeGetter()): Promise<void> => | ||
new Promise((resolve) => setTimeout(() => resolve(), ms)) | ||
|
||
export const waitPromise = waitPromiseFactory(setTimeout) |
This file contains 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,3 @@ | ||
import { waitPromise } from './wait-promise' | ||
|
||
export const waitTimePromise = waitPromise() |
This file contains 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,3 @@ | ||
import { wait } from './wait' | ||
|
||
export const waitTime = wait() |
This file contains 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,14 @@ | ||
import { ClearTimeoutFn, SetTimeoutFn } from './types' | ||
|
||
export const waitFactory = (setTimeout: SetTimeoutFn, clearTimeout: ClearTimeoutFn) => | ||
(timeGetter = () => 0) => | ||
(cb: () => void) => | ||
(ms = timeGetter()) => { | ||
const id = setTimeout(cb, ms) | ||
|
||
return () => { | ||
clearTimeout(id) | ||
} | ||
} | ||
|
||
export const wait = waitFactory(setTimeout, clearTimeout) |
This file contains 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,71 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import test from 'tape' | ||
import { createSpy, getSpyCalls } from 'spyfn' | ||
import { tickTimeout, makeSetTimeoutContext, makeSetTimeout, makeClearTimeout, getSetTimeoutCalls, getClearTimeoutCalls } from 'time-test' | ||
import { pingFactory } from '../src/ping' | ||
|
||
test('ping', (t) => { | ||
const timeoutGetter = createSpy(((i = 0, v = [100, 200, 300]) => () => v[i++])()) | ||
const cb = createSpy(() => {}) | ||
|
||
const timeContext = makeSetTimeoutContext() | ||
const tick = tickTimeout(timeContext) | ||
|
||
const pinger = pingFactory( | ||
makeSetTimeout(timeContext), | ||
makeClearTimeout(timeContext) | ||
)(timeoutGetter)(cb) | ||
|
||
// Begin pinging | ||
const unsub = pinger() | ||
|
||
t.deepEquals( | ||
getSetTimeoutCalls(timeContext), | ||
[ | ||
{ delay: 100 }, | ||
], | ||
'should call setInterval' | ||
) | ||
|
||
tick(50) | ||
|
||
t.deepEquals( | ||
getSpyCalls(cb), | ||
[], | ||
'should not call spy' | ||
) | ||
|
||
tick() | ||
tick() | ||
unsub() | ||
tick() | ||
|
||
t.deepEquals( | ||
getSetTimeoutCalls(timeContext), | ||
[ | ||
{ delay: 100 }, | ||
{ delay: 200 }, | ||
{ delay: 300 }, | ||
], | ||
'should call setTimeout' | ||
) | ||
|
||
t.deepEquals( | ||
getClearTimeoutCalls(timeContext), | ||
[ | ||
{ id: 2 }, | ||
], | ||
'should call clearTimeout' | ||
) | ||
|
||
t.deepEquals( | ||
getSpyCalls(cb), | ||
[ | ||
[], | ||
[], | ||
], | ||
'should call spy' | ||
) | ||
|
||
t.end() | ||
}) |
Oops, something went wrong.