[英]Meteor Sub/Pub with Simulation where pub is slow to update
我有一個包裝和外部API的酒吧。 客戶端訂閱了外部api pub。 他們可以按下“激活”按鈕來激活計費方式。 按鈕調用更新集合的更新方法。 酒館會更新外部api。 模擬運行並更新客戶端集合。 按鈕將按預期方式更改為“停用”。 這就是問題所在。外部api需要一些時間才能返回更新的文檔。 在按鈕變為“停用”的100-200毫秒內,它將翻轉回“激活”,然后在500毫秒后回到“停用”,這應該假設外部api沒有問題。
我確定我可以提出一些駭人聽聞的解決方案來解決客戶端中的此問題,但想知道是否有一種方法可以告訴模擬/客戶端集合發布速度很慢並且更新的頻率不那么高? 因此,給pub / external API更多的時間來完成更新。
事實證明這很簡單。
僅客戶端仿真是不夠的。 技巧是同時進行服務器端仿真。 要完成此操作,首先需要設置Meteor的鈎子。
_initServer() {
if (Meteor.isServer) {
console.log(`Server initializing external collection "${this.name}"`)
let self = this
Meteor.publish(this.name, function (selector, options) {
check(selector, Match.Optional(Match.OneOf(undefined, null, Object)))
check(options, Match.Optional(Match.OneOf(undefined, null, Object)))
self.publication = this
self._externalApi.fetchAll()
.then((docs)=>docs.forEach((doc)=>this.added(self.name, doc._id, doc)))
.then(()=>this.ready())
// todo handle error
.catch((error)=>console.error(`${self.name}._initServer: self._externalApi.fetchAll`, error))
})
}
}
然后,在更新功能中,您可以在客戶端和服務器上進行模擬,如下所示:
this.update = new ValidatedMethod({
name: `${self.name}.update`,
validate: (validators && validators.update) ? validators.update : self.updateSchema.validator({clean: true}),
run(doc) {
console.log(`${self.name}.update `, doc)
if (Meteor.isServer && self._externalApi.update) {
// server side simulation
self.changed(doc)
self._externalApi.update(doc._id, doc)
.then(self.changed)
.catch((error)=>handleError(`${self.name}.update`, 'externalApi.update', error))
} else {
// client side simulation
self.collection.update(doc._id, {$set: doc})
}
},
})
抱歉,如果這過於簡化,則這些示例來自我們用於外部api的大型庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.