我遇到了一个问题,我不是异步/rxjs开发方面的专家.
让我解释一下我的问题:
我有一个Angular 服务TimeoutService
,它只包含一个processTimeout()
方法:
processTimeout(timeout: NodeJS.Timeout, stillLoading: boolean): void {
if (timeout !== undefined) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
if (stillLoading) this.dialog.open(LoadingDialogComponent);
}, this.TIMEOUT_DURATION_MS);
}
processTimeout()
执行以下操作:
-
以
NodeJS.Timeout
为参数,stillLoading
boolean
. -
首先,它判断超时是否已经开启,如果已经开启,则将其清除.
-
然后,我设置我的超时,如果在超时回调时,
stillLoading
值是true
(这总是因为当我将它作为参数从componentA
传递给processTimeout
方法时,值是true
),我会打开一个对话框,说明该请求花费了很多时间.
问题是,传递给方法的stillLoading
个布尔值是我的标准Angular 组件的属性(例如,让我们将其称为具有boolean
loadingA
属性的ComponentA
),并且我已经知道js/ts具有基本类型的传递值:
当我调用我的API服务时,它被设置为True,当加载完成时(当订阅我的API请求已经完成时),它被设置为False.
但将loadingA
的值设置为False是在我从服务中调用processTimeout()
方法之后发生的.
- 所以我的问题是,什么是最好的方法/最佳实践,这样
processTimeout()
中使用的布尔值将保持引用或指针(对我也一样,但我不知道Js/ts在幕后是如何工作的,但变量应该有一个内存/DOM十六进制地址),或者甚至是可观察的地址,或者是什么,这样processTimeout()
中的stillLoading
的值与我的componentA
中的属性loadingA
保持相同?(属性loadingA
作为stillLoading
参数传递,也是componentA
中的原始boolean
类型).
Because of the nature of the timeout that makes the code inside it execute after the timeout, i need the boolean value at that time (from the componentA
), and not the value it was when passed-by-value in the method.
I'm down to listen to every possible solution, and if possible respecting ts and angular best practices.
- 在写这个问题时,我还认为我也需要为Timeout对象提供相同的解决方案(但因为它是一个对象,所以它应该已经通过引用传递了,对吗?)
谢谢.
try :老实说,我不喜欢太多,因为我更喜欢在这个问题上浪费时间之前得到一些建议,有一个 idea 是在我的componentA
loadingA
属性上传递一个新实例化的可观察点,但我不确定我对可观察点在这种情况下工作的方式的理解.
预期:processTimeout
中的stillLoading
方法参数是与我的componentA
loadingA
布尔属性相同的变量(相同的内存地址),或者是我可以用来满足我需要的工具上的任何解决方案/线索.