get-svelte
Version:
A simple, powerful state management library for Svelte applications, inspired by GetX for Flutter
80 lines (79 loc) • 3.03 kB
JavaScript
/**
* Abstract base class for controllers in the Get system.
* Provides state management functionality and listener management for reactive updates.
* Controllers created from this base class can be registered and managed by the Get class.
*/
export default class GetxController {
constructor() {
/**
* Collection of listener functions that will be called when state changes
* @private
*/
this.listeners = [];
}
/**
* Registers a new listener function to be notified of state changes
* @param {Function} listener - The callback function to execute when state changes
*/
addListener(listener) {
this.listeners.push(listener);
}
/**
* Removes a previously registered listener function
* @param {Function} listener - The callback function to remove from listeners
*/
removeListener(listener) {
this.listeners = this.listeners.filter(l => l !== listener);
}
/**
* Notifies all registered listeners of a state change
* Should be called by subclasses when their state changes
* @protected
*/
notifyListener() {
this.listeners.forEach(listener => listener());
}
/**
* Lifecycle hook called when the controller is initialized
* Can be overridden by subclasses to perform initialization tasks
* @protected
*/
onInit() {
// This method can be overridden by subclasses to perform actions when the controller is initialized.
// For example, you might want to set up initial state or subscribe to events.
}
/**
* Lifecycle hook called when the controller is closed/deleted
* Can be overridden by subclasses to perform cleanup tasks
*/
onClose() {
// This method can be overridden by subclasses to perform actions when the controller is deleted.
// For example, you might want to clean up resources or unsubscribe from events.
this.listeners = [];
}
/**
* Sets the inner caller for this controller
* This is typically called by the Get class when registering the controller
* @param {InnerCaller} innerCaller - The inner caller instance
* @param {Function} onDisposeCallBack - Callback function to execute when the controller is disposed
*/
setInnerCaller(innerCaller, onDisposeCallBack) {
this.innerCaller = innerCaller;
this.innerCaller.close = this.onClose.bind(this);
this.innerCaller.init = this.onInit.bind(this);
this.innerCaller.onDisposeCallBack = onDisposeCallBack;
}
/**
* Disposes of this controller instance
* Triggers the disposal callback which typically removes it from the Get registry
* @returns {any} Result of the disposal callback
*/
dispose() {
return this.innerCaller.onDisposeCallBack();
}
}
// Prevent overwriting the dispose method
Object.defineProperty(GetxController.prototype, 'dispose', {
writable: false,
configurable: false,
});