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

Private protected API. #199

Closed
stefanpoeter opened this issue Mar 26, 2016 · 3 comments
Closed

Private protected API. #199

stefanpoeter opened this issue Mar 26, 2016 · 3 comments

Comments

@stefanpoeter
Copy link

Hey,

I am missing a private protected API and I would want to start a discussion on that. Or maybe there is already one and I've missed it.

For example: If I want to provide objects with a log method, I would do it like so:

var log = stampit.init(function () {

    this.log = console.log;

});

When I create new stamps based on this log stamp these objects could simply call the log method. Simple as that, but the log method is also exposed in the public API where I do not need it.

As a suggestion I would rather have it this way:

var log = stampit.init(({ instance, stamp }, private) => {

   private.log = console.log;

});

This way the private object is not exposed in the public API but can be used by composed stamps.

var a = stamp.compose(log).init(({ instance, stamp }, private) => {
    private.log('Some log method');
}

What do you think?

@koresar
Copy link
Member

koresar commented Mar 26, 2016

@BauchBeinePoe hello there.
Very good idea. I like it!

I'm actively working on stampit v3 which is going to be stamp-specification compatible. And your idea would perfectly fit the specification IMO.

The name should be protected because the object will be shared across several initializers. The "private" data already exist, it's the closure variables inside the initializers.

The initializer signature would be like that:

const Stamp = compose({
  initializers: [(options, { stamp, instance, args, protected }) => {
    protected.log('Some log method');
  }]
});

In stampit v3 the API would be this:

const Stamp = stampit.init((options, { stamp, instance, args, protected }) => {
  protected.log('Some log method');
});

Let's move this conversation here: stampit-org/stamp-specification#84

@koresar
Copy link
Member

koresar commented Apr 11, 2016

Thank you for the idea @BauchBeinePoe. As you know the community voted to not implement it. Although, a workaround using stampit v2 could be this:

var log = stampit.init(({ instance }) => {
   instance.log = console.log; // First, temporary assign the private state to the instance.
});

var a = stamp.compose(log).init(({ instance }) => {
    const log = instance.log;
    log('Some log method');
    delete instance.log; // Later, delete private state form the instance.
});

With the future stampit v3 (or the existing module stamp-specification) it could be implemented simpler:

var log = compose({initializers: [(options) => {
  options.log = options.log || console.log;
}]});

var a = compose({initializers: [(options) => {
  options.log('Some log method');
}]});

Although, stamps developed with stampit v2 and v3 will (most likely) be incompatible. :(

@koresar koresar closed this as completed Apr 15, 2016
@koresar
Copy link
Member

koresar commented Apr 30, 2017

Protected properties are now implemented as a separate stamp - https://www.npmjs.com/package/@stamp/privatize

But please note, the @stamp/* modules are compatible with @stamp/it (or @stamp/compose) only.

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

2 participants