当前位置:网站首页>NgRx Store createSelector 的单步调试和源代码分析
NgRx Store createSelector 的单步调试和源代码分析
2022-08-01 21:54:00 【华为云】
源代码:
import { Component } from '@angular/core';import { createSelector } from '@ngrx/store'; export interface State { counter1: number; counter2: number;} export const selectCounter1 = (state: State) => state.counter1;export const selectCounter2 = (state: State) => state.counter2;export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2); // selectTotal has a memoized value of null, because it has not yet been invoked.
createSelector 内部:
function createSelector(...input) { return createSelectorFactory(defaultMemoize)(...input);}
defaultMemoize 返回一个对象,每个字段指向一个函数:
输入参数我传入了三个纯函数,即没有 side effect,可以重复执行的函数。这三个纯函数被维护到了一个数组内:
进入到 createSelectorFactory 返回的函数体内部:
从源代码可以看出,createSelector接收的可变数目的参数,最后一个被当成 projector 看待,其余均视作 selector.
分下列几种情况讨论:
(1) 第一次执行,lastArguments undefined, 进入该 IF 分支,执行 projection,将结果保存,并返回。
(2) 如果是重复执行,且输入参数没有变化,则直接返回上一次执行的结果
(3) 执行到这里,说明是重复执行,且参数发生了变化,则重新执行 projection
(4) 如果再次执行和前次执行的结果相同,返回前次结果
createSelector 最终返回的,是带有记忆功能的 selector:
以后如若要调试,记住代码的大概位置在 970,搜索关键字 memoized.
边栏推荐
- Chapter 12, target recognition of digital image processing
- shell specification and variables
- selenium无头,防检测
- 毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
- 【C语言实现】最大公约数的3种求法
- 2022-08-01 第八组 曹雨 泛型 枚举
- 【C语言】猜数字小游戏
- Based on php online learning platform management system acquisition (php graduation design)
- Mini Program--Independent Subcontracting & Subcontracting Pre-download
- File operations of WEB penetration
猜你喜欢
Delicious this year
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
感觉自己好傻
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
shell specification and variables
_ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
Shell programming conditional statement
教你VSCode如何快速对齐代码、格式化代码
Uses of Anacoda
User Experience | How to Measure User Experience?
随机推荐
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
数据分析面试手册《指标篇》
Small program -- subcontracting
小程序--分包
Spark练习题+答案
感觉自己好傻
AI应用第一课:支付宝刷脸登录
ModuleNotFoundError: No module named ‘yaml‘
图像融合GANMcC学习笔记
ImportError: `save_weights` requires h5py.问题解决
[ASM] Bytecode Operation MethodWriter
scikit-learn no moudule named six
[Mobile Web] Mobile terminal adaptation
Analysis of the development trend of game metaverse
File operations of WEB penetration
基于php在线考试管理系统获取(php毕业设计)
一种灵活的智能合约协作方式
MySQL related knowledge
小程序中的多表联合查询
【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读