Code Monkey home page Code Monkey logo

Comments (5)

koresar avatar koresar commented on June 2, 2024

Hi Henrique.

You did it all right. That's the right way to implement the decorator pattern. To fix the little bug you can just mark your wrapper function with a flag or something.

const GreetDecorator = stampit.composers(({ stamp }) => {
    const { greet } = stamp.compose.methods || {};
    if (!greet || greet.wrapped) return;

    function greetWrapper(...args) {
      greet.apply(this, args);
      console.log('this is new!!!');
    }
    greetWrapper.wrapped = true;
    stamp.compose.methods.greet = greetWrapper;
  }
});

from stampit.

koresar avatar koresar commented on June 2, 2024

Side tip. You can make the method decorator stamp more universal and reusable via static methods.

const MethodDecorator = stampit.statics({
    decorateMethod(name, func) {
      return this.composers(({ stamp }) => {
        const method = stamp.compose.methods || stamp.compose.methods[name];
        if (!method || method.wrapped) return;

        function methodWrapper(...args) {
          method.apply(this, args);
          func.apply(this, args);
        }
        methodWrapper.wrapped = true;
        stamp.compose.methods.method = methodWrapper;
      }
    })
});

And usages:

MyStamp = MyStamp.compose(MethodDecorator).decorateMethod("greet", function () {
  console.log('this is new!!!');
});

The above code is not tested :)

from stampit.

koresar avatar koresar commented on June 2, 2024

Closing this issue considering all questions are answered. Feel free to reopen any time.

And thanks for the code. :) It sounds like a new Fun With Stamps episode.

from stampit.

hbarcelos avatar hbarcelos commented on June 2, 2024

Hi @koresar. Sorry for the long delay, things have been pretty crazy this year.

Your suggestions helped me a lot. I think I managed to pull a implementation that works very well for most use cases.

I was about to start writing some documentation before publishing it. As soon as I finish it, I'll post it here.

Thank you very much.

from stampit.

koresar avatar koresar commented on June 2, 2024

from stampit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.