Minor readability improvements (#185)

This commit is contained in:
Jaco Greeff
2019-08-12 16:31:43 +02:00
committed by GitHub
parent 1585abc7bc
commit e481af93c5
3 changed files with 22 additions and 57 deletions
+9 -21
View File
@@ -29,30 +29,27 @@ export class Keyring extends Base implements KeyringStruct {
public addExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null);
const json = this.saveAccount(pair);
return {
json,
json: this.saveAccount(pair),
pair
};
}
public addPair (pair: KeyringPair, password: string): CreateResult {
this.keyring.addPair(pair);
const json = this.saveAccount(pair, password);
return {
json,
json: this.saveAccount(pair, password),
pair
};
}
public addUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult {
const pair = this.keyring.addFromUri(suri, meta, type);
const json = this.saveAccount(pair, password);
return {
json,
json: this.saveAccount(pair, password),
pair
};
}
@@ -111,7 +108,6 @@ export class Keyring extends Base implements KeyringStruct {
? _address
: this.encodeAddress(_address);
const publicKey = this.decodeAddress(address);
const stores = type
? [this.stores[type]]
: Object.values(this.stores);
@@ -143,11 +139,9 @@ export class Keyring extends Base implements KeyringStruct {
return Object
.entries(available)
.filter(([, data]): boolean => {
const { json: { meta: { contract } } } = data;
return !!contract && contract.genesisHash === this.genesisHash;
})
.filter(([, { json: { meta: { contract } } }]): boolean =>
!!contract && contract.genesisHash === this.genesisHash
)
.map(([address]): KeyringAddress => this.getContract(address) as KeyringAddress);
}
@@ -226,11 +220,7 @@ export class Keyring extends Base implements KeyringStruct {
} else if (addressRegex.test(key)) {
this.loadAddress(json, key);
} else if (contractRegex.test(key)) {
if (
json.meta && json.meta.contract &&
this.genesisHash &&
this.genesisHash === json.meta.contract.genesisHash
) {
if (json.meta && json.meta.contract && this.genesisHash && this.genesisHash === json.meta.contract.genesisHash) {
this.loadContract(json, key);
}
}
@@ -316,15 +306,13 @@ export class Keyring extends Base implements KeyringStruct {
const available = this.addresses.subject.getValue();
if (!available[address]) {
const json = {
this.addresses.add(this._store, address, {
address,
meta: {
isRecent: true,
whenCreated: Date.now()
}
};
this.addresses.add(this._store, address, (json as KeyringJson));
});
}
return this.addresses.subject.getValue()[address];
@@ -28,8 +28,7 @@ function callNext (current: SubjectInfo, subject: BehaviorSubject<SubjectInfo>,
export default function genericSubject (keyCreator: (address: string) => string, withTest: boolean = false): AddressSubject {
let current: SubjectInfo = {};
const subject = new BehaviorSubject({});
const next = (): void =>
callNext(current, subject, withTest);
const next = (): void => callNext(current, subject, withTest);
development.subject.subscribe(next);
@@ -38,16 +37,11 @@ export default function genericSubject (keyCreator: (address: string) => string,
current = { ...current };
current[address] = {
json: {
...json,
address
},
json: { ...json, address },
option: createOptionItem(address, json.meta.name, !json.meta.isRecent)
};
const isDevMode = development.isDevelopment();
if (!json.meta.isInjected && (!json.meta.isTesting || isDevMode)) {
if (!json.meta.isInjected && (!json.meta.isTesting || development.isDevelopment())) {
store.set(keyCreator(address), json);
}
+10 -27
View File
@@ -52,36 +52,19 @@ class KeyringOption implements KeyringOptionInstance {
assert(!hasCalledInitOptions, 'Unable to initialise options more than once');
observableAll.subscribe((): void => {
const options = this.emptyOptions();
const opts = this.emptyOptions();
this.addAccounts(keyring, options);
this.addAddresses(keyring, options);
this.addContracts(keyring, options);
this.addAccounts(keyring, opts);
this.addAddresses(keyring, opts);
this.addContracts(keyring, opts);
options.address = this.linkItems({
Addresses: options.address,
Recent: options.recent
});
options.account = this.linkItems({
Accounts: options.account,
Development: options.testing
});
options.contract = this.linkItems({
Contracts: options.contract
});
opts.address = this.linkItems({ Addresses: opts.address, Recent: opts.recent });
opts.account = this.linkItems({ Accounts: opts.account, Development: opts.testing });
opts.contract = this.linkItems({ Contracts: opts.contract });
opts.all = ([] as KeyringSectionOptions).concat(opts.account, opts.address);
opts.allPlus = ([] as KeyringSectionOptions).concat(opts.account, opts.address, opts.contract);
options.all = ([] as KeyringSectionOptions).concat(
options.account,
options.address
);
options.allPlus = ([] as KeyringSectionOptions).concat(
options.account,
options.address,
options.contract
);
this.optionsSubject.next(options);
this.optionsSubject.next(opts);
});
hasCalledInitOptions = true;