无法在 ngrx 选择器上释放记忆

Unable to release memoization on ngrx selector

我在 angular 应用程序中使用 NGRX,并使用选择器获取当前作业密钥,如下所示。使用此键,从文档存储中获取图像。

我查看了一些链接,建议 ngrx 是状态存储,而不是文档存储。我发现将它用作文档存储会大大降低速度。

https://github.com/btroncone/ngrx-store-localstorage/issues/39

无论如何,解决方案是拥有一个单独的图像存储,并且只处理处于 ngrx 状态的图像 key/identifier。哪个效果好。

this.store
    .pipe(select(getCurrentImage))
    .subscribe(async currentImageKey => {
        this.loadImage(currentImageKey);//Load and set the currentFile
    });

稍后在应用程序中更新图像时,图像 key/identified 没有改变,所以我想通过从选择器中释放记忆来强制更新。这应该会导致再次从文档存储中检索图像。

此答案建议释放备忘录。

Create non-memoized selector on ngrx

因此按照该建议,尝试在调用图像处理操作之前释放选择器。

async onAnalyseImage() {
    getCurrentImage.release();
    this.store.dispatch(
        ImageViewerActions.analyseImage({
            jobKey: this._currentFile.jobKey
        })
    );
}

然后一旦 action/effect 完成,我想通过一些更改来更新当前图像的值,但是状态中的键没有改变。所以我释放了选择器,它应该将记忆值设置为空。

on(ImageViewerActions.refreshImageCanvas, state => ({
    ...state,
    currentLoadedImage: state.currentLoadedImage,
})),

调用 "refresh" 操作,reducer 尝试将状态值设置为相同的值,希望再次调用选择器。

这并没有发生,记忆仍然存在。我可以更改为另一张图片,正如预期的那样,调用了选择器并更新了图片。

但是,我无法刷新相同的值键。

currentLoadedImage 是一个字符串。

memoization 进行字符串比较,Javascript 一个字符串是一个字符串是一个字符串。

所以即使可以传递一个新的字符串对象,它仍然会显示为同一个对象。 a===b