instance-manager
Manage references to dynamically created classes and their instances.
Homepage | 🗃 Repository | 📦 NPM | 📚 Documentation | 🐛 Issue Tracker
🏠🪑 Table of Content
- 🧰 Features
- 🔮 Background
- 👶 Install
- 🚀 Usage
- 🤖 API
- ⏳ Changelog
- 🛠 Developing
- 🏗 Roadmap
- 🤝 Contributing
- 🧙 Contributors
- ⭐ Show your support
- 🐙 Community
- 🔗 Related Projects
- 👨🔧 Maintainers
- 📝 License
🧰 Features
- Useful for managing references to dynamically-created classes and their instances
- If you use class factory functions to create classes, you can store the class
config along with the class in
instance-manager
- Access classes and their instances using primitives (numbers)
- Classes and instances are weakly-references, so they will be garbage-collected
🔮 Background
This package is used to enable loader and plugin instances created from dynamically created classes to share information in Webpack, effectively using a single dynamically created instance to behave both as a loader and a plugin.
If you need that functionality, head over to ploadin, where the functionality has been abstracted.
This package can be used for more cases. The problem faced can be generalized as follows:
- There are dynamically created classes with unknown number of instances.
- You need to access a specific instance of a specific class.
- You are unable to pass the instance reference directly.
👶 Install
npm install instance-manager
Then in your file
const InstanceManager = default;const im = ;
Or if using TypeScript
;;
🚀 Usage
Simple usage
// -----// Setup// ----- // Import the packageconst InstanceManager = ; // Instantiateconst im = ; // Class factory that creates a class based on passed optionsconst classFactory = { { return optionsmessage; } return MyClass;}; // Create a dynamic classconst myClassOptions = param: 42 ;const MyDynamicClass = ; // -----------------------------// InstanceManager API - Classes// ----------------------------- // Add classesconst arrayClassId = im; // Optionally pass an object. Useful when working with dynamically created// classes where the object can be the parameters that were passed to the// factory functionconst myClassId = im; // Get classes by IDim === Array; // true // Or get class IDsim === arrayClassId; // true // Get class options (the 2nd argument)im === myClassOptions; // true // Or get class options by class IDim === myClassOptions; // true // Remove class and all its instances along from InstanceManagerim; // -------------------------------// InstanceManager API - Instances// ------------------------------- // Adds class instances.const instanceId1 = im; // Get instance idconst anotherInstance = ;const anotherId = im;const sameId = im;anotherId === sameId; // true // If the class hasn't been registered before it will be added too.const myNum = '2';const numInstanceId = im;im; // some number, so it is registered // Get instance having class reference and knowing instance IDim === anotherInstance; // true // Get instance knowing only class ID and instance IDconst klass = im;im === anotherInstance; // true // Remove single instanceim; // Remove instance by referenceim;
Advanced usage
Advanced usage is shown with TypeScript.
Problem
Imagine we have the following 3 files:
class-factory.ts
- defines a class factory.create-message.ts
- dynamically creates a class and instance. Sends a message that is read by listener.listener.ts
- reads message, needs to access an instance that created the message, but doesn't have a way to get the reference directly.
// class-factory.ts;
// create-message.ts;;;; // Dynamically create your class and instance;; ;; // Send signal that listener is waiting for. You can pass only strings.// In Webpack, this is the quivalent of specifying the path to the loadermessageQueue.sendmessage;
// listener.ts; messageQueue.listen;
Solution
We will use the instance-manager
to store references to each newly created
class and instance and we will attach these IDs as properties of those classes
and instance.
We will then send the IDs as strings through the message.
The listener will be able to read the IDs, and will be able to request the
correct instances from our instance of instance-manager
.
// provider.ts; // InstanceManager instance that will manage our dynamically created// instances and classes;;
// class-factory.ts; // This time, we register each created class and instance to instance-manager ;
// create-message.ts;;;; // Dynamically create your class and instance;; ;; // This time, we can access the IDs that were generated by registering the// class / instance with the InstanceManager; // So this time, we can pass the class and instance IDs to the listenermessageQueue.send`?classId=&instanceId=`;
// listener.ts;;; messageQueue.listen;
🤖 API
Full API documentation can be found here.
⏳ Changelog
This projects follows semantic versioning. The changelog can be found here.
🛠 Developing
If you want to contribute to the project or forked it, this guide will get you up and going.
🏗 Roadmap
This package is considered feature-complete. However, if you have ideas how it could be improved, please be sure to share it with us by opening an issue.
🤝 Contributing
Contributions, issues and feature requests are welcome! Thank you ❤️
Feel free to dive in! See current issues, open an issue, or submit PRs.
How to report bugs, feature requests, and how to contribute and what conventions we use is all described in the contributing guide.
When contributing we follow the Contributor Covenant. See our Code of Conduct.
🧙 Contributors
Contributions of any kind welcome. Thanks goes to these wonderful people ❤️
Recent and Top Contributors
Generated using Hall of Fame.
All Contributors
Contribution type emoji legend
No additional contributors. Be the first one!
This project follows the all-contributors specification.
⭐ Show your support
Give a ⭐️ if this project helped you!
🐙 Community
🔗 Related Projects
- ploadin - Webpack plugin and loader in one
- mini-extract-plugin - Generalized extensible variation of mini-css-extract-plugin that can be used to create plugins to extract text from custom file types
👨🔧 Maintainers
👤 Juro Oravec
- Twitter: @JuroOravec
- GitHub: @JuroOravec
- LinkedIn: @jurooravec
- Sourcerer: @JuroOravec
📝 License
Copyright © 2020 Juro Oravec.
This project is MIT licensed.