【问题标题】:How to combine RxDB and MobX如何结合 RxDB 和 MobX
【发布时间】:2018-12-17 21:05:33
【问题描述】:

我开始使用 electron + react + mobx 设置,现在想将 RxDB 添加到组合中。我以为我可以在商店内处理 rxdb 的东西(插入/订阅),但我真的不知道怎么做。

基本上我的问题是:

如何同步我的 Mobx 商店和我的 RxDB?

目前的代码大致是这样的:

class RecordingStore {
     ...
     constructor() {
        database.getDatabase( 'mydb', 'idb').then(async(db) => {
            this.db = db
            await db.recordings.sync({
                remote: syncURL,
                direction: {
                    pull: true,
                    push: true
                }
            });
        }
     }

     @action addRecording(title) {
        const item = new Recording(title)
        // should I really keep two collections? (RxDb AND Mobx)
        this.recordings.push(item)
        this.db.recordings.insert({ title: title }).then(()=>{console.log("recording saved")})
        return item
  }

【问题讨论】:

    标签: reactjs electron mobx


    【解决方案1】:

    据我所知,rxdb 只是 PouchDB 的一个 rxjs 可观察实例!

    如果你使用的是 Mobx,那么你可以安全地跳过 rxjs 步骤,直接使用 PouchDB;否则编写简单的方法并使用 mobx-pouchbd 依赖和..:

    save() {
        if (super.save()) {
            POUCH_DB_INSTANCE.put(this.toJS());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 2019-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      相关资源
      最近更新 更多