decko icon indicating copy to clipboard operation
decko copied to clipboard

Is memoize supposed to work on getters?

Open jrmyio opened this issue 7 years ago • 2 comments

(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.

jrmyio avatar Feb 08 '18 12:02 jrmyio

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);
        }
    );
}

kiruh avatar Aug 28 '18 12:08 kiruh

PR welcome!

developit avatar Oct 12 '18 22:10 developit