Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion modules/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
* The store module provides a central storage mechanism for managing and sharing data across your application.
* It allows you to create, retrieve, and update variables within a private store.
*/
import { nanoid } from 'nanoid';

const store = () => {
const _store = {};
const _subscriptions = [];

/**
* Creates the initial store by accepting an object with key-value pairs. This function throws an error
Expand Down Expand Up @@ -56,9 +59,41 @@ const store = () => {
}

_store[key] = newValue;

// Trigger all subscribed callbacks with the updated state
_subscriptions.forEach(sub => sub.callback(_store));
};

/**
* Subscribes a callback function to be invoked whenever the state changes.
*
* @param {Function} callback - The callback function to be invoked when state changes.
* @returns {number} A unique subscription ID that can be used to unsubscribe.
*/
const subscribe = (callback) => {
if (typeof callback !== 'function') {
throw new Error('Callback must be a function');
}

const id = nanoid();
_subscriptions.push({ id, callback });

return id;
};

/**
* Unsubscribes a callback function using its subscription ID.
*
* @param {string} subscriptionId - The unique ID returned by subscribe().
*/
const unsubscribe = (subscriptionId) => {
const index = _subscriptions.findIndex(sub => sub.id === subscriptionId);
if (index > -1) {
_subscriptions.splice(index, 1);
}
};

return { createStore, getState, updateState };
return { createStore, getState, updateState, subscribe, unsubscribe };
};

export default store();
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
},
"lint-staged": {
"**/*": "prettier --write --ignore-unknown"
},
"dependencies": {
"nanoid": "^5.1.6"
}
}