Skip to content

Commit

Permalink
🐣 tymeout: init
Browse files Browse the repository at this point in the history
  • Loading branch information
psxcode committed Apr 10, 2020
1 parent b862041 commit 94ce3ed
Show file tree
Hide file tree
Showing 14 changed files with 434 additions and 0 deletions.
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"
}
}
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)
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()
})
86 changes: 86 additions & 0 deletions packages/tymeout/test/wait-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* eslint-disable no-param-reassign */
import test from 'tape'
import { createSpy } from 'spyfn'
import { makeSetTimeoutContext, tickTimeout, makeSetTimeout, getSetTimeoutCalls, getClearTimeoutCalls } from 'time-test'
import { waitPromiseFactory } from '../src/wait-promise'

test('waitPromise', async (t) => {
const timeoutGetter = createSpy(((i = 0, v = [100, 200, 300]) => () => v[i++])())

const timeContext = makeSetTimeoutContext()
const tick = tickTimeout(timeContext)

const waiter = waitPromiseFactory(
makeSetTimeout(timeContext)
)(timeoutGetter)

// Use getter value
const p1 = waiter()
// Use explicit value
const p2 = waiter(500)

t.deepEquals(
getSetTimeoutCalls(timeContext),
[
{ delay: 100 },
{ delay: 500 },
],
'should call setTimeout'
)

t.deepEquals(
getClearTimeoutCalls(timeContext),
[],
'should not call clearTimeout'
)

tick()

// Promise should be now resolved
await p1

tick()

// Promise should be now resolved
await p2
})

test('waitPromise; no getter', async (t) => {
const timeContext = makeSetTimeoutContext()
const tick = tickTimeout(timeContext)

const waiter = waitPromiseFactory(
makeSetTimeout(timeContext)
)()

// Use getter value
const p1 = waiter()
// Use explicit value
const p2 = waiter(500)

t.deepEquals(
getSetTimeoutCalls(timeContext),
[
{ delay: 0 },
{ delay: 500 },
],
'should call setTimeout'
)

t.deepEquals(
getClearTimeoutCalls(timeContext),
[],
'should not call clearTimeout'
)

tick()

// Promise should be now resolved
await p1

tick()

// Promise should be now resolved
await p2
})

Loading

0 comments on commit 94ce3ed

Please sign in to comment.