Compare commits
10 Commits
2a84086828
...
1.1.5
| Author | SHA1 | Date | |
|---|---|---|---|
| ad774185c2 | |||
| c513a54717 | |||
| 7e53db5785 | |||
| f463dec7f1 | |||
| 7f55ae0d4e | |||
| 6f52c56857 | |||
| cfdcdf1def | |||
| f8cdfe131f | |||
| 95f85aedaa | |||
| 5c7cf36cf3 |
@@ -1,4 +1,4 @@
|
||||
import { Stream } from "stream";
|
||||
import { Stream } from "@brycethorup/stream";
|
||||
|
||||
export class Interactor {
|
||||
#actions = new Stream();
|
||||
@@ -7,9 +7,12 @@ export class Interactor {
|
||||
#latestReactions = {};
|
||||
|
||||
constructor(initialReaction = new Reaction()) {
|
||||
this.#actions.listen((action) =>
|
||||
this[`handle${action.constructor.name}`](action)
|
||||
);
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -45,8 +48,34 @@ export class Interactor {
|
||||
|
||||
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}`],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "interactor",
|
||||
"version": "1.0.6",
|
||||
"name": "@brycethorup/interactor",
|
||||
"version": "1.1.5",
|
||||
"description": "Cross-element communications and state management utility.",
|
||||
"author": "Bryce Thorup",
|
||||
"type": "module",
|
||||
"main": "interactor.js",
|
||||
"dependencies": {
|
||||
"stream": "^1.0.1"
|
||||
"@brycethorup/stream": "^1.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user