-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* workaround for (#168) * refactor export names * import at the root mod (#168) * fix pack testing * 0.53.2
- Loading branch information
Showing
21 changed files
with
368 additions
and
320 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,29 @@ | ||
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm | ||
|
||
import { | ||
test, | ||
sinon, | ||
} from 'tstest' | ||
|
||
import { | ||
wrapAsyncError, | ||
} from './wrap-async-error.js' | ||
|
||
test('wrapAsyncError() smoke testing', async t => { | ||
const spy = sinon.spy() | ||
|
||
const wrapAsync = wrapAsyncError(spy) | ||
|
||
const DATA = 'test' | ||
const promise = Promise.resolve(DATA) | ||
const wrappedPromise = wrapAsync(promise) | ||
t.equal(await wrappedPromise, undefined, 'should resolve Promise<any> to void') | ||
|
||
const rejection = Promise.reject(new Error('test')) | ||
const wrappedRejection = wrapAsync(rejection) | ||
t.equal(wrappedRejection, undefined, 'should be void and not to reject') | ||
|
||
t.equal(spy.callCount, 0, 'should have no error before sleep') | ||
await new Promise(resolve => setImmediate(resolve)) // wait async event loop task to be executed | ||
t.equal(spy.callCount, 1, 'should emit error when promise reject with error') | ||
}) |
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,50 @@ | ||
/** | ||
* Wrap promise in sync way (catch error by emitting it) | ||
* 1. convert a async callback function to be sync function | ||
* by catcing any errors and emit them to error event | ||
* 2. wrap a Promise by catcing any errors and emit them to error event | ||
*/ | ||
interface WrapAsync { | ||
|
||
(promise: Promise<any>): void | ||
|
||
<T extends (...args: any[]) => Promise<any>> ( | ||
asyncFunction: T, | ||
): (...args: Parameters<T>) => void | ||
|
||
} | ||
|
||
type OnErrorCallback = (error: any) => void | ||
|
||
const wrapAsyncError: (onError: OnErrorCallback) => WrapAsync = onError => <T extends (...args: any[]) => Promise<any>> ( | ||
asyncStaff: T | Promise<any>, | ||
) => { | ||
/** | ||
* 1. Promise | ||
*/ | ||
if (asyncStaff instanceof Promise) { | ||
asyncStaff | ||
.then(_ => _) | ||
.catch(e => onError(e)) | ||
/** | ||
* Huan(202110) FIXME: | ||
* The same code works inside the Wechaty/Puppet class | ||
* but here we have to use `as any` to bypass the type check | ||
*/ | ||
return undefined as any | ||
} | ||
|
||
/** | ||
* 2. Function | ||
*/ | ||
return function (this: any, ...args: Parameters<T>): void { | ||
asyncStaff.apply(this, args).catch(onError) | ||
} | ||
} | ||
|
||
export type { | ||
WrapAsync, | ||
} | ||
export { | ||
wrapAsyncError, | ||
} |
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 was deleted.
Oops, something went wrong.
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 { | ||
FileBox, | ||
} from 'file-box' | ||
import { | ||
MemoryCard, | ||
} from 'memory-card' | ||
import { | ||
StateSwitch, | ||
} from 'state-switch' | ||
|
||
import { | ||
wrapAsyncError, | ||
} from '../helpers/wrap-async-error.js' | ||
|
||
import { | ||
GError, | ||
} from '../gerror/mod.js' | ||
|
||
export { | ||
FileBox, | ||
MemoryCard, | ||
StateSwitch, | ||
wrapAsyncError, | ||
GError, | ||
} |
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,15 @@ | ||
import type { | ||
PuppetConstructor, | ||
PuppetInterface as Puppet, | ||
} from '../puppet/mod.js' | ||
import { | ||
Puppet as PuppetAbstract, | ||
} from '../puppet/mod.js' | ||
|
||
export type { | ||
PuppetConstructor, | ||
Puppet, | ||
} | ||
export { | ||
PuppetAbstract, | ||
} |
Oops, something went wrong.