当前位置:网站首页>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.
边栏推荐
猜你喜欢

使用分类权重解决数据不平衡的问题

HCIP---Multiple Spanning Tree Protocol related knowledge points

Small program -- subcontracting

Pagoda application experience

HCIP---Architecture of Enterprise Network

Image fusion GANMcC study notes

render-props and higher order components

shell specification and variables

Based on php film and television information website management system acquisition (php graduation design)

上传markdown文档到博客园
随机推荐
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
小程序--独立分包&分包预下载
Recycling rental system 100% open source without encryption Mall + recycling + rental
Scala practice questions + answers
【Objective-C中的@synthesize】
【C语言实现】求两个整数的较大值
Yizhou Financial Analysis | The intelligent transformation of bank ATM machines is accelerated; the new Internet loan regulations bring challenges
Shell programming conditional statement
Based on php film and television information website management system acquisition (php graduation design)
Based on php hotel online reservation management system acquisition (php graduation project)
render-props和高阶组件
Getting Started Database Days4
SOM网络2: 代码的实现
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
HCIP---Architecture of Enterprise Network
程序员必备的 “ 摸鱼神器 ” 来了 !
number of solutions to solve a multivariate multi-degree equation
WEB 渗透之文件类操作
groupByKey和reduceBykey的区别
求解多元多次方程解的个数