Some cleanups to web3Enable (god-function) (#507)
* Some cleanups to web3Enable (god-function) * Only use document complete * Cleanup * Rewrite
This commit is contained in:
parent
b3ab81a7d7
commit
d523d210c5
24
package.json
24
package.json
@ -8,12 +8,12 @@
|
||||
"packages/*"
|
||||
],
|
||||
"resolutions": {
|
||||
"@polkadot/api": "^2.2.2-7",
|
||||
"@polkadot/keyring": "^3.6.0-beta.14",
|
||||
"@polkadot/networks": "^3.6.0-beta.14",
|
||||
"@polkadot/types": "^2.2.2-7",
|
||||
"@polkadot/util": "^3.6.0-beta.14",
|
||||
"@polkadot/util-crypto": "^3.6.0-beta.14",
|
||||
"@polkadot/api": "^2.2.2-11",
|
||||
"@polkadot/keyring": "^3.6.0-beta.16",
|
||||
"@polkadot/networks": "^3.6.0-beta.16",
|
||||
"@polkadot/types": "^2.2.2-11",
|
||||
"@polkadot/util": "^3.6.0-beta.16",
|
||||
"@polkadot/util-crypto": "^3.6.0-beta.16",
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"safe-buffer": "^5.2.1",
|
||||
"typescript": "^4.0.2"
|
||||
@ -35,21 +35,21 @@
|
||||
"watch": "cd packages/extension && yarn polkadot-exec-webpack --config webpack.config.js --watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.11.6",
|
||||
"@polkadot/dev": "^0.58.1",
|
||||
"@babel/core": "^7.12.3",
|
||||
"@polkadot/dev": "^0.58.3",
|
||||
"@types/jest": "^26.0.14",
|
||||
"babel-loader": "^8.1.0",
|
||||
"copy-webpack-plugin": "^6.2.0",
|
||||
"copy-webpack-plugin": "^6.2.1",
|
||||
"extract-loader": "^5.1.0",
|
||||
"file-loader": "^6.1.0",
|
||||
"html-loader": "^1.3.1",
|
||||
"file-loader": "^6.1.1",
|
||||
"html-loader": "^1.3.2",
|
||||
"i18next-scanner": "^2.11.0",
|
||||
"sinon-chrome": "^3.0.1",
|
||||
"stylelint": "^13.7.2",
|
||||
"stylelint-config-recommended": "^3.0.0",
|
||||
"stylelint-config-styled-components": "^0.1.1",
|
||||
"thread-loader": "^2.1.3",
|
||||
"url-loader": "^4.1.0",
|
||||
"url-loader": "^4.1.1",
|
||||
"webpack": "^4.44.2",
|
||||
"webpack-cli": "^3.3.12",
|
||||
"webpack-extension-manifest-plugin": "^0.5.0"
|
||||
|
||||
@ -5,10 +5,10 @@
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@polkadot/api": "^2.2.2-7",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/api": "^2.2.2-11",
|
||||
"@polkadot/extension-inject": "0.35.0-beta.21",
|
||||
"@polkadot/keyring": "^3.6.0-beta.14",
|
||||
"@polkadot/ui-keyring": "^0.60.2-0"
|
||||
"@polkadot/keyring": "^3.6.0-beta.16",
|
||||
"@polkadot/ui-keyring": "^0.60.2-3"
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2"
|
||||
"@babel/runtime": "^7.12.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/api": "*"
|
||||
|
||||
@ -5,10 +5,10 @@
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/extension-inject": "0.35.0-beta.21",
|
||||
"@polkadot/util": "^3.6.0-beta.14",
|
||||
"@polkadot/util-crypto": "^3.6.0-beta.14"
|
||||
"@polkadot/util": "^3.6.0-beta.16",
|
||||
"@polkadot/util-crypto": "^3.6.0-beta.16"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@polkadot/api": "*"
|
||||
|
||||
@ -42,6 +42,19 @@ let web3EnablePromise: Promise<InjectedExtension[]> | null = null;
|
||||
|
||||
export { isWeb3Injected, web3EnablePromise };
|
||||
|
||||
function getWindowExtensions (originName: string): Promise<[InjectedExtensionInfo, Injected | void][]> {
|
||||
return Promise.all(
|
||||
Object.entries(win.injectedWeb3).map(([name, { enable, version }]): Promise<[InjectedExtensionInfo, Injected | void]> =>
|
||||
Promise.all([
|
||||
Promise.resolve({ name, version }),
|
||||
enable(originName).catch((error: Error): void => {
|
||||
console.error(`Error initializing ${name}: ${error.message}`);
|
||||
})
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// enables all the providers found on the injected window interface
|
||||
export function web3Enable (originName: string): Promise<InjectedExtension[]> {
|
||||
if (!originName) {
|
||||
@ -49,23 +62,13 @@ export function web3Enable (originName: string): Promise<InjectedExtension[]> {
|
||||
}
|
||||
|
||||
web3EnablePromise = documentReadyPromise((): Promise<InjectedExtension[]> =>
|
||||
Promise
|
||||
.all(
|
||||
Object.entries(win.injectedWeb3).map(([name, { enable, version }]): Promise<[InjectedExtensionInfo, Injected | void]> => {
|
||||
return Promise.all([
|
||||
Promise.resolve({ name, version }),
|
||||
enable(originName).catch((error: Error): void => {
|
||||
console.error(`Error initializing ${name}: ${error.message}`);
|
||||
})
|
||||
]);
|
||||
})
|
||||
)
|
||||
.then((values: [InjectedExtensionInfo, Injected | void][]): InjectedExtension[] =>
|
||||
getWindowExtensions(originName)
|
||||
.then((values): InjectedExtension[] =>
|
||||
values
|
||||
.filter(([, ext]): boolean => !!ext)
|
||||
.filter((value): value is [InjectedExtensionInfo, Injected] => !!value[1])
|
||||
.map(([info, ext]): InjectedExtension => {
|
||||
// if we don't have an accounts subscriber, add a single-shot version
|
||||
if (ext && !ext.accounts.subscribe) {
|
||||
if (!ext.accounts.subscribe) {
|
||||
ext.accounts.subscribe = (cb: (accounts: InjectedAccount[]) => void | Promise<void>): Unsubcall => {
|
||||
ext.accounts.get().then(cb).catch(console.error);
|
||||
|
||||
@ -75,12 +78,10 @@ export function web3Enable (originName: string): Promise<InjectedExtension[]> {
|
||||
};
|
||||
}
|
||||
|
||||
const injected: Partial<InjectedExtension> = { ...info, ...ext };
|
||||
|
||||
return injected as InjectedExtension;
|
||||
return { ...info, ...ext };
|
||||
})
|
||||
)
|
||||
.catch((): InjectedExtension[] => [] as InjectedExtension[])
|
||||
.catch((): InjectedExtension[] => [])
|
||||
.then((values): InjectedExtension[] => {
|
||||
const names = values.map(({ name, version }): string => `${name}/${version}`);
|
||||
|
||||
|
||||
@ -3,12 +3,10 @@
|
||||
|
||||
export function documentReadyPromise <T> (creator: () => Promise<T>): Promise<T> {
|
||||
return new Promise((resolve): void => {
|
||||
if (['complete', 'interactive'].includes(document.readyState)) {
|
||||
if (document.readyState === 'complete') {
|
||||
resolve(creator());
|
||||
} else {
|
||||
window.addEventListener('load', (): void => {
|
||||
resolve(creator());
|
||||
});
|
||||
window.addEventListener('load', () => resolve(creator()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -5,10 +5,10 @@
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2"
|
||||
"@babel/runtime": "^7.12.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chrome": "^0.0.124",
|
||||
"@types/chrome": "^0.0.125",
|
||||
"@types/firefox-webext-browser": "^78.0.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@ -5,17 +5,17 @@
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/extension-base": "0.35.0-beta.21",
|
||||
"@polkadot/extension-inject": "0.35.0-beta.21",
|
||||
"@polkadot/keyring": "^3.6.0-beta.14",
|
||||
"@polkadot/react-identicon": "^0.60.2-0",
|
||||
"@polkadot/react-qr": "^0.60.2-0",
|
||||
"@polkadot/util": "^3.6.0-beta.14",
|
||||
"@polkadot/util-crypto": "^3.6.0-beta.14",
|
||||
"i18next": "^19.8.1",
|
||||
"react": "^16.13.1",
|
||||
"react-dom": "^16.13.1",
|
||||
"@polkadot/keyring": "^3.6.0-beta.16",
|
||||
"@polkadot/react-identicon": "^0.60.2-3",
|
||||
"@polkadot/react-qr": "^0.60.2-3",
|
||||
"@polkadot/util": "^3.6.0-beta.16",
|
||||
"@polkadot/util-crypto": "^3.6.0-beta.16",
|
||||
"i18next": "^19.8.2",
|
||||
"react": "^16.14.0",
|
||||
"react-dom": "^16.14.0",
|
||||
"react-dropzone": "^11.2.0",
|
||||
"react-i18next": "^11.7.3",
|
||||
"react-is": "^16.13.1",
|
||||
@ -30,9 +30,9 @@
|
||||
"@types/react-copy-to-clipboard": "^4.3.0",
|
||||
"@types/react-dom": "^16.9.8",
|
||||
"@types/react-router": "^5.1.8",
|
||||
"@types/react-router-dom": "^5.1.5",
|
||||
"@types/react-router-dom": "^5.1.6",
|
||||
"@types/sinon-chrome": "^2.2.10",
|
||||
"@types/styled-components": "^5.1.3",
|
||||
"@types/styled-components": "^5.1.4",
|
||||
"enzyme": "^3.11.0",
|
||||
"enzyme-adapter-react-16": "^1.15.5",
|
||||
"sinon-chrome": "^3.0.1"
|
||||
|
||||
@ -5,13 +5,13 @@
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "Apache-2",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.11.2",
|
||||
"@babel/runtime": "^7.12.1",
|
||||
"@polkadot/extension-base": "0.35.0-beta.21",
|
||||
"@polkadot/extension-inject": "0.35.0-beta.21",
|
||||
"@polkadot/extension-ui": "0.35.0-beta.21",
|
||||
"@polkadot/phishing": "^0.1.4"
|
||||
"@polkadot/phishing": "^0.1.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/dev": "^0.58.1"
|
||||
"@polkadot/dev": "^0.58.3"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user