@actus/core 中文文档教程
@actus/core
这是 Actus 命令栏的机器和内核。
它由一个有限状态机、一个输入解析器和一个结果排序算法(自学习)组成。
Usage
npm install @actus/core
有关更多详细信息,请参阅 packages/
中的 Svelte 示例。
import { interpret, filterAndSort, selectionMachine } from "@actus/core";
const selectionService = interpret(selectionMachine);
const commands = [
{
id: "1",
title: "My command",
description: "My description",
exec: () => {
console.log("Executed!");
},
},
];
selectionService.send("NEW_COMMANDS", commands);
// Bind it to the UI of choice and send events
// when the user inputs something or clicks on a result
selectionService.send("INPUT", "m");
selectionService.send("EXEC", "1");
The Finite State Machine
查看 src/selection-machine.ts 以查看机器及其服务/操作/守卫实现。
这是机器的可视化:
Commands
命令的类型定义:
type Command = {
id: string;
title: CommandTitle;
description: CommandDescription;
exec: ExecutionFn;
getMatchString?: GenerateMatchStringFn;
requiredArgs?: string[];
};
type CommandTitle = string | CommandTitleFn;
type CommandTitleFn = (input: ParserResult) => string;
type CommandDescription = string | CommandDescriptionFn;
type CommandDescriptionFn = (input: ParserResult) => string;
type ExecutionFn = (command: Command, input: ParserResult) => void;
type GenerateMatchStringFn = (input: ParserResult) => string;
type ParserResult = [string] | [string, ParserParams] | null;
type ParserParams = {
[key: string]: string;
};
The parser
解析器是使用 Nearley 构建的。
查看 src/grammar/parse-input.ne 了解语法。
以下是解析输出的内容:
type ParserResult = [string] | [string, ParserParams] | null;
type ParserParams = {
[key: string]: string;
};
示例:
hello -> ["hello"]
hello -p -> ["hello", {p: null}]
hello -p 1 -> ["hello", {p: "1"}]
hello -p 1 -r "hello x" -> ["hello", {p: "1", r: "hello x"}]
hello -p " -> null (broken string)
Self learning
从某种意义上说,它是自学的,您为特定输入选择的项目越多,它对项目的排名就越高。 跟随趋势并有新的命令 有机会相当快地到达顶部,它不会永远保留执行历史记录,但会不时对其进行规范化。 请参阅 src/exec-graph.ts 以了解其实现。
@actus/core
This is the machine and inner core of the Actus command bar.
It consists of a Finite State Machine, an input parser, and a result ranking algorithm (self learning).
Usage
npm install @actus/core
See the Svelte example in packages/
for more detailed info.
import { interpret, filterAndSort, selectionMachine } from "@actus/core";
const selectionService = interpret(selectionMachine);
const commands = [
{
id: "1",
title: "My command",
description: "My description",
exec: () => {
console.log("Executed!");
},
},
];
selectionService.send("NEW_COMMANDS", commands);
// Bind it to the UI of choice and send events
// when the user inputs something or clicks on a result
selectionService.send("INPUT", "m");
selectionService.send("EXEC", "1");
The Finite State Machine
Have a look at src/selection-machine.ts to see the machine and its services / actions / guards implementations.
Here's a visaulization of the machine:
Commands
Type definition of a command:
type Command = {
id: string;
title: CommandTitle;
description: CommandDescription;
exec: ExecutionFn;
getMatchString?: GenerateMatchStringFn;
requiredArgs?: string[];
};
type CommandTitle = string | CommandTitleFn;
type CommandTitleFn = (input: ParserResult) => string;
type CommandDescription = string | CommandDescriptionFn;
type CommandDescriptionFn = (input: ParserResult) => string;
type ExecutionFn = (command: Command, input: ParserResult) => void;
type GenerateMatchStringFn = (input: ParserResult) => string;
type ParserResult = [string] | [string, ParserParams] | null;
type ParserParams = {
[key: string]: string;
};
The parser
The parser is built using Nearley.
Check out src/grammar/parse-input.ne for the grammar.
Here's what the parse outputs:
type ParserResult = [string] | [string, ParserParams] | null;
type ParserParams = {
[key: string]: string;
};
Examples:
hello -> ["hello"]
hello -p -> ["hello", {p: null}]
hello -p 1 -> ["hello", {p: "1"}]
hello -p 1 -r "hello x" -> ["hello", {p: "1", r: "hello x"}]
hello -p " -> null (broken string)
Self learning
It's self learning in the sense that it ranks items higher the more you pick them for a certain input. To follow trends and have new commands have achance to get to the top fairly quick, it doesn't keep the execution history forever but normalizes it from time to time. See src/exec-graph.ts for the implementation of this.