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

Commit 549583d

Browse files
authored
Merge pull request #493 from lightninglabs/jsdoc
Jsdoc
2 parents 19729dc + 64e804b commit 549583d

File tree

14 files changed

+530
-3
lines changed

14 files changed

+530
-3
lines changed

src/action/app-storage.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileOverview repesents the local storage database on a user's device
3+
* which can be used to persist user settings on disk.
4+
*/
5+
16
import * as log from './log';
27

38
class AppStorage {
@@ -6,6 +11,12 @@ class AppStorage {
611
this._AsyncStorage = AsyncStorage;
712
}
813

14+
/**
15+
* Read the user settings from disk and set them accordingly in the
16+
* application state. After the state has bee read to the global
17+
* `store` instance `store.loaded` is set to true.
18+
* @return {Promise<undefined>}
19+
*/
920
async restore() {
1021
try {
1122
const stateString = await this._AsyncStorage.getItem('settings');
@@ -24,6 +35,11 @@ class AppStorage {
2435
}
2536
}
2637

38+
/**
39+
* Persist the user settings to disk so that they may be read the
40+
* next time the application is opened by the user.
41+
* @return {Promise<undefined>}
42+
*/
2743
async save() {
2844
try {
2945
const state = JSON.stringify(this._store.settings);
@@ -34,6 +50,11 @@ class AppStorage {
3450
}
3551
}
3652

53+
/**
54+
* Delete all of the data in local storage completely. Should be used
55+
* carefully e.g. when the user wants to wipe the data on disk.
56+
* @return {Promise<undefined>}
57+
*/
3758
async clear() {
3859
try {
3960
await this._AsyncStorage.clear();

src/action/channel.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* @fileOverview actions to set channel state within the app and to
3+
* call the corresponding GRPC apis for channel management.
4+
*/
5+
16
import { toSatoshis, parseSat } from '../helper';
27
import * as log from './log';
38

@@ -13,16 +18,31 @@ class ChannelAction {
1318
// Create channel actions
1419
//
1520

21+
/**
22+
* Initiate the create channel view by resetting input values
23+
* and then navigating to the view.
24+
* @return {undefined}
25+
*/
1626
initCreate() {
1727
this._store.channel.pubkeyAtHost = '';
1828
this._store.channel.amount = '';
1929
this._nav.goChannelCreate();
2030
}
2131

32+
/**
33+
* Set the amount input for the create channel view. This amount
34+
* is either in btc or fiat depending on user settings.
35+
* @param {string} options.amount The string formatted number
36+
*/
2237
setAmount({ amount }) {
2338
this._store.channel.amount = amount;
2439
}
2540

41+
/**
42+
* Set the channel public key and hostname in a single variable
43+
* which can be parsed before calling the create channel grpc api.
44+
* @param {string} options.pubkeyAtHost The combined public key and host
45+
*/
2646
setPubkeyAtHost({ pubkeyAtHost }) {
2747
this._store.channel.pubkeyAtHost = pubkeyAtHost;
2848
}
@@ -31,17 +51,33 @@ class ChannelAction {
3151
// Channel list actions
3252
//
3353

54+
/**
55+
* Initiate the channel list view by navigating to the view and updating
56+
* the app's channel state by calling all necessary grpc apis.
57+
* @return {undefined}
58+
*/
3459
init() {
3560
this._nav.goChannels();
3661
this.update();
3762
}
3863

64+
/**
65+
* Select a channel item from the channel list view and then navigate
66+
* to the detail view to list channel parameters.
67+
* @param {Object} options.item The selected channel object
68+
* @return {undefined}
69+
*/
3970
select({ item }) {
4071
this._store.selectedChannel = item;
4172
this._nav.goChannelDetail();
4273
this.update();
4374
}
4475

76+
/**
77+
* Update the peers, channels, and pending channels in the app state
78+
* by querying all required grpc apis.
79+
* @return {Promise<undefined>}
80+
*/
4581
async update() {
4682
await Promise.all([
4783
this.getPeers(),
@@ -50,6 +86,15 @@ class ChannelAction {
5086
]);
5187
}
5288

89+
//
90+
// Generic channel actions
91+
//
92+
93+
/**
94+
* List the open channels by calling the respective grpc api and updating
95+
* the channels array in the global store.
96+
* @return {Promise<undefined>}
97+
*/
5398
async getChannels() {
5499
try {
55100
const { channels } = await this._grpc.sendCommand('listChannels');
@@ -69,6 +114,11 @@ class ChannelAction {
69114
}
70115
}
71116

117+
/**
118+
* List the pending channels by calling the respective grpc api and updating
119+
* the pendingChannels array in the global store.
120+
* @return {Promise<undefined>}
121+
*/
72122
async getPendingChannels() {
73123
try {
74124
const response = await this._grpc.sendCommand('pendingChannels');
@@ -112,6 +162,11 @@ class ChannelAction {
112162
}
113163
}
114164

165+
/**
166+
* List the peers by calling the respective grpc api and updating
167+
* the peers array in the global store.
168+
* @return {Promise<undefined>}
169+
*/
115170
async getPeers() {
116171
try {
117172
const { peers } = await this._grpc.sendCommand('listPeers');
@@ -131,6 +186,13 @@ class ChannelAction {
131186
}
132187
}
133188

189+
/**
190+
* Attempt to connect to a peer and open a channel in a single call.
191+
* If a connection already exists, just a channel will be opened.
192+
* This action can be called from a view event handler as does all
193+
* the necessary error handling and notification display.
194+
* @return {Promise<undefined>}
195+
*/
134196
async connectAndOpen() {
135197
try {
136198
const { channel, settings } = this._store;
@@ -149,6 +211,13 @@ class ChannelAction {
149211
}
150212
}
151213

214+
/**
215+
* Connect to peer and fail gracefully by catching exceptions and
216+
* logging their output.
217+
* @param {string} options.host The hostname of the peer
218+
* @param {string} options.pubkey The public key of the peer
219+
* @return {Promise<undefined>}
220+
*/
152221
async connectToPeer({ host, pubkey }) {
153222
try {
154223
await this._grpc.sendCommand('connectPeer', {
@@ -159,6 +228,13 @@ class ChannelAction {
159228
}
160229
}
161230

231+
/**
232+
* Open a channel to a peer without advertising it and update channel
233+
* state on data event from the streaming grpc api.
234+
* @param {string} options.pubkey The public key of the peer
235+
* @param {number} options.amount The amount in satoshis to fund the channel
236+
* @return {Promise<undefined>}
237+
*/
162238
async openChannel({ pubkey, amount }) {
163239
const stream = this._grpc.sendStreamCommand('openChannel', {
164240
node_pubkey: new Buffer(pubkey, 'hex'),
@@ -173,6 +249,12 @@ class ChannelAction {
173249
});
174250
}
175251

252+
/**
253+
* Close the selected channel by attempting a cooperative close.
254+
* This action can be called from a view event handler as does all
255+
* the necessary error handling and notification display.
256+
* @return {Promise<undefined>}
257+
*/
176258
async closeSelectedChannel() {
177259
try {
178260
const { selectedChannel } = this._store;
@@ -183,6 +265,14 @@ class ChannelAction {
183265
}
184266
}
185267

268+
/**
269+
* Close a channel using the grpc streaming api and update the state
270+
* on data events. Once the channel close is complete the channel will
271+
* be removed from the channels array in the store.
272+
* @param {string} options.channelPoint The channel identifier
273+
* @param {Boolean} options.force Force or cooperative close
274+
* @return {Promise<undefined>}
275+
*/
186276
async closeChannel({ channelPoint, force = false }) {
187277
const stream = this._grpc.sendStreamCommand('closeChannel', {
188278
channel_point: this._parseChannelPoint(channelPoint),

src/action/grpc.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @fileOverview a low level action to proxy GRPC api calls to and from lnd
3+
* over electron's IPC renderer api. This module should not be invokes directly
4+
* from the UI but rather used within other higher level actions.
5+
*/
6+
17
import { Duplex } from 'stream';
28
import * as log from './log';
39

@@ -11,17 +17,34 @@ class GrpcAction {
1117
// WalletUnlocker grpc client
1218
//
1319

20+
/**
21+
* The first GRPC api that is called to initialize the wallet unlocker.
22+
* Once `unlockerReady` is set to true on the store GRPC calls can be
23+
* made to the client.
24+
* @return {Promise<undefined>}
25+
*/
1426
async initUnlocker() {
1527
await this._sendIpc('unlockInit', 'unlockReady');
1628
log.info('GRPC unlockerReady');
1729
this._store.unlockerReady = true;
1830
}
1931

32+
/**
33+
* This GRPC api is called after the wallet is unlocked to close the grpc
34+
* client to lnd before the main lnd client is re-opened
35+
* @return {Promise<undefined>}
36+
*/
2037
async closeUnlocker() {
2138
await this._sendIpc('unlockClose', 'unlockClosed');
2239
log.info('GRPC unlockerClosed');
2340
}
2441

42+
/**
43+
* Wrapper function to execute calls to the wallet unlocker.
44+
* @param {string} method The unlocker GRPC api to call
45+
* @param {Object} body The payload passed to the api
46+
* @return {Promise<Object>}
47+
*/
2548
async sendUnlockerCommand(method, body) {
2649
return this._sendIpc('unlockRequest', 'unlockResponse', method, body);
2750
}
@@ -30,21 +53,44 @@ class GrpcAction {
3053
// Lightning (lnd) grpc client
3154
//
3255

56+
/**
57+
* This is called to initialize the main GRPC client to lnd. Once `lndReady`
58+
* is set to true on the store GRPC calls can be made to the client.
59+
* @return {Promise<undefined>}
60+
*/
3361
async initLnd() {
3462
await this._sendIpc('lndInit', 'lndReady');
3563
log.info('GRPC lndReady');
3664
this._store.lndReady = true;
3765
}
3866

67+
/**
68+
* Closes the main GRPC client to lnd. This should only be called upon exiting
69+
* the application as api calls need to be throughout the lifetime of the app.
70+
* @return {Promise<undefined>}
71+
*/
3972
async closeLnd() {
4073
await this._sendIpc('lndClose', 'lndClosed');
4174
log.info('GRPC lndClosed');
4275
}
4376

77+
/**
78+
* Wrapper function to execute calls to the lnd grpc client.
79+
* @param {string} method The lnd GRPC api to call
80+
* @param {Object} body The payload passed to the api
81+
* @return {Promise<Object>}
82+
*/
4483
sendCommand(method, body) {
4584
return this._sendIpc('lndRequest', 'lndResponse', method, body);
4685
}
4786

87+
/**
88+
* Wrapper function to execute GRPC streaming api calls to lnd. This function
89+
* proxies data to and from lnd using a duplex stream which is returned.
90+
* @param {string} method The lnd GRPC api to call
91+
* @param {Object} body The payload passed to the api
92+
* @return {Duplex} The duplex stream object instance
93+
*/
4894
sendStreamCommand(method, body) {
4995
const self = this;
5096
const stream = new Duplex({

src/action/index.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @fileOverview this is the main action module where all actions are initilized
3+
* by injecting dependencies and then triggering startup actions when certain
4+
* flags on the global store are set to true.
5+
*/
6+
17
import { observe } from 'mobx';
28
import { AsyncStorage, Clipboard } from 'react-native';
39
import { nap } from '../helper';
@@ -21,7 +27,7 @@ const ipcRenderer = window.ipcRenderer; // exposed to sandbox via preload.js
2127
// Inject dependencies
2228
//
2329

24-
store.init();
30+
store.init(); // initialize computed values
2531

2632
export const db = new AppStorage(store, AsyncStorage);
2733
export const log = new LogAction(store, ipcRenderer);
@@ -43,28 +49,43 @@ export const invoice = new InvoiceAction(
4349
export const payment = new PaymentAction(store, grpc, transaction, nav, notify);
4450
export const setting = new SettingAction(store, wallet, db);
4551

46-
payment.listenForUrl(ipcRenderer);
52+
payment.listenForUrl(ipcRenderer); // enable incoming url handler
4753

4854
//
4955
// Init actions
5056
//
5157

52-
db.restore();
58+
db.restore(); // read user settings from disk
5359

60+
/**
61+
* Triggered after user settings are restored from disk.
62+
*/
5463
observe(store, 'loaded', async () => {
5564
await grpc.initUnlocker();
5665
});
5766

67+
/**
68+
* Triggered after the wallet unlocker grpc client is initialized.
69+
*/
5870
observe(store, 'unlockerReady', async () => {
5971
await wallet.init();
6072
});
6173

74+
/**
75+
* Triggered after the user's password has unlocked the wallet.
76+
*/
6277
observe(store, 'walletUnlocked', async () => {
6378
await nap();
6479
await grpc.closeUnlocker();
6580
await grpc.initLnd();
6681
});
6782

83+
/**
84+
* Triggered once the main lnd grpc client is inialized. This is when
85+
* the user can really begin to interact with the application and calls
86+
* to and from lnd can be done. The display the current state of the
87+
* lnd node all balances, channels and transactions are fetched.
88+
*/
6889
observe(store, 'lndReady', () => {
6990
info.getInfo();
7091
wallet.update();

0 commit comments

Comments
 (0)