Skip to content

Commit

Permalink
Refactor export names (#169)
Browse files Browse the repository at this point in the history
* workaround for (#168)

* refactor export names

* import at the root mod (#168)

* fix pack testing

* 0.53.2
  • Loading branch information
huan authored Oct 27, 2021
1 parent 3965e2d commit 4d5bd18
Show file tree
Hide file tree
Showing 21 changed files with 368 additions and 320 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "wechaty-puppet",
"version": "0.53.1",
"version": "0.53.2",
"description": "Abstract Puppet for Wechaty",
"type": "module",
"exports": {
".": {
"import": "./dist/esm/src/mod.js",
"require": "./dist/cjs/src/mod.js"
"import": "./dist/esm/src/mods/mod.js",
"require": "./dist/cjs/src/mods/mod.js"
}
},
"typings": "./dist/esm/src/mod.d.ts",
"typings": "./dist/esm/src/mods/mod.d.ts",
"engines": {
"node": ">=16",
"npm": ">=7",
"wechaty": ">=0.68"
"wechaty": ">=0.79"
},
"scripts": {
"clean": "shx rm -fr dist/*",
Expand Down Expand Up @@ -48,10 +48,10 @@
},
"homepage": "https://github.com/wechaty/wechaty-puppet#readme",
"devDependencies": {
"@chatie/eslint-config": "^0.16.2",
"@chatie/eslint-config": "^1.0.2",
"@chatie/git-scripts": "^0.6.2",
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^0.20.6",
"@chatie/tsconfig": "^1.0.2",
"@types/uuid": "^8.3.1",
"rxjs": "^7.4.0"
},
Expand All @@ -62,9 +62,9 @@
"file-box": "^1.0.4",
"memory-card": "^0.12.2",
"state-switch": "^1.1.8",
"tstest": "^0.7.3",
"typed-emitter": "^1.3.1",
"uuid": "^3.4.0",
"tstest": "^1.0.1",
"typed-emitter": "^1.4.0",
"uuid": "^8.3.2",
"watchdog": "^0.9.2"
},
"files": [
Expand Down
29 changes: 29 additions & 0 deletions src/helpers/wrap-async-error.spec.ts
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')
})
50 changes: 50 additions & 0 deletions src/helpers/wrap-async-error.ts
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,
}
12 changes: 5 additions & 7 deletions src/mixins/validate-mixin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type {
Puppet,
PuppetInterface,
} from '../puppet/mod.js'

import type { PuppetSkelton } from '../puppet/puppet-skelton.js'
import {
interfaceOfPuppet,
looseInstanceOfPuppet,
} from '../puppet/interface-of.js'
} from '../puppet/interface-of.js'

import type { Puppet } from '../puppet/puppet-abstract.js'
import type { PuppetInterface } from '../puppet/interface.js'
import type { PuppetSkelton } from '../puppet/puppet-skelton.js'

const validateMixin = <MixinBase extends typeof PuppetSkelton>(mixinBase: MixinBase) => {

Expand Down
164 changes: 0 additions & 164 deletions src/mod.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/mods/mod-helper.ts
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,
}
15 changes: 15 additions & 0 deletions src/mods/mod-impl.ts
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,
}
Loading

0 comments on commit 4d5bd18

Please sign in to comment.