Created
October 27, 2022 23:41
-
-
Save semlinker/3c656dff2a6ee4e538860a2ffe8528cd to your computer and use it in GitHub Desktop.
Proxy Pattern in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HttpServiceProxy { | |
private httpService: HttpService; | |
private dataCache: Map<string, any> = new Map(); | |
constructor() { | |
this.httpService = new HttpService(); | |
} | |
async sendRequest(method: string, url: string, body?: BodyInit) { | |
const cacheKey = method + ":" + url; | |
if (this.dataCache.has(cacheKey)) { | |
console.log(`Use key: ${cacheKey} to get cached data`); | |
return this.dataCache.get(cacheKey); | |
} | |
const response = await this.httpService.sendRequest(method, url, body); | |
this.dataCache.set(cacheKey, response); | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment