import { Component, PropTypes } from 'react'
import Evee from 'evee'

const evee = new Evee()

export default class Storage extends Component {
  static propTypes = {
    subscribe: PropTypes.oneOfType([
      PropTypes.string,
      PropTypes.arrayOf(PropTypes.string)
    ]),
    driver: PropTypes.shape({
      getItem: PropTypes.func.isRequired,
      setItem: PropTypes.func.isRequired
    }).isRequired
  }

  constructor(props) {
    super(props)
    const subscribedKeys = this.getSubscribedKeys()
    this.state = subscribedKeys.reduce(async (result, key) => ({
      ...result,
      [key]: await this.props.driver.getItem(key) || null
    }), {})
    this.subscriptions = subscribedKeys.map(key => (
      evee.on(key, event => this.setState({ [key]: event.data }))
    ))
  }

  componentWillUnmount() {
    if (this.subscriptions) {
      this.subscriptions.forEach(subscription => evee.drop(subscription))
    }
  }

  getSubscribedKeys() {
    if (!this.props.subscribe) {
      return []
    }
    if (typeof this.props.subscribe === 'string') {
      return [this.props.subscribe]
    }
    return this.props.subscribe
  }

  setItem = async (key, value) => {
    await this.props.driver.setItem(key, value)
    evee.emit(key, value)
    return value
  }

  render() {
    return this.props.children({ ...this.state, setItem: this.setItem })
  }
}

@@ -413,45 +413,43 @@
 %0A%0A  
-constructor(props) %7B%0A    super(props)
+state = %7B%7D%0A%0A  componentDidMount() %7B
 %0A   
@@ -505,21 +505,113 @@
     
-this.state = 
+Promise.all(subscribedKeys.map(this.props.driver.getItem))%0A      .then(values =%3E %7B%0A        this.setState(
 subs
@@ -628,22 +628,16 @@
 .reduce(
-async 
 (result,
@@ -644,16 +644,23 @@
  key
+, index
 ) =%3E (%7B%0A
     
@@ -655,16 +655,20 @@
 ) =%3E (%7B%0A
+    
       ..
@@ -686,51 +686,32 @@
     
+    
 %5Bkey%5D: 
-await this.props.driver.getItem(key)
+values%5Bindex%5D
  %7C%7C 
@@ -715,16 +715,20 @@
 %7C%7C null%0A
+    
     %7D), 
@@ -730,21 +730,26 @@
  %7D), %7B%7D)
-%0A
+)%0A  
     
+  
 this.sub
@@ -789,16 +789,20 @@
 ey =%3E (%0A
+    
       ev
@@ -863,17 +863,83 @@
 %7D))%0A    
-)
+    ))%0A      %7D)%0A      .catch(error =%3E %7B%0A        throw error%0A      %7D
 )%0A  %7D%0A%0A 
@@ -1304,14 +1304,8 @@
 em =
- async
  (ke
@@ -1327,94 +1327,213 @@
     
-await this.props.driver.setItem(key, value)%0A    evee.emit(key, value)%0A    return value
+return new Promise((resolve, reject) =%3E %7B%0A      this.props.driver.setItem(key, value)%0A        .then(() =%3E %7B%0A          evee.emit(key, value)%0A          resolve(value)%0A        %7D)%0A        .catch(reject)%0A    %7D)
 %0A  %7D

Fix AsyncStorage build