Skip to content

Commit

Permalink
feat: last changes to storage and instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosantangelo committed Dec 22, 2020
1 parent 593b9b7 commit e0bfbf7
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 52 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Calling `.connect()` (no params) with no previous successfull call, will throw.
async connect(
providerType?: ProviderType,
chainId: ChainId = ChainId.MAINNET
): Promise<ConnectResponse>
): Promise<ConnectionResponse>
```

**Usage**
Expand Down Expand Up @@ -177,7 +177,7 @@ export enum ChainId {
### ConnectionResponse
```typescript
export type ConnectResponse = {
export type ConnectionResponse = {
provider: Provider
chainId: ChainId
account: null | string
Expand All @@ -189,13 +189,13 @@ export type ConnectResponse = {
```typescript
import {
connection,
ConnectResponse,
ConnectionResponse,
ProviderType,
ChainId
} from 'decentraland-connection'

async function connect() {
let result: ConnectResponse
let result: ConnectionResponse
try {
result = await connection.connect() // this will throw if no successful connect was called before
} catch (error) {
Expand Down
44 changes: 31 additions & 13 deletions src/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
RequestArguments,
ProviderType,
ChainId,
ConnectResponse,
ConnectionData,
ConnectionResponse,
Provider,
ClosableConnector,
LegacyProvider
Expand All @@ -19,24 +20,25 @@ import { getConfiguration } from './configuration'
import './declarations'

export class ConnectionManager {
providerType?: ProviderType
connector?: AbstractConnector

constructor(public storage: Storage) {}

async connect(
providerType?: ProviderType,
chainId: ChainId = ChainId.MAINNET
): Promise<ConnectResponse> {
const { storageKey } = getConfiguration()

this.providerType = providerType || this.storage.get(storageKey)
if (!this.providerType) {
throw new Error('connect called without a provider and none was stored')
): Promise<ConnectionResponse> {
if (!providerType) {
const connectionData = this.getConnectionData()
if (!connectionData) {
throw new Error('connect called without a provider and none was stored')
}
providerType = connectionData.providerType
chainId = connectionData.chainId
}

this.storage.set(storageKey, providerType)
this.connector = this.getConnector(this.providerType, chainId)
this.setConnectionData(providerType, chainId)
this.connector = this.getConnector(providerType, chainId)

const {
provider,
Expand Down Expand Up @@ -65,6 +67,9 @@ export class ConnectionManager {
if (this.isClosableConnector()) {
await (this.connector as ClosableConnector).close()
}

this.storage.clear()
this.connector = undefined
}
}

Expand Down Expand Up @@ -100,10 +105,23 @@ export class ConnectionManager {
}
}

private getConnectionData(): ConnectionData | undefined {
const { storageKey } = getConfiguration()
const connectionData = this.storage.get(storageKey)
return connectionData ? JSON.parse(connectionData) : undefined
}

private setConnectionData(providerType: ProviderType, chainId: ChainId) {
const { storageKey } = getConfiguration()
const connectionData = JSON.stringify({
providerType,
chainId
} as ConnectionData)
this.storage.set(storageKey, connectionData)
}

private isClosableConnector() {
return [ProviderType.FORTMATIC, ProviderType.WALLET_CONNECT].includes(
this.providerType!
)
return this.connector && typeof this.connector['close'] !== 'undefined'
}

private toProvider(provider: LegacyProvider | Provider): Provider {
Expand Down
4 changes: 4 additions & 0 deletions src/storage/LocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ export class LocalStorage extends Storage {
set(key: string, value: any): void {
window.localStorage.setItem(key, value)
}

clear(): void {
window.localStorage.clear()
}
}
1 change: 1 addition & 0 deletions src/storage/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export abstract class Storage {
abstract get(key: string): any | undefined
abstract set(key: string, value: any): void
abstract clear(): void
}
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ export interface Provider extends EventEmitter {

export type LegacyProvider = Pick<Provider, 'send'>

export type ConnectResponse = {
export type ConnectionData = {
providerType: ProviderType
chainId: ChainId
}

export type ConnectionResponse = {
provider: Provider
chainId: ChainId
account: null | string
Expand Down
62 changes: 35 additions & 27 deletions test/ConnectionManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import sinon from 'sinon'
import { getConfiguration } from '../src/configuration'
import { ConnectionManager, connection } from '../src/ConnectionManager'
import { LocalStorage } from '../src/storage'
import { ChainId, ClosableConnector, ProviderType } from '../src/types'
Expand All @@ -20,7 +21,7 @@ describe('ConnectionManager', () => {

afterEach(() => {
sinon.restore()
storage.clean()
storage.clear()
})

describe('connection', () => {
Expand All @@ -30,15 +31,6 @@ describe('ConnectionManager', () => {
})

describe('#connect', () => {
it('should set the provider type', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)

expect(connectionManager.providerType).to.eq(undefined)
await connectionManager.connect(ProviderType.INJECTED)
expect(connectionManager.providerType).to.eq(ProviderType.INJECTED)
})

it('should set the connector', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
Expand Down Expand Up @@ -112,11 +104,16 @@ describe('ConnectionManager', () => {

it('should store the last provider and chain', async () => {
const stubConnector = new StubConnector()
const configuration = getConfiguration()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)

await connectionManager.connect(ProviderType.INJECTED)
await connectionManager.connect(ProviderType.INJECTED, ChainId.KOVAN)

expect(storage.get()).to.eq(ProviderType.INJECTED)
const value = JSON.stringify({
providerType: ProviderType.INJECTED,
chainId: ChainId.KOVAN
})
expect(storage.get(configuration.storageKey)).to.eq(value)
})

it('should connect to the last supplied provider', async () => {
Expand Down Expand Up @@ -166,23 +163,34 @@ describe('ConnectionManager', () => {
})

it('should call close if the provider type allows it', async () => {
await deactivate(ProviderType.FORTMATIC, true)
await deactivate(ProviderType.WALLET_CONNECT, true)
await deactivate(ProviderType.INJECTED, false)

async function deactivate(providerType: ProviderType, result: boolean) {
connectionManager.providerType = providerType
connectionManager.connector = new StubClosableConnector()
const closeStub = sinon.stub(
connectionManager.connector as ClosableConnector,
'close'
)
connectionManager.connector = new StubClosableConnector()
const closeStub = sinon.stub(
connectionManager.connector as ClosableConnector,
'close'
)

await connectionManager.disconnect()
await connectionManager.disconnect()

expect(closeStub.calledOnce).to.eq(result)
sinon.restore()
}
expect(closeStub.calledOnce).to.eq(true)
sinon.restore()
})

it('should clean the storage', async () => {
const configuration = getConfiguration()
storage.set(configuration.storageKey, 'data')

connectionManager.connector = new StubConnector()
await connectionManager.disconnect()

expect(storage.get(configuration.storageKey)).to.eq(undefined)
})

it('should clean the instance variables', async () => {
connectionManager.connector = new StubConnector()

await connectionManager.disconnect()

expect(connectionManager.connector).to.eq(undefined)
})
})

Expand Down
14 changes: 7 additions & 7 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ export class StubClosableConnector extends StubConnector {
}

export class StubStorage extends Storage {
value: any
cache: Record<string, any> = {}

get(_key: string = '') {
return this.value
get(key: string) {
return this.cache[key]
}

set(_key: string, value: any) {
this.value = value
set(key: string, value: any) {
this.cache[key] = value
}

clean() {
this.value = undefined
clear() {
this.cache = {}
}
}

0 comments on commit e0bfbf7

Please sign in to comment.