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 { public addExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null); const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null);
const json = this.saveAccount(pair);
return { return {
json, json: this.saveAccount(pair),
pair pair
}; };
} }
public addPair (pair: KeyringPair, password: string): CreateResult { public addPair (pair: KeyringPair, password: string): CreateResult {
this.keyring.addPair(pair); this.keyring.addPair(pair);
const json = this.saveAccount(pair, password);
return { return {
json, json: this.saveAccount(pair, password),
pair pair
}; };
} }
public addUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult { public addUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult {
const pair = this.keyring.addFromUri(suri, meta, type); const pair = this.keyring.addFromUri(suri, meta, type);
const json = this.saveAccount(pair, password);
return { return {
json, json: this.saveAccount(pair, password),
pair pair
}; };
} }
@@ -111,7 +108,6 @@ export class Keyring extends Base implements KeyringStruct {
? _address ? _address
: this.encodeAddress(_address); : this.encodeAddress(_address);
const publicKey = this.decodeAddress(address); const publicKey = this.decodeAddress(address);
const stores = type const stores = type
? [this.stores[type]] ? [this.stores[type]]
: Object.values(this.stores); : Object.values(this.stores);
@@ -143,11 +139,9 @@ export class Keyring extends Base implements KeyringStruct {
return Object return Object
.entries(available) .entries(available)
.filter(([, data]): boolean => { .filter(([, { json: { meta: { contract } } }]): boolean =>
const { json: { meta: { contract } } } = data; !!contract && contract.genesisHash === this.genesisHash
)
return !!contract && contract.genesisHash === this.genesisHash;
})
.map(([address]): KeyringAddress => this.getContract(address) as KeyringAddress); .map(([address]): KeyringAddress => this.getContract(address) as KeyringAddress);
} }
@@ -226,11 +220,7 @@ export class Keyring extends Base implements KeyringStruct {
} else if (addressRegex.test(key)) { } else if (addressRegex.test(key)) {
this.loadAddress(json, key); this.loadAddress(json, key);
} else if (contractRegex.test(key)) { } else if (contractRegex.test(key)) {
if ( if (json.meta && json.meta.contract && this.genesisHash && this.genesisHash === json.meta.contract.genesisHash) {
json.meta && json.meta.contract &&
this.genesisHash &&
this.genesisHash === json.meta.contract.genesisHash
) {
this.loadContract(json, key); this.loadContract(json, key);
} }
} }
@@ -316,15 +306,13 @@ export class Keyring extends Base implements KeyringStruct {
const available = this.addresses.subject.getValue(); const available = this.addresses.subject.getValue();
if (!available[address]) { if (!available[address]) {
const json = { this.addresses.add(this._store, address, {
address, address,
meta: { meta: {
isRecent: true, isRecent: true,
whenCreated: Date.now() whenCreated: Date.now()
} }
}; });
this.addresses.add(this._store, address, (json as KeyringJson));
} }
return this.addresses.subject.getValue()[address]; 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 { export default function genericSubject (keyCreator: (address: string) => string, withTest: boolean = false): AddressSubject {
let current: SubjectInfo = {}; let current: SubjectInfo = {};
const subject = new BehaviorSubject({}); const subject = new BehaviorSubject({});
const next = (): void => const next = (): void => callNext(current, subject, withTest);
callNext(current, subject, withTest);
development.subject.subscribe(next); development.subject.subscribe(next);
@@ -38,16 +37,11 @@ export default function genericSubject (keyCreator: (address: string) => string,
current = { ...current }; current = { ...current };
current[address] = { current[address] = {
json: { json: { ...json, address },
...json,
address
},
option: createOptionItem(address, json.meta.name, !json.meta.isRecent) option: createOptionItem(address, json.meta.name, !json.meta.isRecent)
}; };
const isDevMode = development.isDevelopment(); if (!json.meta.isInjected && (!json.meta.isTesting || development.isDevelopment())) {
if (!json.meta.isInjected && (!json.meta.isTesting || isDevMode)) {
store.set(keyCreator(address), json); 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'); assert(!hasCalledInitOptions, 'Unable to initialise options more than once');
observableAll.subscribe((): void => { observableAll.subscribe((): void => {
const options = this.emptyOptions(); const opts = this.emptyOptions();
this.addAccounts(keyring, options); this.addAccounts(keyring, opts);
this.addAddresses(keyring, options); this.addAddresses(keyring, opts);
this.addContracts(keyring, options); this.addContracts(keyring, opts);
options.address = this.linkItems({ opts.address = this.linkItems({ Addresses: opts.address, Recent: opts.recent });
Addresses: options.address, opts.account = this.linkItems({ Accounts: opts.account, Development: opts.testing });
Recent: options.recent opts.contract = this.linkItems({ Contracts: opts.contract });
}); opts.all = ([] as KeyringSectionOptions).concat(opts.account, opts.address);
options.account = this.linkItems({ opts.allPlus = ([] as KeyringSectionOptions).concat(opts.account, opts.address, opts.contract);
Accounts: options.account,
Development: options.testing
});
options.contract = this.linkItems({
Contracts: options.contract
});
options.all = ([] as KeyringSectionOptions).concat( this.optionsSubject.next(opts);
options.account,
options.address
);
options.allPlus = ([] as KeyringSectionOptions).concat(
options.account,
options.address,
options.contract
);
this.optionsSubject.next(options);
}); });
hasCalledInitOptions = true; hasCalledInitOptions = true;