Как получить данные из rxjs? - PullRequest
0 голосов
/ 10 мая 2019

У меня есть файл для сохранения случайного идентификатора в AsyncStorage.

AppService.js

import Rx from 'rxjs/Rx';

import uuid from 'uuid/v4'

import StorageService from '../storage/StorageService'

export default class AppService {
  constructor() {
    this.deviceuuid = null
  }

  rxInit() {
    return StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
      if (deviceuuid == null) {
        this.deviceuuid = uuid()
        console.log('deviceuuid is empty, create one')
        return StorageService.shared.set('deviceuuid', this.deviceuuid)
      } else {
        this.deviceuuid = deviceuuid
        return Rx.Observable.of(this.deviceuuid)
      }
    }).do((deviceuuid) => {
        console.log(`deviceuuid is ${deviceuuid}`)
    })
  }
}

AppService.shared = new AppService()

StorageService.js

import Rx from 'rxjs/Rx'

import { AsyncStorage } from 'react-native'

import Storage from 'react-native-storage'

let storage = new Storage({
  size: 1000,
  storageBackend: AsyncStorage,
  defaultExpires: null,
})  

export default class StorageService {
  set(key, value) {
    return Rx.Observable.fromPromise(storage.save({key: key, data: value})).map(() => value)
  }

  get(key) {
    return Rx.Observable.fromPromise(storage.load({key: key})).catch(() => Rx.Observable.of(null))
  }

  remove(key) {
    return Rx.Observable.fromPromise(storage.remove({key: key})).catch(() => Rx.Observable.of(null))
  }
}

StorageService.shared = new StorageService()

Когда я строю проект, я вижу deviceuuid is 14c629ee-0dc7-43ef-91c2-eed642271670

Теперь я хочу получить deviceuuid с другого экрана.

  componentWillMount() {
    console.log('LoginScreen componentWillMount');
    console.log('rxInit data =>', AppService.shared.rxInit())

    let testId = StorageService.shared.get('deviceuuid')
    console.log('testID', testId);

    StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
      console.log('deviceuuid', deviceuuid);
      this.setState({ uuid: deviceuuid })
    }).do((deviceuuid) => {
      console.log(`LoginScreen deviceuuid is ${deviceuuid}`)
    })
  }

результат как на картинке

enter image description here

Понятия не имею, как получить deviceuuid.

и код не работает. Я не вижу никакого результата журнала консоли.

StorageService.shared.get('deviceuuid').flatMap((deviceuuid) => {
  console.log('deviceuuid', deviceuuid);
  this.setState({ uuid: deviceuuid })
}).do((deviceuuid) => {
  console.log(`LoginScreen deviceuuid is ${deviceuuid}`)
})

Я не знаком с rxjs, любая помощь будет признательна.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...