Skip to content
This repository was archived by the owner on Jun 19, 2021. It is now read-only.

Commit 6160746

Browse files
committed
feat: implement basic Bot class
1 parent 637ce0c commit 6160746

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const configure = ({input, output, watch, ...others}) => ({
4141
input,
4242
output: Object.assign({
4343
format: "cjs",
44-
exports: "named"
44+
exports: "default"
4545
}, output),
4646
experimentalCodeSplitting: true,
4747
watch: {clearScreen: false},

src/event-types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ export interface IMessage {
22
readonly from_address: string
33
readonly text: string
44
readonly count: number
5-
readonly response: (text: string) => void
5+
response(text: string): void
66
}
77

88
export interface IBot {
99
init: void
1010
passphrase: void
11-
ready: (device: Device) => void
12-
message: (message: IMessage) => void
13-
error: (error: Error) => void
11+
ready(device: Device): void
12+
message(message: IMessage): void
13+
error(error: Error): void
1414
}

src/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
import {EventEmitter} from 'events'
2+
import Typed from 'strict-event-emitter-types'
23

3-
export default class Bot extends EventEmitter {}
4+
import HWallet from './event-handler'
5+
import {IBot} from './event-types'
6+
7+
type EventType<T> = Typed<EventEmitter, T>
8+
9+
// TODO: map to https://github.com/byteball/reddit-attestation/blob/master/index.js
10+
11+
export default class Bot extends (EventEmitter as { new(): EventType<IBot> }) {
12+
private _device?: Device
13+
private _wallet?: Wallet
14+
15+
constructor() {
16+
super()
17+
// #region still doubting 🤔 do I need to simplify this? (and how!)
18+
HWallet.started(() => { this.emit('init') })
19+
HWallet.onAsking('passphrase', () => { this.emit('passphrase') })
20+
// #endregion
21+
}
22+
23+
private async init() {
24+
this._wallet = await import('headless-byteball')
25+
this._device = await import('byteballcore/device')
26+
const eventBus = require('byteballcore/event_bus')
27+
return new Promise<EventEmitter>(async resolve => {
28+
eventBus.once('headless_wallet_ready', () => {
29+
this._wallet!.setupChatEventHandlers()
30+
this.emit('ready', this._device!)
31+
resolve(eventBus)
32+
})
33+
})
34+
}
35+
}

src/shim.d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1+
interface Wallet {
2+
setupChatEventHandlers()
3+
issueChangeAddressAndSendPayment(
4+
asset: string | null,
5+
amount: number,
6+
user_byteball_address: string,
7+
user_device_address?: string,
8+
calback: (err: Error, unit: number) => void
9+
)
10+
}
111
declare module 'headless-byteball' {
212
export const setupChatEventHandlers: any
13+
export const issueChangeAddressAndSendPayment: Wallet.issueChangeAddressAndSendPayment
314
}
415

516
declare module 'byteballcore/conf' {
617
export const deviceName: string
718
}
19+
20+
interface Device {
21+
sendMessageToDevice(
22+
device_address: string,
23+
subject: 'text', // TODO: get full subject type
24+
body: string,
25+
callbacks?: () => void,
26+
conn?: any // TODO: need to create dts of https://github.com/byteball/byteballcore/blob/master/db.js
27+
)
28+
}
29+
declare module 'byteballcore/device' {
30+
export const sendMessageToDevice: Device.sendMessageToDevice
31+
}
32+
33+
declare module 'byteballcore/event_bus' {
34+
import {EventEmitter} from 'events'
35+
const event: EventEmitter
36+
export default event
37+
}
38+
39+
declare module 'byteballcore/*' {
40+
const _: any
41+
export default _
42+
}

0 commit comments

Comments
 (0)