UNPKG

@soundsright/connector

Version:

soundsright wallet connector

124 lines (123 loc) 5.07 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const events_1 = require("events"); const types_1 = require("./types"); const connections_1 = require("./connections"); __exportStar(require("./types"), exports); __exportStar(require("./connections"), exports); __exportStar(require("./helper"), exports); class Connector extends events_1.EventEmitter { constructor() { super(); this.LAST_CONNECT_CACHE_KEY = 'lastConnect'; this.cache = localStorage; this.connections = {}; this.registLastConnectCache(); this.registConnection(types_1.ConnectType.MetaMask, connections_1.MetaMaskConnection.getInstance()); this.registConnection(types_1.ConnectType.WalletConnect, connections_1.WalletConnectConnection.getInstance()); } static getInstance() { if (!Connector._instance) { Connector._instance = new this(); } return Connector._instance; } get type() { return this._type; } get state() { return this._type ? this.connections[this._type].state : {}; } connect(type) { return __awaiter(this, void 0, void 0, function* () { this._type = type; return this.connections[type].connect(); }); } switchChain(chainId) { return __awaiter(this, void 0, void 0, function* () { yield this.connections[this.type].switchChain(chainId); }); } addToken(options) { return __awaiter(this, void 0, void 0, function* () { yield this.connections[this.type].addToken(options); }); } disconnect() { var _a, _b; return __awaiter(this, void 0, void 0, function* () { yield ((_b = (_a = this.connections[this.type]) === null || _a === void 0 ? void 0 : _a.disconnect) === null || _b === void 0 ? void 0 : _b.call(_a)); }); } eventThrough(emitter, type) { emitter.on(type, (...args) => { this.emit(type, ...args); }); } registConnection(type, handler) { this.connections[type] = handler; this.eventThrough(handler, 'change'); this.eventThrough(handler, 'connect'); this.eventThrough(handler, 'disconnect'); this.eventThrough(handler, 'accountsChanged'); this.eventThrough(handler, 'chainChanged'); this.eventThrough(handler, 'uriAvailable'); } registLastConnectCache() { this.on('connect', () => { var _a; (_a = this.cache) === null || _a === void 0 ? void 0 : _a.setItem(this.LAST_CONNECT_CACHE_KEY, this.type); }); this.on('disconnect', () => { var _a; (_a = this.cache) === null || _a === void 0 ? void 0 : _a.removeItem(this.LAST_CONNECT_CACHE_KEY); }); } setCache(cache) { this.cache = cache; } tryLastConnect() { var _a; return __awaiter(this, void 0, void 0, function* () { const lastConnect = (_a = this.cache) === null || _a === void 0 ? void 0 : _a.getItem(this.LAST_CONNECT_CACHE_KEY); if (lastConnect) { try { yield this.connect(lastConnect); return true; } catch (e) { return false; } } return false; }); } registUriHandler(handler) { connections_1.WalletConnectConnection.getInstance().registUriHandler(handler); } } exports.default = Connector;