From bac3b409d2fb170aaa3454b50e243ab4725da6a2 Mon Sep 17 00:00:00 2001 From: Bryce Thorup Date: Thu, 6 Nov 2025 14:09:13 -0700 Subject: [PATCH] add code ya know --- index.js | 2 +- interactor.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 interactor.js diff --git a/index.js b/index.js index c75bb3b..4e5350a 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,2 @@ -import { Interactor, Action, Reaction } from "./src/interactor.js"; +import { Interactor, Action, Reaction } from "./interactor.js"; export { Interactor, Action, Reaction }; diff --git a/interactor.js b/interactor.js new file mode 100644 index 0000000..eecb4d0 --- /dev/null +++ b/interactor.js @@ -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; + } +} diff --git a/package.json b/package.json index b5bfb35..4ac8557 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "interactor", - "version": "1.0.3", + "version": "1.0.4", "description": "Cross-element communications and state management utility.", "author": "Bryce Thorup", "type": "module",