medikoo / memoizee

Complete memoize/cache solution for JavaScript
ISC License
1.73k stars 61 forks source link

memoizee in TypeScript's class? #104

Closed jacek-jablonski closed 5 years ago

jacek-jablonski commented 5 years ago

Hi I would like to use memoizee in TypeScript's class, f.e.:

import memoize from "memoizee"
import { getModule, Module, MutationAction, VuexModule } from "vuex-module-decorators"
import store from "@/store"
import { User } from "./models"
import { UserAPI } from "./api"

@Module({ namespaced: true, name: "user", store, dynamic: true })
class UserModule extends VuexModule {
  user: User | null = null
  getUser = memoize(UserAPI.getUser, { maxAge: 10 * 60 * 1000, promise: true })

  @MutationAction
  async fetchProfile() {
    const user = await this.getUser()
    return { user }
  }
}

export default getModule(UserModule)

However, it complains that: I have TypeError: this.getUser is not a function. Why is that? What is proper usage of memoizee in TypeScript's classes?

medikoo commented 5 years ago

It looks to me as some syntax issue. From error message it's clear that there's no getUser method accessible from context of fetchProfile method.

I'm not perfectly familiar with TS syntax, so it's hard for me to help you.

Also memoizee is a plain JS project, and here code snippets should also be posted in plain JS (one that runs with no transpilation in modern engines).

I suggest to post it on Stack Overflow, and address TypeScript community with that question. They definitely will help you solve this.

msheep commented 4 years ago

You can try

getUser = () => memoize(UserAPI.getUser, { maxAge: 10 60 1000, promise: true })