1.0.4 #5

Merged
bryce merged 2 commits from 1.0.4 into main 2025-11-06 14:19:07 -07:00
3 changed files with 54 additions and 2 deletions
Showing only changes of commit bac3b409d2 - Show all commits

View File

@@ -1,2 +1,2 @@
import { Interactor, Action, Reaction } from "./src/interactor.js"; import { Interactor, Action, Reaction } from "./interactor.js";
export { Interactor, Action, Reaction }; export { Interactor, Action, Reaction };

52
interactor.js Normal file
View File

@@ -0,0 +1,52 @@
import { Stream } from './stream.js';
export class Interactor {
_actions = new Stream();
_reactions = new Stream();
_latestReaction;
constructor(initialReaction = new Reaction()) {
this._latestReactions = {};
this._actions.listen((action) => this._interaction(action));
this._react(initialReaction);
}
act(action) {
if (!(action instanceof Action)) throw 'invalid action';
this._actions.add(action);
}
observe(reactionHandler, provideLatestReaction = true) {
if (provideLatestReaction) reactionHandler(this._latestReaction);
let listener = (reaction) => {
if (!(reaction instanceof Reaction)) throw 'invalid reaction';
reactionHandler(reaction);
};
this._reactions.listen(listener);
return listener;
}
ignore(listener) {
this._reactions.ignore(listener);
}
_interaction(action) {}
_react(reaction) {
if (!(reaction instanceof Reaction)) throw 'invalid reaction';
let latest = this._latestReactions[reaction.constructor.name];
if (!reaction.equals(latest)) {
this._latestReaction = reaction;
this._latestReactions[reaction.constructor.name] = reaction;
this._reactions.add(reaction);
}
}
}
export class Action {}
export class Reaction {
equals(reaction) {
return false;
}
}

View File

@@ -1,6 +1,6 @@
{ {
"name": "interactor", "name": "interactor",
"version": "1.0.3", "version": "1.0.4",
"description": "Cross-element communications and state management utility.", "description": "Cross-element communications and state management utility.",
"author": "Bryce Thorup", "author": "Bryce Thorup",
"type": "module", "type": "module",