Skip to content

Commit

Permalink
fix: improve type definitions, mark tslib as dep (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Apr 26, 2021
1 parent 7dda512 commit e451004
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-toes-return.md
@@ -0,0 +1,5 @@
---
"synckit": patch
---

fix: improve type definitions, mark tslib as dep
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -39,13 +39,15 @@ const syncFn = createSyncFn(require.resolve('./worker'))

// do whatever you want, you will get the result synchronously!
const result = syncFn(...args)
```

```js
// worker.js
import { runAsWorker } from 'synckit'

runAsWorker(async (...args) => {
// do expensive work
// but you mush mark sure the `result` is serializable by `JSON.stringify`
// but you must make sure the `result` is serializable by `JSON.stringify`
return result
})
```
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -41,6 +41,7 @@
"typecov": "type-coverage"
},
"dependencies": {
"tslib": "^2.2.0",
"uuid": "^8.3.2"
},
"devDependencies": {
Expand All @@ -52,6 +53,7 @@
"@types/uuid": "^8.3.0",
"clean-publish": "^2.1.1",
"npm-run-all": "^4.1.5",
"ts-expect": "^1.3.0",
"ts-jest": "^26.5.5",
"ts-node": "^9.1.1",
"type-coverage": "^2.17.3",
Expand Down
22 changes: 11 additions & 11 deletions src/index.ts
Expand Up @@ -5,6 +5,10 @@ import fs from 'fs'

import { v4 as uuid } from 'uuid'

import { AnyAsyncFn, AnyFn, Syncify } from './types'

export * from './types'

/**
* @link https://github.com/sindresorhus/temp-dir/blob/main/index.js#L9
*/
Expand All @@ -30,12 +34,12 @@ const isTsconfigPathsAvailable = () => {
return tsconfigPathsAvailable
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const syncFnCache = new Map<string, (...args: any) => any>()
const syncFnCache = new Map<string, AnyFn>()

export const createSyncFn = <T extends (...args: unknown[]) => unknown>(
export function createSyncFn<T extends AnyAsyncFn>(
workerPath: string,
) => {
): Syncify<T>
export function createSyncFn<R>(workerPath: string) {
if (!path.isAbsolute(workerPath)) {
throw new Error('`workerPath` must be absolute')
}
Expand All @@ -59,7 +63,7 @@ export const createSyncFn = <T extends (...args: unknown[]) => unknown>(
: /* istanbul ignore next */ '')
: 'node'

const syncFn = (...args: Parameters<T>): ReturnType<T> => {
const syncFn = (...args: unknown[]): R => {
const filename = path.resolve(tmpdir, `synckit-${uuid()}.json`)

fs.writeFileSync(filename, JSON.stringify(args))
Expand All @@ -83,13 +87,9 @@ export const createSyncFn = <T extends (...args: unknown[]) => unknown>(
return syncFn
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const runAsWorker = async <R, T extends (...args: any[]) => Promise<R>>(
fn: T,
) => {
export const runAsWorker = async <T extends AnyAsyncFn>(fn: T) => {
const filename = process.argv[2]
const content = fs.readFileSync(filename, 'utf-8')
const options = JSON.parse(content) as Parameters<T>
const result = await fn(...options)
fs.writeFileSync(filename, JSON.stringify(result))
fs.writeFileSync(filename, JSON.stringify(await fn(...options)))
}
18 changes: 18 additions & 0 deletions src/types.ts
@@ -0,0 +1,18 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyFn<T = any, R extends any[] = any[]> = (...args: R) => T

// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-type-alias
export type AnyPromise = Promise<any>

// eslint-disable-next-line @typescript-eslint/no-type-alias
export type AnyAsyncFn = AnyFn<AnyPromise>

export type Syncify<T extends AnyFn<AnyPromise>> = T extends (
...args: infer Args
) => Promise<infer R>
? (...args: Args) => R
: never

export type PromiseType<T extends AnyPromise> = T extends Promise<infer R>
? R
: never
33 changes: 33 additions & 0 deletions test/types-d.ts
@@ -0,0 +1,33 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { TypeEqual, expectType } from 'ts-expect'

import {
AnyPromise,
createSyncFn,
PromiseType,
runAsWorker,
Syncify,
} from 'synckit'

// @ts-expect-error
expectType<Syncify<() => 1>>(true)

expectType<TypeEqual<Syncify<() => Promise<true>>, () => true>>(true)
expectType<TypeEqual<Syncify<() => AnyPromise>, () => PromiseType<AnyPromise>>>(
true,
)
expectType<TypeEqual<Syncify<() => Promise<never>>, () => never>>(true)

// @ts-expect-error
createSyncFn<() => 0>('')

// @ts-expect-error
expectType<() => true>(createSyncFn<() => Promise<1>>(''))

expectType<() => true>(createSyncFn<() => Promise<true>>(''))
expectType<() => true>(createSyncFn<() => Promise<never>>(''))

// @ts-expect-error
runAsWorker(() => 1)

runAsWorker<() => Promise<number>>(() => Promise.resolve(1))
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -9615,6 +9615,11 @@ trough@^1.0.0:
resolved "https://registry.yarnpkg.com/trough/-/trough-1.0.5.tgz#b8b639cefad7d0bb2abd37d433ff8293efa5f406"
integrity sha512-rvuRbTarPXmMb79SmzEp8aqXNKcK+y0XaB298IXueQ8I2PsrATcPBCSPyK/dDNa2iWOhKlfNnOjdAOTBU/nkFA==

ts-expect@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/ts-expect/-/ts-expect-1.3.0.tgz#3f8d3966e0e22b5e2bb88337eb99db6816a4c1cf"
integrity sha512-e4g0EJtAjk64xgnFPD6kTBUtpnMVzDrMb12N1YZV0VvSlhnVT3SGxiYTLdGy8Q5cYHOIC/FAHmZ10eGrAguicQ==

ts-jest@^26.5.5:
version "26.5.5"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-26.5.5.tgz#e40481b6ee4dd162626ba481a2be05fa57160ea5"
Expand Down

0 comments on commit e451004

Please sign in to comment.