Skip to content

Commit fac0934

Browse files
authored
Alternative lazy Submittable (#1370)
* Aletrnative lazy Submittable * Extrinsic -> submittable * .default
1 parent acf00d3 commit fac0934

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

packages/api/src/submittable/Extrinsic.ts renamed to packages/api/src/submittable/Submittable.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { filterEvents, isKeyringPair } from '../util';
1717
import ApiBase from '../base';
1818
import SubmittableResult from './Result';
1919

20-
type Creator<ApiType> = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
21-
2220
interface SubmittableOptions<ApiType> {
2321
api: ApiInterfaceRx;
2422
decorateMethod: ApiBase<ApiType>['decorateMethod'];
@@ -34,7 +32,7 @@ const DEFAULT_MORTAL_LENGTH = 5 * ONE_MINUTE;
3432

3533
const _Extrinsic: Constructor<Extrinsic> = ClassOf('Extrinsic');
3634

37-
class Submittable<ApiType> extends _Extrinsic implements SubmittableExtrinsic<ApiType> {
35+
export default class Submittable<ApiType> extends _Extrinsic implements SubmittableExtrinsic<ApiType> {
3836
private readonly _api: ApiInterfaceRx;
3937

4038
private readonly _decorateMethod: ApiBase<ApiType>['decorateMethod'];
@@ -254,8 +252,3 @@ class Submittable<ApiType> extends _Extrinsic implements SubmittableExtrinsic<Ap
254252
);
255253
}
256254
}
257-
258-
export default function createSubmittableExtrinsic<ApiType> (type: ApiTypes, api: ApiInterfaceRx, decorateMethod: ApiBase<ApiType>['decorateMethod']): Creator<ApiType> {
259-
return (extrinsic: Call | Uint8Array | string): SubmittableExtrinsic<ApiType> =>
260-
new Submittable(extrinsic, { api, decorateMethod, type });
261-
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* eslint-disable no-dupe-class-members */
2+
// Copyright 2017-2019 @polkadot/api authors & contributors
3+
// This software may be modified and distributed under the terms
4+
// of the Apache-2.0 license. See the LICENSE file for details.
5+
6+
import { Call } from '@polkadot/types/interfaces';
7+
import { Constructor } from '@polkadot/types/types';
8+
import { ApiInterfaceRx, ApiTypes } from '../types';
9+
import { SubmittableExtrinsic } from './types';
10+
11+
import ApiBase from '../base';
12+
13+
type Creator<ApiType> = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
14+
15+
let Submittable: Constructor<SubmittableExtrinsic<any>>;
16+
17+
export default function createSubmittable<ApiType> (type: ApiTypes, api: ApiInterfaceRx, decorateMethod: ApiBase<ApiType>['decorateMethod']): Creator<ApiType> {
18+
return (extrinsic: Call | Uint8Array | string): SubmittableExtrinsic<ApiType> => {
19+
// HACK This is not great, but basically what we do here is to lazily only require the class
20+
// right at the point it is actually needed - delaying initialization
21+
if (!Submittable) {
22+
Submittable = require('./Submittable').default;
23+
}
24+
25+
return new Submittable(extrinsic, { api, decorateMethod, type });
26+
};
27+
}

packages/api/src/submittable/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// This software may be modified and distributed under the terms
33
// of the Apache-2.0 license. See the LICENSE file for details.
44

5-
export { default as createSubmittable } from './Extrinsic';
5+
export { default as createSubmittable } from './createSubmittable';
66
export { default as SubmittableResult } from './Result';

0 commit comments

Comments
 (0)