使用 rxjs Observable 订阅 mobx @observable 但没有在深层组件(ts 文件)中进行更改
subscribing mobx @observable using rxjs Observable but not getting on change in a deep level component (ts file)
我正在尝试使用 rxjs observable 从 angular-mobx 商店获取零钱。但如果观察到的数组发生变化,则不会从那里得到任何变化。但是,如果我使用“=”符号分配新值,那么我会在订阅中得到更改。谁能解释一下?或者仅通过像拼接或替换数组中的对象这样的更改来帮助获得更改?谢谢
https://stackblitz.com/edit/angular-reuych 演示应用程序
import { computed, action, observable } from "mobx-angular";
import {observe } from "mobx";
import { Observable } from 'rxjs';
import { Injectable } from "@angular/core";
import * as moment from "moment-timezone";
@Injectable()
export class Store {toRx(obj, prop) {
return Observable.create(observer =>
observe(obj, prop, (change) => observer.next(change.newValue), true)
);
}
@observable storeCampaigns:any=[];
}
然后在组件中这样订阅
this.store.toRx(this.store.storeCampaigns, 'campaigns')
.subscribe(val => {
console.log("calendar get change", val)
如果您深入研究 mobx
,您将到达以下 link observer,您可以在其中找到以下评论
/**
* A node in the state dependency root that observes other nodes, and can be observed itself.
*
* ComputedValue will remember the result of the computation for the duration of the batch, or
* while being observed.
*
* During this time it will recompute only when one of its direct dependencies changed,
* but only when it is being accessed with `ComputedValue.get()`.
*
* Implementation description:
* 1. First time it's being accessed it will compute and remember result
* give back remembered result until 2. happens
* 2. First time any deep dependency change, propagate POSSIBLY_STALE to all observers, wait for 3.
* 3. When it's being accessed, recompute if any shallow dependency changed.
* if result changed: propagate STALE to all observers, that were POSSIBLY_STALE from the last step.
* go to step 2. either way
*
* If at any point it's outside batch and it isn't observed: reset everything and go to 1.
*/
这几乎表明只有在第一次访问该值时(即当您订阅时)或当依赖项发生变化时(即,当您更改存储的引用时,在您的情况下使用商店内的 =
运算符)。
因此,如果我必须按照它的工作方式进行总结,其行为与其他状态管理库非常相似,它们仅在商店内的某些引用出现时才向其订阅者推送新值改变了。
** 免责声明,不是 mobx pro,但这是源代码所说的。
我正在尝试使用 rxjs observable 从 angular-mobx 商店获取零钱。但如果观察到的数组发生变化,则不会从那里得到任何变化。但是,如果我使用“=”符号分配新值,那么我会在订阅中得到更改。谁能解释一下?或者仅通过像拼接或替换数组中的对象这样的更改来帮助获得更改?谢谢
https://stackblitz.com/edit/angular-reuych 演示应用程序
import { computed, action, observable } from "mobx-angular";
import {observe } from "mobx";
import { Observable } from 'rxjs';
import { Injectable } from "@angular/core";
import * as moment from "moment-timezone";
@Injectable()
export class Store {toRx(obj, prop) {
return Observable.create(observer =>
observe(obj, prop, (change) => observer.next(change.newValue), true)
);
}
@observable storeCampaigns:any=[];
}
然后在组件中这样订阅
this.store.toRx(this.store.storeCampaigns, 'campaigns')
.subscribe(val => {
console.log("calendar get change", val)
如果您深入研究 mobx
,您将到达以下 link observer,您可以在其中找到以下评论
/**
* A node in the state dependency root that observes other nodes, and can be observed itself.
*
* ComputedValue will remember the result of the computation for the duration of the batch, or
* while being observed.
*
* During this time it will recompute only when one of its direct dependencies changed,
* but only when it is being accessed with `ComputedValue.get()`.
*
* Implementation description:
* 1. First time it's being accessed it will compute and remember result
* give back remembered result until 2. happens
* 2. First time any deep dependency change, propagate POSSIBLY_STALE to all observers, wait for 3.
* 3. When it's being accessed, recompute if any shallow dependency changed.
* if result changed: propagate STALE to all observers, that were POSSIBLY_STALE from the last step.
* go to step 2. either way
*
* If at any point it's outside batch and it isn't observed: reset everything and go to 1.
*/
这几乎表明只有在第一次访问该值时(即当您订阅时)或当依赖项发生变化时(即,当您更改存储的引用时,在您的情况下使用商店内的 =
运算符)。
因此,如果我必须按照它的工作方式进行总结,其行为与其他状态管理库非常相似,它们仅在商店内的某些引用出现时才向其订阅者推送新值改变了。
** 免责声明,不是 mobx pro,但这是源代码所说的。