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

Does method replace/reuse break the basic use case? #9

Open
senocular opened this issue May 16, 2022 · 0 comments
Open

Does method replace/reuse break the basic use case? #9

senocular opened this issue May 16, 2022 · 0 comments

Comments

@senocular
Copy link

Given this example from the readme:

const METADATA = new WeakMap();

function meta(value) {
  return (_, context) => {
    METADATA.set(context.metadataKey, value);
  };
}

@meta('a')
class C {
  @meta('b')
  m() {}
}

METADATA.get(C[Symbol.metadata]); // 'a'
METADATA.get(C.m[Symbol.metadata]); // 'b'

Would this fall apart if another decorator was involved that also replaced the method with a function that it could also use for other replacements? For example something like...

const env = { inProd: true }
const NO_OP = () => {}

function noopInProd() {
  if (env.inProd) {
    return NO_OP
  }
}

Which when used with the original example (with an additional method)

@meta('a')
class C {
  @meta('b')
  @noopInProd
  m() {}

  @meta('c')
  @noopInProd
  m2() {}
}

METADATA.get(C.m[Symbol.metadata]); // 'c'?
METADATA.get(C.m2[Symbol.metadata]); // 'c'?

Since these methods would now be the same function, wouldn't they give the same metadata? (Also, I assume the gets are supposed to be referring to the methods through C.prototype rather than C directly?)

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

No branches or pull requests

1 participant