前端性能优化:使用SingleFlight 合并请求

在Go语言中,有一个名为golang.org/x/sync/singleflight的包,它提供了SingleFlight的实现,以确保多个调用只执行一次。。在处理多个组件或模块需要从后端获取数据的情况下,为了提高效率,我们也可以使用 SingleFlight 技巧,确保多个组件对相同接口的调用只发起一次请求,从而减少不必要的网络开销。本文将介绍如何在 Angular 应用中实现 SingleFlight,并为所有的 HTTP GET 请求添加该优化机制。Vue,React可以类似处理。

1. SingleFlightService 的创建

首先,我们需要创建一个 SingleFlightService 服务,该服务负责管理正在执行的请求。以下是 SingleFlightService 的基本实现:

typescript 复制代码
// singleflight.service.ts

import { Injectable } from '@angular/core';
import { from, Observable } from 'rxjs';
import { shareReplay } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class SingleFlightService {
  private inFlightRequests: { [key: string]: Observable<any> } = {};

  execute<T>(key: string, action: () => Observable<T>): Observable<T> {
    console.log('execute key', key)
    if (!this.inFlightRequests[key]) {
      this.inFlightRequests[key] = from(action()).pipe(
        shareReplay(1)
      );
    }
    return this.inFlightRequests[key];
  }
}

2. SingleFlightInterceptor 的创建

接下来,我们需要创建一个拦截器 SingleFlightInterceptor,该拦截器负责拦截 HTTP GET 请求并利用 SingleFlightService 来实现单飞功能。以下是 SingleFlightInterceptor 的基本实现:

typescript 复制代码
// singleflight.interceptor.ts

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SingleFlightService } from './singleflight.service';

@Injectable()
export class SingleFlightInterceptor implements HttpInterceptor {
  constructor(private singleFlightService: SingleFlightService) { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // Check if it's a GET request
    if (req.method === 'GET') {
      const key = req.url; // You can customize the key based on your requirements
      return this.singleFlightService.execute(key, () => next.handle(req));
    }

    // For non-GET requests, proceed without modification
    return next.handle(req);
  }
}

3. 在模块中注册拦截器

最后,在你的应用程序模块中,将 SingleFlightInterceptor 添加到 HTTP_INTERCEPTORS 提供商列表,以便在整个应用程序中生效:

typescript 复制代码
// app.module.ts

import { NgModule } from '@angular/core';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { SingleFlightInterceptor } from './singleflight.interceptor';

@NgModule({
  imports: [HttpClientModule],
  providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: SingleFlightInterceptor,
      multi: true
    }
  ]
})
export class YourAppModule { }

4.不想全局使用可以单独封装一个service

typescript 复制代码
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SingleFlightService {
  private inFlightRequests: { [key: string]: Promise<any> } = {};

  execute<T>(key: string, action: () => Promise<T>): Promise<T> {
    console.log('execute key', key)
    if (!this.inFlightRequests[key]) {
      this.inFlightRequests[key] = action().then(data => {
        // Clear the in-flight flag once the promise is resolved
        delete this.inFlightRequests[key];
        return data;
      }).catch(error => {
        // Handle errors here if needed
        console.error(`Error in single flight request for key ${key}:`, error);
        // Clear the in-flight flag in case of an error
        delete this.inFlightRequests[key];
        throw error;
      });
    }
    return this.inFlightRequests[key];
  }
}

// 使用方式
this.singleFlightService.execute(someKey, ()=> somePromseReq());
相关推荐
拉不动的猪8 分钟前
无缝适配 PC 和移动端‌我们要注意哪些点呢
前端·javascript·面试
酱酱们的每日掘金39 分钟前
🔥 4 月精选:AICoding Cursor上新与 MCP 实战揭秘!- AI Coding 周刊第 5 期
前端·ai编程·mcp
天天扭码1 小时前
一分钟解决 | 高频面试算法题——和为 K 的子数组(前缀和)
前端·算法·面试
搞瓶可乐1 小时前
鸿蒙ArkUI之布局实战,线性布局(Column,Row)、弹性布局(Flex)、层叠布局(Stack),详细用法
前端·harmonyos·鸿蒙系统·arkui·弹性布局·布局实战·堆叠布局
Aphasia3112 小时前
小厂面试常考算法题整合(一)✍🏻
前端·算法·面试
五月仲夏2 小时前
React基础知识(补充中)
前端·react.js·前端框架
王富贵的记录2 小时前
React 函数组件和类组件的区别
前端·javascript·react.js
yuhaiqiang2 小时前
在公司写代码是工作,在开源社区写代码是生活
前端·后端
左耳咚2 小时前
Egg.js 服务端 HTML 强缓存问题排查与解决
前端·egg.js
DevUI团队2 小时前
Electron 入门学习指南:快速搭建跨平台桌面应用
前端·javascript·electron