Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is memoize supposed to work on getters? #15

Open
jrmyio opened this issue Feb 8, 2018 · 2 comments
Open

Is memoize supposed to work on getters? #15

jrmyio opened this issue Feb 8, 2018 · 2 comments

Comments

@jrmyio
Copy link

jrmyio commented Feb 8, 2018

(tested in Typescript)

import { memoize } from 'decko';

class Stores(){
}

class Container {

    @memoize
    public get stores () {
        return new Stores();
    }
}

Throws an error:
​​Invalid property descriptor. Cannot both specify accessors and a value or writable attribute, #<Object>​​

For reference: https://github.com/darrylhodgins/typescript-memoize does work.

@kiruh
Copy link

kiruh commented Aug 28, 2018

No, it's not working with getters since decorator method in source code doesn't check if you're applying decorator on regular method or getter:

function decorator(fn) {
    return opt => (
        typeof opt==='function' ? fn(opt) : (target, key, desc) => {
            desc.value = fn(desc.value, opt, target, key, desc);
        }
    );
}

This would work with getters:

function decorator(fn) {
    return opt => (
        typeof opt==='function' ? fn(opt) : (target, key, desc) => {
            if (desc.value) desc.value = fn(desc.value, opt, target, key, desc);
            if (desc.get) desc.get = fn(desc.get, opt, target, key, desc);
        }
    );
}

@developit
Copy link
Owner

PR welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants