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

🐣 tymeout: init #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"packages/stroki",
"packages/syntx",
"packages/tsfn",
"packages/tymeout",
"packages/typeon",
"packages/unchunk",
"packages/weslint",
Expand Down
22 changes: 22 additions & 0 deletions packages/tymeout/LICENSE
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.
27 changes: 27 additions & 0 deletions packages/tymeout/package.json
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"
}
}
136 changes: 136 additions & 0 deletions packages/tymeout/readme.md
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
```
5 changes: 5 additions & 0 deletions packages/tymeout/src/index.ts
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'
25 changes: 25 additions & 0 deletions packages/tymeout/src/ping.ts
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)
2 changes: 2 additions & 0 deletions packages/tymeout/src/promise.ts
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'
3 changes: 3 additions & 0 deletions packages/tymeout/src/types.ts
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
8 changes: 8 additions & 0 deletions packages/tymeout/src/wait-promise.ts
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)
3 changes: 3 additions & 0 deletions packages/tymeout/src/wait-time-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { waitPromise } from './wait-promise'

export const waitTimePromise = waitPromise()
3 changes: 3 additions & 0 deletions packages/tymeout/src/wait-time.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { wait } from './wait'

export const waitTime = wait()
14 changes: 14 additions & 0 deletions packages/tymeout/src/wait.ts
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)
71 changes: 71 additions & 0 deletions packages/tymeout/test/ping.ts
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()
})
Loading