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

Commit 7b2710f

Browse files
committed
feat(lib): able to setoption before instantiates
1 parent b6c2729 commit 7b2710f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/event-types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ export interface IBot {
1212
message(message: IMessage): void
1313
error(error: Error): void
1414
}
15+
16+
import conf from 'byteballcore/conf'
17+
import constants from 'byteballcore/constants'
18+
export interface IOption {
19+
testnet: boolean
20+
constants: typeof constants
21+
conf: typeof conf
22+
}

src/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ import {EventEmitter} from 'events'
22
import Typed from 'strict-event-emitter-types'
33

44
import HWallet from './event-handler'
5-
import {IBot} from './event-types'
5+
import {IBot, IOption} from './event-types'
6+
import setOption from './options'
67

78
type EventType<T> = Typed<EventEmitter, T>
89

910
// TODO: map to https://github.com/byteball/reddit-attestation/blob/master/index.js
1011

11-
export default class Bot extends (EventEmitter as { new(): EventType<IBot> }) {
12+
export class Bot extends (EventEmitter as { new(): EventType<IBot> }) {
13+
static get instance() { return HWallet }
14+
static setOption = setOption
15+
1216
private _device?: Device
1317
private _wallet?: Wallet
1418

15-
constructor() {
19+
constructor(option?: Partial<IOption>) {
1620
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+
if (option) Bot.setOption(option)
2122
}
2223

2324
private async init() {
25+
/// use of `await import` is for module thaat immediately run a service 😓
2426
this._wallet = await import('headless-byteball')
2527
this._device = await import('byteballcore/device')
2628
const eventBus = require('byteballcore/event_bus')

src/options.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import conf from 'byteballcore/conf'
2+
import constants from 'byteballcore/constants'
3+
4+
import {IOption} from './event-types'
5+
6+
export default function (option: Partial<IOption>) {
7+
if (option.testnet) {
8+
// #region @see https://github.com/byteball/bot-example/blob/master/testnetify.sh#L2
9+
constants.version = '1.0t'
10+
constants.alt = '2'
11+
// #endregion
12+
conf.hub = 'byteball.org/bb-test'
13+
conf.ports = 16611
14+
/// Anything else❓
15+
}
16+
Object.assign(constants, option.constants)
17+
Object.assign(conf, option.conf)
18+
}

0 commit comments

Comments
 (0)