【发布时间】:2017-11-24 12:36:15
【问题描述】:
我很难理解如何实现 aor-realtime(尝试将它与 firebase 一起使用;只能读取,不能写入)。
我卡住的第一个地方:这个库生成了一个传奇,对吧?如何将其与 restClient/resource 连接?我有一些自定义 sagas 会提醒我错误,但有一个主要的 restClient/resource 支持这些。那些传奇只是处理一些副作用。在这种情况下,我只是不明白客户端的角色是什么,以及它如何与生成的 saga 交互(反之亦然)
另一个问题是关于持久性的:更新流并没有一次性加载初始记录集。每次更新我都应该打电话给observer.next()吗?或者缓存更新的记录并调用 next() 与整个集合到日期。
这是我目前的尝试,但我仍然不知道如何将它连接到我的管理员/资源。
import realtimeSaga from 'aor-realtime';
import { client, getToken } from '../firebase';
import { union } from 'lodash'
let cachedToken
const observeRequest = path => (type, resource, params) => {
// Filtering so that only chats are updated in real time
if (resource !== 'chat') return;
let results = {}
let ids = []
return {
subscribe(observer) {
let databaseRef = client.database().ref(path).orderByChild('at')
let events = [ 'child_added', 'child_changed' ]
events.forEach(e => {
databaseRef.on(e, ({ key, val }) => {
results[key] = val()
ids = union([ key ], ids)
observer.next(ids.map(id => results[id]))
})
})
const subscription = {
unsubscribe() {
// Clean up after ourselves
databaseRef.off()
results = {}
ids = []
// Notify the saga that we cleaned up everything
observer.complete();
}
};
return subscription;
},
};
};
export default path => realtimeSaga(observeRequest(path));
【问题讨论】:
标签: admin-on-rest