diff --git a/index.js b/index.js deleted file mode 100644 index 46ba5f1..0000000 --- a/index.js +++ /dev/null @@ -1,19 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -__exportStar(require("./lib/StreamStore"), exports); -__exportStar(require("./lib/StreamDebugLogger"), exports); -__exportStar(require("./lib/StreamHistory"), exports); diff --git a/jest.config.js b/jest.config.js deleted file mode 100644 index b413e10..0000000 --- a/jest.config.js +++ /dev/null @@ -1,5 +0,0 @@ -/** @type {import('ts-jest').JestConfigWithTsJest} */ -module.exports = { - preset: 'ts-jest', - testEnvironment: 'node', -}; \ No newline at end of file diff --git a/lib/StreamDebugLogger.js b/lib/StreamDebugLogger.js deleted file mode 100644 index 83e61ad..0000000 --- a/lib/StreamDebugLogger.js +++ /dev/null @@ -1,70 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.StreamDebugLogger = void 0; -/** - * A class for logging stream debug messages. - */ -var StreamDebugLogger = /** @class */ (function () { - /** - * Creates a new instance of StreamDebugLogger. - * @param streamStore The StreamStore instance to listen for events from. - */ - function StreamDebugLogger(streamStore) { - var _this = this; - this.streamStore = streamStore; - this.streamStore.storeEvents$.subscribe(function (event) { - switch (event.type) { - case 'actionCreated': - _this.logActionCreation(event.data.name); - break; - case 'actionDestroyed': - _this.logActionDestruction(event.data.name); - break; - case 'actionTriggered': - _this.logDataChanges(event.data.name, event.data.current, event.data.changed); - break; - case 'effectTriggered': - _this.logEffect(event.data.name); - } - }); - } - /** - * Logs a message to the console. - * @param message The message to log. - */ - StreamDebugLogger.prototype.log = function (message) { - console.info("[StreamStrore]".concat(message)); - }; - /** - * Logs changes to the data in a stream. - * @param actionName The name of the stream that was triggered. - * @param oldData The previous data in the stream. - * @param newData The new data in the stream. - */ - StreamDebugLogger.prototype.logDataChanges = function (actionName, oldData, newData) { - console.info("[".concat(actionName, "] triggered: "), oldData, newData); - }; - /** - * Logs the creation of a new stream. - * @param actionName The name of the stream that was created. - */ - StreamDebugLogger.prototype.logActionCreation = function (actionName) { - console.info("[".concat(actionName, "] action created")); - }; - /** - * Logs the destruction of a stream. - * @param actioName The name of the stream that was destroyed. - */ - StreamDebugLogger.prototype.logActionDestruction = function (actioName) { - console.info("[".concat(actioName, "] destroyed")); - }; - /** - * Logs the triggering of an effect. - * @param actionName The name of the stream that triggered the effect. - */ - StreamDebugLogger.prototype.logEffect = function (actionName) { - console.info("[".concat(actionName, "] effect triggered")); - }; - return StreamDebugLogger; -}()); -exports.StreamDebugLogger = StreamDebugLogger; diff --git a/lib/StreamHistory.js b/lib/StreamHistory.js deleted file mode 100644 index fcd19d8..0000000 --- a/lib/StreamHistory.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.StreamHistory = void 0; -var StreamHistory = /** @class */ (function () { - function StreamHistory() { - } - return StreamHistory; -}()); -exports.StreamHistory = StreamHistory; diff --git a/lib/StreamStore.js b/lib/StreamStore.js deleted file mode 100644 index 2fd421a..0000000 --- a/lib/StreamStore.js +++ /dev/null @@ -1,120 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.StreamStore = void 0; -var rxjs_1 = require("rxjs"); -/** - * A store for managing and observing streams of data. - */ -var StreamStore = /** @class */ (function () { - function StreamStore() { - /** - * A dictionary of registered stream subjects. - */ - this.actions = {}; - /** - * A dictionary of stored data for each stream. - */ - this.storeData = {}; - /** - * A subject for emitting stream store events. - */ - this.storeEvents = new rxjs_1.Subject(); - } - Object.defineProperty(StreamStore.prototype, "storeEvents$", { - /** - * An observable for stream store events. - */ - get: function () { - return this.storeEvents.asObservable(); - }, - enumerable: false, - configurable: true - }); - /** - * Sets the data for a given action stream. - * @param actionName The name of the action stream to set data for. - * @param data The data to set for the action stream. - */ - StreamStore.prototype.setStoreData = function (actionName, data) { - this.emmitStoreEvent('actionTriggered', { name: actionName, current: this.storeData[actionName], changed: data }); - this.storeData[actionName] = Object.freeze(data); - }; - /** - * Emits a stream store event with the given type and data. - * @param type The type of the stream store event. - * @param data The data related to the stream store event. - */ - StreamStore.prototype.emmitStoreEvent = function (type, data) { - this.storeEvents.next({ - type: type, - data: data - }); - }; - /** - * Creates a new action stream with the given name and initial data. - * @param name The name of the action stream to create. - * @param initialData The initial data for the action stream. - * @throws An error if a stream with the given name already exists. - */ - StreamStore.prototype.createAction = function (name, initialData) { - if (this.actions[name]) { - throw new Error("(StreamStore: ".concat(name, ") There is a stream with that name already registered")); - } - this.actions[name] = new rxjs_1.Subject(); - this.emmitStoreEvent('actionCreated', { name: name }); - this.dispatch(name, initialData); - }; - /** - * Registers a side effect that should be executed whenever a given action is dispatched - * @template T - The type of data the action dispatches - * @param {string} actionName - The name of the action to register the side effect for - * @param {(actionData: T, store: StreamStore) => void} effect - A function that represents the side effect to execute when the action is dispatched - * @returns {void} - */ - StreamStore.prototype.createEffect = function (actionName, effect) { - var _this = this; - this.actions[actionName].subscribe(function (data) { - _this.emmitStoreEvent('effectTriggered', { name: actionName }); - effect(data, _this); - }); - }; - /** - * Retrieves the latest data for a given action stream. - * @template T - The type of data associated with the action stream. - * @param {string} actionName - The name of the action stream to retrieve data for. - * @returns {T} The data associated with the specified action stream. - */ - StreamStore.prototype.data = function (actionName) { - return this.storeData[actionName]; - }; - /** - * Gets an observable for the given action stream. - * @param actionName The name of the action stream to get an observable for. - * @returns An observable for the action stream. - */ - StreamStore.prototype.action = function (actionName) { - return this.actions[actionName].asObservable().pipe((0, rxjs_1.startWith)(this.storeData[actionName])); - }; - /** - * Dispatches data to the given action stream. - * @param actionName The name of the action stream to dispatch data to. - * @param payload The data to dispatch to the action stream. - */ - StreamStore.prototype.dispatch = function (actionName, payload) { - if (payload === void 0) { payload = null; } - this.setStoreData(actionName, payload); - this.actions[actionName].next(this.storeData[actionName]); - }; - /** - * Destroys the given action stream and all related data in the store. - * @param actionName The name of the action stream to destroy. - */ - StreamStore.prototype.destroyAction = function (actionName) { - this.actions[actionName].complete(); - delete this.storeData[actionName]; - delete this.actions[actionName]; - this.emmitStoreEvent('actionDestroyed', { name: actionName }); - }; - return StreamStore; -}()); -exports.StreamStore = StreamStore; diff --git a/package.json b/package.json index e4fa3da..005d00d 100644 --- a/package.json +++ b/package.json @@ -12,13 +12,16 @@ "data flow", "data management" ], - "main": "index.ts", + "main": "dist/index.js", "repository": { "type": "git", "url": "https://github.com/lexmihaylov/driftrx.git" }, "scripts": { - "build": "tsc index.ts", + "build": "tsc index.ts --outDir ./dist --declaration", + "clean": "rm -rf dist", + "prepare": "npm run build", + "postpublish": "npm run clean", "test": "jest --silent --verbose --collect-coverage tests" }, "author": "Alexander Mihaylov (https://lexmihaylov/github.io)", @@ -31,4 +34,4 @@ "jest": "^29.5.0", "ts-jest": "^29.1.0" } -} +} \ No newline at end of file