armor-ask v0.1.1
Armor Ask
armor-ask is a library in JavaScript. It implements a ask-answer mode to exchange data between different parts in your project.
ask-answer is similar to sub-pub, but asking an question is synchronous, so you can immediately get result returned when you ask a question. You can also wait for an answer if it doesn't be provided yet.
Installation
npm install armor-ask
Usage
import ArmorAsk from "armor-ask";
let foo = Object.assign({}, ArmorAsk);
let bar = Object.assign({}, ArmorAsk);
let question = "x + y = ?";
foo.answer(question, (params, question) => {
let { x, y } = params;
return `question: ${question}\nanswer: ${x} + ${y} = ${x + y}`;
});
let answer = bar.ask(question, { params: { x: 100, y: 100 } });
// output:
// question: x + y = ?
// answer: 100 + 100 = 200
console.log(answer);API
ask(question:string, options:object)
Ask a question and expect an answer returned.
If a question is asked and there is an answer that has been provided, then a solution that is provided by the answer should be returned, the type of solution could be any.
question:stringThe question to ask. The any one ofundefinedornullis equal to''.options:evaluate:boolIf the solution returned is a function, it will be evaluated by default unless this option is given false.params:anyIf the solution is a function and the option evaluate is true, the value of this option would be passed in solution as the first argument.wait:numberThe milliseconds to wait for an answer that has not been provided yet when the question is asked. Ifwaitis 0, it will immediately get a solution, which would be undefined if there isn't an answer. Otherwise, it will return a promise which would be resolved when the answer is provided, ifwaitis positive, the promise would be rejected until the time of waiting runs out; ifwaitis negative, the promise will be waiting to be resolved until an answer is provided.ns:stringThe namespace where it should look for an answer for this question. The option's default value is "default", the any one ofundefinedornullis equal todefault.
let question = "Who are you?";
foo.answer(question, "foo");
let result = foo.ask(question); // return "foo"
let callback = () => {};
foo
.ask("give me a function", { wait: 1000, evaluate: false })
.then(fn => console.log(fn === callback)); // true
foo.answer("give me a function", callback);answer(question:string, solution:any, options:object)
To provide an answer for a question and return a true when it successfully registered or a false when it failed to register the solution for a question.
question:stringThe question to answer. The any one ofundefinedornullis equal to''.solution:anyThe solution for a question, this will be returned when thequestionis asked. If it's a function, it takes three arguments:params,question,ns.optionsonce:boolIf given ture, the answer would immediately be closed when its corresponding question is asked. The default value is false.update:boolIf given true, the new answer would replece the current answer if it exists, otherwise it would failed to register the new answer. The default value is false.ns:stringThe namespace to register the question and answer.The any one ofundefinedornullis equal to 'default'.ctx:objectIf given, it would be the context of the solution if it's a function. The default context of solution is answerer itself.onShut:functionIf given, it would be called when the answer is closed. It takes two arguments that they arequestionandns.
foo.answer("question", { once: true, update: true, onShut: () => {} });shut(questions:string, options:object)
Close the answers.
question:stringThe question should be closed. If it's givenundefinedornull, the all of the questions could be the one to be closed.optionsnsThe namespace to look for the given question to close. The default value is 'default'. the one ofundefinedornullis equals to 'default'.allIf given true, the value ofnswould be ignored and it would look for question in all of namespaces.
foo.shut(); // close all the answers in default namespace.
foo.shut("some question", { ns: "new" }); // close the answer to 'some question' in 'new' namespace.
foo.shut(void 0, { all: true }); // close all the answers that were provided by foo.Ask Helper
Maybe sometimes you want to know if an question has been provided an answer, or how many answers have been provided, there are two methods belonging to ask api that can help you do it.
ask.has(question:string, options:object)
To find out if there is an answer for the question, return true if an answer is found, otherwise return false.
options:objectns:stringThe namespace to search for an answer, the default value isdefault.allIf given true, to search all namespaces for an answer.
foo.ask.has('who are you', {all: true});ask.list(options:object)
Return an plain object that contains all questions that has been answered.
options:objectns:stringThe namespace to list all questions, the default value isdefault.allIf given true, list all questions in all of namespaces.
foo.ask.list({all: true});