From bac3b409d2fb170aaa3454b50e243ab4725da6a2 Mon Sep 17 00:00:00 2001 From: Bryce Thorup Date: Thu, 6 Nov 2025 14:09:13 -0700 Subject: [PATCH 1/2] 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", -- 2.49.1 From 020299f82ac4f67bb8647f2cb710e1dd24fadbd4 Mon Sep 17 00:00:00 2001 From: Bryce Thorup Date: Thu, 6 Nov 2025 14:18:49 -0700 Subject: [PATCH 2/2] added stream dep --- package-lock.json | 20 ++++++++++++++++++++ package.json | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..0af48fb --- /dev/null +++ b/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "interactor", + "version": "1.0.4", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "interactor", + "version": "1.0.4", + "dependencies": { + "stream": "^1.0.0" + } + }, + "node_modules/stream": { + "version": "1.0.0", + "resolved": "https://git.thorup.us/api/packages/bryce/npm/stream/-/1.0.0/stream-1.0.0.tgz", + "integrity": "sha512-JGDjmgbnZcPCiQUxsboU7UufAryyivajDR3sCD790N/xfGpzlfl7OpPXf7EodDc43ojE+8dU8c3MOGbsH/Er4g==" + } + } +} diff --git a/package.json b/package.json index 4ac8557..191ae7c 100644 --- a/package.json +++ b/package.json @@ -4,5 +4,8 @@ "description": "Cross-element communications and state management utility.", "author": "Bryce Thorup", "type": "module", - "main": "index.js" + "main": "index.js", + "dependencies": { + "stream": "^1.0.0" + } } -- 2.49.1