Skip to content

Commit

Permalink
Merge branch 'master' into cf-return-default-values
Browse files Browse the repository at this point in the history
  • Loading branch information
cavanmflynn authored Dec 17, 2019
2 parents 33ca4d8 + ea1a333 commit d838551
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 70 deletions.
8 changes: 4 additions & 4 deletions src/default-empty-arg.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Add an empty object as the first argument if no argument was passed
* @param {Function} func
* @param {Function} func The function being called
* @param {boolean} hasCallback Whether or not the passed function has a callback
* @return {any}
*/
export function defaultEmptyArg(func: (...args: any[]) => any): any {
export function defaultEmptyArg(func: (...args: any[]) => any, hasCallback: boolean = true): any {
return function(...args: any[]) {
// 2 is used, rather than 1, because of the callback.
if (args.length < 2) {
if (args.length < (hasCallback ? 2 : 1)) {
args.unshift({});
}
return func.apply(this, args);
Expand Down
16 changes: 5 additions & 11 deletions src/services/chain-notifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionConfig, SubscriptionMethod } from '../types';
import { ConnectionConfig } from '../types';
import { createServiceClient } from './create-service-client';

/**
Expand All @@ -12,16 +12,10 @@ export function createChainNotifier(config: ConnectionConfig): any {
server,
credentials,
);
const subscriptionMethods: SubscriptionMethod[] = [
{
name: 'registerConfirmationsNtfn',
},
{
name: 'registerSpendNtfn',
},
{
name: 'registerBlockEpochNtfn',
},
const subscriptionMethods = [
'registerConfirmationsNtfn',
'registerSpendNtfn',
'registerBlockEpochNtfn',
];

return createServiceClient({
Expand Down
8 changes: 2 additions & 6 deletions src/services/create-service-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ export function createServiceClient(config: GrpcServiceConfig) {
if (typeof method !== 'function') {
return target[key]; // forward
} else if (config.subscriptionMethods) {
const sm = config.subscriptionMethods.find((m) => m.name === key);
if (sm) {
if (sm.skipEmptyArgDefault) {
return method;
}
return defaultEmptyArg(method);
if (config.subscriptionMethods.includes(key)) {
return defaultEmptyArg(method, false);
}
}
return promisify(defaultEmptyArg(method));
Expand Down
8 changes: 3 additions & 5 deletions src/services/invoices.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionConfig, SubscriptionMethod } from '../types';
import { ConnectionConfig } from '../types';
import { createServiceClient } from './create-service-client';

/**
Expand All @@ -12,10 +12,8 @@ export function createInvoices(config: ConnectionConfig): any {
server,
credentials,
);
const subscriptionMethods: SubscriptionMethod[] = [
{
name: 'subscribeSingleInvoice',
},
const subscriptionMethods = [
'subscribeSingleInvoice',
];

return createServiceClient({
Expand Down
41 changes: 11 additions & 30 deletions src/services/lightning.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionConfig, SubscriptionMethod } from '../types';
import { ConnectionConfig } from '../types';
import { createServiceClient } from './create-service-client';

/**
Expand All @@ -12,35 +12,16 @@ export function createLightning(config: ConnectionConfig): any {
// Increase max receive message size for describegraph
'grpc.max_receive_message_length': 50 * 1024 * 1024,
});
const subscriptionMethods: SubscriptionMethod[] = [
{
name: 'subscribeInvoices',
},
{
name: 'subscribeTransactions',
},
{
name: 'subscribeChannelGraph',
},
{
name: 'subscribeChannelEvents',
},
{
name: 'subscribeChannelBackups',
},
{
name: 'sendToRoute',
},
{
name: 'sendPayment',
},
{
name: 'openChannel',
},
{
name: 'closeChannel',
skipEmptyArgDefault: true,
},
const subscriptionMethods = [
'subscribeInvoices',
'subscribeTransactions',
'subscribeChannelGraph',
'subscribeChannelEvents',
'subscribeChannelBackups',
'sendToRoute',
'sendPayment',
'openChannel',
'closeChannel',
];

return createServiceClient({
Expand Down
12 changes: 4 additions & 8 deletions src/services/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionConfig, SubscriptionMethod } from '../types';
import { ConnectionConfig } from '../types';
import { createServiceClient } from './create-service-client';

/**
Expand All @@ -12,13 +12,9 @@ export function createRouter(config: ConnectionConfig): any {
server,
credentials,
);
const subscriptionMethods: SubscriptionMethod[] = [
{
name: 'sendPayment',
},
{
name: 'trackPayment',
},
const subscriptionMethods = [
'sendPayment',
'trackPayment',
];

return createServiceClient({
Expand Down
7 changes: 1 addition & 6 deletions src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ export type GrpcLoader = typeof grpcLoader;

export type Grpc = typeof grpc;

export interface SubscriptionMethod {
name: string;
skipEmptyArgDefault?: boolean;
}

export interface NestedGrpcObject {
[index: string]: {
[index: string]: typeof Client;
Expand All @@ -24,7 +19,7 @@ export interface ConnectionConfig {

export interface GrpcServiceConfig extends ConnectionConfig {
service: any;
subscriptionMethods?: SubscriptionMethod[];
subscriptionMethods?: string[];
}

export interface GrpcObjectConfig {
Expand Down

0 comments on commit d838551

Please sign in to comment.