{"id":2207,"date":"2019-06-18T09:59:29","date_gmt":"2019-06-18T08:59:29","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2207"},"modified":"2022-03-28T16:07:19","modified_gmt":"2022-03-28T15:07:19","slug":"typescript-cachedpromise","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-cachedpromise\/","title":{"rendered":"Typescript CachedPromise"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { dateAdd } from \".\/DateHelpers\";\n\nexport class CachedPromise&lt;T> {\n    private _dataPromise: PromiseLike&lt;T> | undefined = undefined;\n    private _dataDate: Date | undefined;\n    constructor(private _dataFn: () => PromiseLike&lt;T>, private _validMinutes: number = 20) { }\n    async load(reload: boolean = false) {\n        if (!this._dataPromise || reload || this.isExpired()) {\n            this._dataDate = new Date();\n            return this.reload();\n        }\n        return await this._dataPromise;\n    }\n\n    isExpired(): boolean {\n        const expiryDate = dateAdd(new Date(), -1 * this._validMinutes * 60 * 1000);\n        const isExpired = !this._dataDate || this._dataDate &lt; expiryDate;\n        return isExpired;\n    }\n\n    setExpired() {\n        this._dataDate = undefined;\n    }\n\n    async reload() {\n        this._dataPromise = this._dataFn();\n        return await this._dataPromise;\n    }\n}\n\n\/\/ usage:\nconst measurementsPromise = new CachedPromise&lt;any>(() => restApiHelper.getData(\"api\/measurements\"), 10);\n\nmeasurementsPromise.load();\nmeasurementsPromise.load(); \/\/ cached\nmeasurementsPromise.load(true); \/\/ not cached\nmeasurementsPromise.reload(); \/\/ not cached\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2207","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2207"}],"version-history":[{"count":8,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2207\/revisions"}],"predecessor-version":[{"id":6099,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2207\/revisions\/6099"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}