23 Commits

Author SHA1 Message Date
7f55ae0d4e Merge pull request 'added better error handling for missing action handlers' (#11) from 1.1.3 into main
All checks were successful
Push to main / publish (push) Successful in 12s
Reviewed-on: #11
2025-11-06 18:47:04 -07:00
6f52c56857 added better error handling for missing action handlers
All checks were successful
Pull request change / publish (pull_request) Successful in 10s
2025-11-06 18:44:04 -07:00
cfdcdf1def Merge pull request '1.1.2' (#10) from 1.1.2 into main
All checks were successful
Push to main / publish (push) Successful in 17s
Reviewed-on: #10
2025-11-06 18:34:28 -07:00
f8cdfe131f 1.1.2
All checks were successful
Pull request change / publish (pull_request) Successful in 15s
2025-11-06 18:33:33 -07:00
95f85aedaa Merge pull request 'version bump' (#9) from 1.1.1 into main
All checks were successful
Push to main / publish (push) Successful in 14s
Reviewed-on: #9
2025-11-06 15:54:18 -07:00
5c7cf36cf3 version bump
All checks were successful
Pull request change / publish (pull_request) Successful in 10s
2025-11-06 15:53:51 -07:00
2a84086828 Merge pull request 'force handler functions' (#8) from 1.1.0 into main
Some checks failed
Push to main / publish (push) Failing after 11s
Reviewed-on: #8
2025-11-06 15:51:52 -07:00
1dcdc17436 force handler functions
Some checks failed
Pull request change / publish (pull_request) Failing after 10s
2025-11-06 15:51:16 -07:00
466e95cb14 Merge pull request '1.0.6' (#7) from 1.0.6 into main
All checks were successful
Push to main / publish (push) Successful in 13s
Reviewed-on: #7
2025-11-06 15:00:58 -07:00
7ef7c44cf7 proper stream import
All checks were successful
Pull request change / publish (pull_request) Successful in 10s
2025-11-06 15:00:33 -07:00
bf31d5371e version bump 2025-11-06 15:00:18 -07:00
fc4e96ab37 Merge pull request 'stream ver up' (#6) from 1.0.5 into main
All checks were successful
Push to main / publish (push) Successful in 12s
Reviewed-on: #6
2025-11-06 14:57:38 -07:00
7af635a19b version bump
All checks were successful
Pull request change / publish (pull_request) Successful in 10s
2025-11-06 14:57:11 -07:00
665dd28f93 remove indirect ref
Some checks failed
Pull request change / publish (pull_request) Failing after 9s
2025-11-06 14:56:46 -07:00
e026dcdb15 stream ver up
Some checks failed
Pull request change / publish (pull_request) Failing after 10s
2025-11-06 14:55:53 -07:00
2102e9c8cf Merge pull request '1.0.4' (#5) from 1.0.4 into main
All checks were successful
Push to main / publish (push) Successful in 12s
Reviewed-on: #5
2025-11-06 14:19:07 -07:00
020299f82a added stream dep
All checks were successful
Pull request change / publish (pull_request) Successful in 13s
2025-11-06 14:18:49 -07:00
bac3b409d2 add code ya know 2025-11-06 14:09:13 -07:00
442f6055f6 Merge pull request 'runner worx??A?!?!?' (#4) from 1.0.3 into main
All checks were successful
Push to main / publish (push) Successful in 11s
Reviewed-on: #4
2025-11-06 13:53:40 -07:00
bd96c56445 runner worx??A?!?!?
All checks were successful
Pull request change / publish (pull_request) Successful in 10s
2025-11-06 13:53:22 -07:00
9978667571 Merge pull request 'b' (#3) from b into main
Some checks failed
Push to main / publish (push) Failing after 11s
Reviewed-on: #3
2025-11-06 13:51:20 -07:00
66173ac4f7 b
All checks were successful
Pull request change / publish (pull_request) Successful in 10s
2025-11-06 13:51:00 -07:00
85fbdce1d5 Merge pull request 'a' (#2) from a into main
Some checks failed
Push to main / publish (push) Failing after 12s
Reviewed-on: #2
2025-11-06 13:43:30 -07:00
5 changed files with 107 additions and 5 deletions

View File

@@ -17,4 +17,4 @@ jobs:
uses: https://git.thorup.us/actions/npm_publish@v1
with:
npm_repo_key: ${{ secrets.NPM_REPO_KEY }}
npm_repo_domain: "npm.thorup.us"
npm_repo_domain: ${{ vars.NPM_REPO_DOMAIN }}

View File

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

81
interactor.js Normal file
View File

@@ -0,0 +1,81 @@
import { Stream } from "stream";
export class Interactor {
#actions = new Stream();
#reactions = new Stream();
#latestReaction;
#latestReactions = {};
constructor(initialReaction = new Reaction()) {
this.#actions.listen((action) => {
let funcRef = `_handle${action.constructor.name}`;
if (!this[funcRef])
throw `${this.constructor.name} does not have a handler defined for the action "${action.constructor.name}". Create one by adding a function definition named "${funcRef}" in the ${this.constructor.name} class.`;
this[funcRef](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);
}
_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 ActionBundle {
add(action) {
if (!(action.prototype instanceof Action)) {
throw "Only classes extended by `Action` can be added to a `ActionBundle`";
}
let actionName = action.name;
this[`#${actionName}`] = action;
Object.defineProperty(this, actionName, {
get: () => this[`#${actionName}`],
});
}
}
export class Reaction {
equals(reaction) {
return false;
}
}
export class ReactionBundle {
add(reaction) {
if (!(reaction.prototype instanceof Reaction)) {
throw "Only classes extended by `Reaction` can be added to a `ReactionBundle`";
}
let reactionName = reaction.name;
this[`#${reactionName}`] = reaction;
Object.defineProperty(this, reactionName, {
get: () => this[`#${reactionName}`],
});
}
}

20
package-lock.json generated Normal file
View File

@@ -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=="
}
}
}

View File

@@ -1,8 +1,11 @@
{
"name": "interactor",
"version": "1.0.1",
"version": "1.1.3",
"description": "Cross-element communications and state management utility.",
"author": "Bryce Thorup",
"type": "module",
"main": "index.js"
"main": "interactor.js",
"dependencies": {
"stream": "^1.0.1"
}
}