6 Commits

Author SHA1 Message Date
7e53db5785 Merge pull request 'make scoped' (#12) from 1.1.4 into main
All checks were successful
Push to main / publish (push) Successful in 13s
Reviewed-on: #12
2025-11-07 14:00:01 -07:00
f463dec7f1 make scoped
All checks were successful
Pull request change / publish (pull_request) Successful in 11s
2025-11-07 13:59:07 -07:00
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
2 changed files with 35 additions and 6 deletions

View File

@@ -7,9 +7,12 @@ export class Interactor {
#latestReactions = {}; #latestReactions = {};
constructor(initialReaction = new Reaction()) { constructor(initialReaction = new Reaction()) {
this.#actions.listen((action) => this.#actions.listen((action) => {
this[`handle${action.constructor.name}`](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); this._react(initialReaction);
} }
@@ -45,8 +48,34 @@ export class Interactor {
export class Action {} 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 { export class Reaction {
equals(reaction) { equals(reaction) {
return false; 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}`],
});
}
}

View File

@@ -1,11 +1,11 @@
{ {
"name": "interactor", "name": "@brycethorup/interactor",
"version": "1.1.0", "version": "1.1.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",
"main": "interactor.js", "main": "interactor.js",
"dependencies": { "dependencies": {
"stream": "^1.0.1" "@brycethorup/stream": "^1.0.1"
} }
} }