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

Proposal for a Plugin System to Extend Colorus-js Functionality #16

Closed
supitsdu opened this issue Aug 3, 2024 · 3 comments · Fixed by #17
Closed

Proposal for a Plugin System to Extend Colorus-js Functionality #16

supitsdu opened this issue Aug 3, 2024 · 3 comments · Fixed by #17
Labels
💬 Discussions Initiate discussions or seek feedback on project-related topics, decisions, or proposals. ✨ Enhancement New feature or request 🙋 Help Wanted Extra attention is needed

Comments

@supitsdu
Copy link
Owner

supitsdu commented Aug 3, 2024

Description:

As Colorus-js continues to grow, I believe it's time to explore the addition of a plugin system. This would allow users to extend the core functionality of the library with their own custom features, color spaces, and color manipulation algorithms.

Motivation:

  • Keep the Core Lean: The core library can remain focused on essential color manipulation tasks, avoiding bloat from features that not all users need.
  • Community-Driven Innovation: Empower users to create and share plugins, fostering a vibrant ecosystem around Colorus-js.
  • Flexibility: Cater to a wider range of use cases by enabling specialized plugins for specific needs.

Proposed Goals:

  • Intuitive API: Design a clear and easy-to-use API for plugin developers to interact with Colorus-js.
  • Plugin Discovery: Establish a mechanism for users to discover and install plugins (e.g., a plugin registry or repository).
  • Dependency Management: Provide a way to handle plugin dependencies gracefully.
  • Security: Ensure that plugins can be loaded and executed safely, minimizing potential vulnerabilities.

Possible Challenges:

  • API Design: Finding the right balance between flexibility and ease of use for plugin developers.
  • Plugin Discovery: Determining the most effective way for users to find and install plugins.
  • Security: Implementing safeguards to protect against malicious or poorly designed plugins.

Next Steps:

  1. Gather Feedback: Collect input from the community on the overall concept, potential use cases, and any concerns.
  2. Design API: Collaborate with potential contributors to draft an initial plugin API.
  3. Prototype: Build a proof-of-concept implementation to test the API and gather feedback.
  4. Iterate: Refine the design based on feedback and continue development.

Call to Action:

I invite everyone to share their thoughts and ideas on this proposal. Please comment below with your feedback, suggestions, or interest in contributing to this project.

@supitsdu supitsdu added ✨ Enhancement New feature or request 🙋 Help Wanted Extra attention is needed 💬 Discussions Initiate discussions or seek feedback on project-related topics, decisions, or proposals. labels Aug 3, 2024
@supitsdu
Copy link
Owner Author

supitsdu commented Aug 3, 2024

Hey everyone,

I've been working on a refined approach for implementing the plugin system in Colorus-js. Here's a summary of the key features of this new design:

Key Features:

  • use Method: A new method on the Colorus class allows you to extend instances with plugin functionalities.
  • Plugin Functions: Plugins are simply functions that take a Colorus instance as input and return an object containing the methods they want to add.
  • Encapsulation: Each plugin operates on a new copy of the Colorus instance, preventing conflicts between plugins.
  • Chaining: You can chain multiple use calls to apply different plugins to the same color.

Example:

const color = new Colorus('rgb(20 120 80)').use(colorInstance => ({
  getHue: () => colorInstance.hsl.h
}))

console.log(color.getHue()) // Return: 156
console.log(color.darken(0.1).toHex()) // Return: '#126C48'

I'd love to get your feedback on this approach! Do you think it provides a clean and flexible way to extend Colorus-js functionality? Are there any specific use cases or challenges you envision that this approach might need to address?

Feel free to share your thoughts and suggestions below.

@supitsdu
Copy link
Owner Author

supitsdu commented Aug 3, 2024

Here's another idea for how we could implement the plugin system in Colorus-js that I'd love to get your thoughts on:

Alternative Approach: Constructor-Based Configuration

Instead of having a separate use method, we could allow users to configure plugins directly in the Colorus constructor. Here's how it would work:

  1. Plugin Options: Add a plugins option to the Colorus constructor's options object.
  2. Plugin Array: Users pass an array of plugin functions to the plugins option.
  3. Automatic Registration: The Colorus constructor iterates over the plugin functions, applies them to the new instance, and extends it with the plugin methods.

Example:

const color = new Colorus('rgb(20 120 80)', {
  plugins: [
    colorInstance => ({
      getHue: () => colorInstance.hsl.h
    })
  ]
})

console.log(color.getHue()) // Returns: 156
console.log(color.darken(0.1).toHex()) // Returns: '#126C48'

Potential Benefits:

  • Simpler: This approach avoids the need for a separate use method, making the API potentially cleaner and more intuitive.
  • Concise: Plugin configuration is done directly where the Colorus instance is created, improving readability.
  • Flexible: It still allows for complex plugins to be defined and applied on a per-instance basis.

Considerations:

  • Plugin Ordering: We might need to consider how to handle the order in which plugins are applied if multiple plugins are provided.

Feedback:

I'm curious to hear your thoughts on this alternative approach! Would this be a more user-friendly way to implement plugins in Colorus-js? Do you see any potential drawbacks or challenges we should be aware of? Your feedback would be greatly appreciated!

@supitsdu
Copy link
Owner Author

supitsdu commented Aug 3, 2024

After considering different approaches, I'm leaning toward this plugin system design:

const color = new Colorus('rgb(20, 120, 80)', {
  plugins: {
    getHue: function() { return this.hsl.h },
    // ... other plugin methods
  }
});

Why I Think This Design is Best

  1. Clean and Concise Syntax: The key-value object for defining plugins is very intuitive and readable. It directly maps method names to their implementations, making it easy to understand what a plugin does at a glance.

  2. Implicit this Binding: By using regular functions for plugins, we leverage JavaScript's natural method binding behavior. This means the this keyword inside the plugin function automatically refers to the Colorus instance, simplifying the code and avoiding the need for manual binding.

  3. No Intermediate Objects: Unlike some other approaches that require returning an object of methods from the plugin function, this design directly assigns the plugin function as the method on the Colorus instance. This eliminates an extra step and makes the code more streamlined.

  4. Flexibility: While concise, this design doesn't sacrifice flexibility. You can still define complex plugins with multiple methods or even access the Colorus instance's data directly within the plugin function.

  5. Familiar Pattern: This pattern is similar to how options are often passed to JavaScript constructors, making it feel familiar to developers.

Comparison to Other Approaches

  • Loading with .loadPlugin() and .use(): This was a good option, but it introduced two additional methods and required remembering to call use() after loading a plugin.
  • Array of Plugin Functions: While functional, this approach felt less intuitive than using a key-value object to define plugins.
  • Directly Extending the Prototype: While possible, directly modifying the prototype is generally considered less flexible and can lead to unexpected issues if multiple plugins try to modify the same method.

Caveats:

  • Method Name Conflicts: We need to be mindful of potential conflicts with existing Colorus methods. We can add a warning or error message if a plugin tries to overwrite a core method.
  • Arrow Functions: It's important to note that arrow functions cannot be used for plugins as they don't have their own this binding.

Next Steps:

With this design in mind, I'd like to move forward with implementation and error handling. I'd appreciate any feedback or suggestions you might have on this approach before I start coding.

Let me know what you think!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💬 Discussions Initiate discussions or seek feedback on project-related topics, decisions, or proposals. ✨ Enhancement New feature or request 🙋 Help Wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant