Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 466e95cb14 | |||
| 7ef7c44cf7 | |||
| bf31d5371e | |||
| fc4e96ab37 | |||
| 7af635a19b | |||
| 665dd28f93 | |||
| e026dcdb15 | |||
| 2102e9c8cf | |||
| 020299f82a | |||
| bac3b409d2 | |||
| 442f6055f6 | |||
| bd96c56445 | |||
| 9978667571 | |||
| 66173ac4f7 | |||
| 85fbdce1d5 | |||
| 4ad844468f | |||
| 2db58bec9e |
@@ -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 }}
|
||||
|
||||
2
index.js
2
index.js
@@ -1,2 +0,0 @@
|
||||
import { Interactor, Action, Reaction } from "./src/interactor.js";
|
||||
export { Interactor, Action, Reaction };
|
||||
52
interactor.js
Normal file
52
interactor.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { Stream } from 'stream';
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
20
package-lock.json
generated
Normal file
20
package-lock.json
generated
Normal 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=="
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
{
|
||||
"name": "interactor",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.6",
|
||||
"description": "Cross-element communications and state management utility.",
|
||||
"author": "Bryce Thorup",
|
||||
"type": "module",
|
||||
"main": "index.js"
|
||||
"main": "interactor.js",
|
||||
"dependencies": {
|
||||
"stream": "^1.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user