当前位置:网站首页>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.
边栏推荐
- familiar friend
- 高等代数_证明_矩阵的行列式为特征值之积, 矩阵的迹为特征值之和
- Uses of Anacoda
- Based on php online examination management system acquisition (php graduation design)
- Centos7--MySQL的安装
- leetcode 204. Count Primes 计数质数 (Easy)
- Unity Shader 常规光照模型代码整理
- Scala practice questions + answers
- 小程序--分包
- SAP ABAP OData 服务如何支持删除(Delete)操作试读版
猜你喜欢
19 Lectures on Disassembly of Multi-merchant Mall System Functions - Invoice Management on the Platform
(*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
C语言必杀技3行代码把运行速度提升4倍
Recycling rental system 100% open source without encryption Mall + recycling + rental
seaborn笔记:可视化统计关系(散点图、折线图)
小程序中的多表联合查询
WEB渗透之SQL 注入
10 Practical Uses of NFTs (NFT System Development)
第一讲 测试知多少
SQL injection of WEB penetration
随机推荐
今年的很美味
程序员必备的 “ 摸鱼神器 ” 来了 !
0DFS Medium LeetCode6134. Find the closest node to the given two nodes
统计单词数
2022 edition of MySQL tutorial, top collection good, take your time
2022 版 MySQL 巅峰教程,收藏好,慢慢看
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
AIDL communication
基于php酒店在线预定管理系统获取(php毕业设计)
小程序容器+自定义插件,可实现混合App快速开发
ModuleNotFoundError: No module named ‘yaml‘
工程建筑行业数据中台指标分析
LeetCode952三部曲之一:解题思路和初级解法(137ms,超39%)
FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
VGUgarbage collector(垃圾回收器)的实现原理
Mini Program--Independent Subcontracting & Subcontracting Pre-download
游戏元宇宙发展趋势展望分析
基于php在线考试管理系统获取(php毕业设计)
Pagoda application experience
SQL injection of WEB penetration