-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
92e01ff
commit 35ce6ce
Showing
5 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
node_modules | ||
coverage | ||
*.js | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.StreamHistory = void 0; | ||
var StreamHistory = /** @class */ (function () { | ||
function StreamHistory() { | ||
} | ||
return StreamHistory; | ||
}()); | ||
exports.StreamHistory = StreamHistory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"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) { | ||
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; |