Commit 64744c23 authored by nanahira's avatar nanahira

reimplement selection and add @Isolate

parent 7b78f3b3
......@@ -3,4 +3,5 @@ export * from './src/register';
export * from './src/decorators';
export * from './src/http-decorators';
export * from './src/def/interfaces';
export * from './src/def/select';
export * from './src/utility';
......@@ -37,7 +37,7 @@
"typescript": "^4.6.4"
},
"peerDependencies": {
"koishi": "^4.7.4"
"koishi": "^4.7.5"
}
},
"node_modules/@babel/code-frame": {
......@@ -1000,15 +1000,15 @@
}
},
"node_modules/@koishijs/core": {
"version": "4.7.4",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.4.tgz",
"integrity": "sha512-S4pPaO05WC0htkQVcTMTluBZv4g8sEEBJ2ItHN2zJA8syet3iKJidT+s2gYt8d2rBo0Ndlvo1gBLGLNl/Zc9yg==",
"version": "4.7.5",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.5.tgz",
"integrity": "sha512-So8tBtSTVIq8dTGs/0o92Om3bQhgYUhmzVhnYTqJbx+kW3kSowl/ORiBhJFK3GR6Dj+JyKLptsqQNE4DjZdNhw==",
"peer": true,
"dependencies": {
"@koishijs/utils": "^5.4.5",
"cordis": "^1.3.3",
"cordis": "^1.5.5",
"fastest-levenshtein": "^1.0.12",
"minato": "^1.1.0"
"minato": "^1.2.1"
},
"engines": {
"node": ">=12.0.0"
......@@ -2280,9 +2280,9 @@
"peer": true
},
"node_modules/cordis": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.3.3.tgz",
"integrity": "sha512-mpyBWAEVYX8/YHoGWIqaOjrOk6DmtrOWZfsBSinNR+nDBlB39Ks6aC1B5oZ7AdnVoRE2ZYBCGecoHrlxcRxd6w==",
"version": "1.5.5",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.5.5.tgz",
"integrity": "sha512-vCw6FA+OLO5e40so5XjhRnIw1ldGH1L74q/wlnuckqFtIq8teYkfnnmlNTS24RJNcgPz2DWdbVgCnoc+5Lc+UQ==",
"peer": true,
"dependencies": {
"cosmokit": "^1.1.2"
......@@ -4764,13 +4764,13 @@
}
},
"node_modules/koishi": {
"version": "4.7.4",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.4.tgz",
"integrity": "sha512-VVHf3TyFsTuKrB8IZffxwmwiGx+zyhDo+DNNEFQjvomgqFdGEceAnLxNIf10K48aB1CDmkmi37b6uaURi4Z1Hw==",
"version": "4.7.5",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.5.tgz",
"integrity": "sha512-Q+czD5hZIsikYwdoKUOZFZIuvXoamikXK6iX4FXnHUPsoXX16PjqX3D1sBKd9JLVMA49KSAGoFW0fN9IMHykYw==",
"peer": true,
"dependencies": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.7.4",
"@koishijs/core": "^4.7.5",
"@koishijs/utils": "^5.4.5",
"@types/koa": "*",
"@types/koa__router": "*",
......@@ -4779,7 +4779,7 @@
"file-type": "^16.5.3",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"ns-require": "^1.1.2",
"ns-require": "^1.1.4",
"parseurl": "^1.3.3",
"path-to-regexp": "^6.2.1",
"portfinder": "^1.0.28",
......@@ -5005,13 +5005,13 @@
}
},
"node_modules/minato": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/minato/-/minato-1.1.0.tgz",
"integrity": "sha512-by1KMWxWlzq0iDV7tMkqS3thDRzYNQoS2ECfX5HRpMGaPQJqSgm5aY9IAky7knwAMoW3cMxQ/IhpvEShQqyj6A==",
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/minato/-/minato-1.2.1.tgz",
"integrity": "sha512-BYY18rr82aXAAVtMKkN4yDFsTUtsAmeDmyTRVOXG3mEzG74sNF5TU1nVT1QSC9dpvAcCFFs/vZO2rJoC8vSetQ==",
"peer": true,
"dependencies": {
"cosmokit": "^1.1.2",
"ns-require": "^1.1.2"
"ns-require": "^1.1.4"
}
},
"node_modules/minimatch": {
......@@ -5114,9 +5114,9 @@
}
},
"node_modules/ns-require": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.3.tgz",
"integrity": "sha512-PFQNIXhJS4ISAcuzkVRq+D9nZDfIYyCRR/9rJQAcL5iRgHfQVc8SsjjGnV43FhttA9F7w+Khk+OGxs8OCMrtWQ==",
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.4.tgz",
"integrity": "sha512-Zk25pQj4u5i6DS0vaNO5aSSXewybVqqVVjz8AOxFy9DNPtmu3jlexMz6kUXLV2oB+X6iQeAnHXSzj5Qz/IeDaQ==",
"peer": true
},
"node_modules/nwsapi": {
......@@ -7662,15 +7662,15 @@
}
},
"@koishijs/core": {
"version": "4.7.4",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.4.tgz",
"integrity": "sha512-S4pPaO05WC0htkQVcTMTluBZv4g8sEEBJ2ItHN2zJA8syet3iKJidT+s2gYt8d2rBo0Ndlvo1gBLGLNl/Zc9yg==",
"version": "4.7.5",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.5.tgz",
"integrity": "sha512-So8tBtSTVIq8dTGs/0o92Om3bQhgYUhmzVhnYTqJbx+kW3kSowl/ORiBhJFK3GR6Dj+JyKLptsqQNE4DjZdNhw==",
"peer": true,
"requires": {
"@koishijs/utils": "^5.4.5",
"cordis": "^1.3.3",
"cordis": "^1.5.5",
"fastest-levenshtein": "^1.0.12",
"minato": "^1.1.0"
"minato": "^1.2.1"
}
},
"@koishijs/plugin-adapter-onebot": {
......@@ -8684,9 +8684,9 @@
"peer": true
},
"cordis": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.3.3.tgz",
"integrity": "sha512-mpyBWAEVYX8/YHoGWIqaOjrOk6DmtrOWZfsBSinNR+nDBlB39Ks6aC1B5oZ7AdnVoRE2ZYBCGecoHrlxcRxd6w==",
"version": "1.5.5",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.5.5.tgz",
"integrity": "sha512-vCw6FA+OLO5e40so5XjhRnIw1ldGH1L74q/wlnuckqFtIq8teYkfnnmlNTS24RJNcgPz2DWdbVgCnoc+5Lc+UQ==",
"peer": true,
"requires": {
"cosmokit": "^1.1.2"
......@@ -10561,13 +10561,13 @@
}
},
"koishi": {
"version": "4.7.4",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.4.tgz",
"integrity": "sha512-VVHf3TyFsTuKrB8IZffxwmwiGx+zyhDo+DNNEFQjvomgqFdGEceAnLxNIf10K48aB1CDmkmi37b6uaURi4Z1Hw==",
"version": "4.7.5",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.5.tgz",
"integrity": "sha512-Q+czD5hZIsikYwdoKUOZFZIuvXoamikXK6iX4FXnHUPsoXX16PjqX3D1sBKd9JLVMA49KSAGoFW0fN9IMHykYw==",
"peer": true,
"requires": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.7.4",
"@koishijs/core": "^4.7.5",
"@koishijs/utils": "^5.4.5",
"@types/koa": "*",
"@types/koa__router": "*",
......@@ -10576,7 +10576,7 @@
"file-type": "^16.5.3",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"ns-require": "^1.1.2",
"ns-require": "^1.1.4",
"parseurl": "^1.3.3",
"path-to-regexp": "^6.2.1",
"portfinder": "^1.0.28",
......@@ -10741,13 +10741,13 @@
"dev": true
},
"minato": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/minato/-/minato-1.1.0.tgz",
"integrity": "sha512-by1KMWxWlzq0iDV7tMkqS3thDRzYNQoS2ECfX5HRpMGaPQJqSgm5aY9IAky7knwAMoW3cMxQ/IhpvEShQqyj6A==",
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/minato/-/minato-1.2.1.tgz",
"integrity": "sha512-BYY18rr82aXAAVtMKkN4yDFsTUtsAmeDmyTRVOXG3mEzG74sNF5TU1nVT1QSC9dpvAcCFFs/vZO2rJoC8vSetQ==",
"peer": true,
"requires": {
"cosmokit": "^1.1.2",
"ns-require": "^1.1.2"
"ns-require": "^1.1.4"
}
},
"minimatch": {
......@@ -10829,9 +10829,9 @@
}
},
"ns-require": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.3.tgz",
"integrity": "sha512-PFQNIXhJS4ISAcuzkVRq+D9nZDfIYyCRR/9rJQAcL5iRgHfQVc8SsjjGnV43FhttA9F7w+Khk+OGxs8OCMrtWQ==",
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/ns-require/-/ns-require-1.1.4.tgz",
"integrity": "sha512-Zk25pQj4u5i6DS0vaNO5aSSXewybVqqVVjz8AOxFy9DNPtmu3jlexMz6kUXLV2oB+X6iQeAnHXSzj5Qz/IeDaQ==",
"peer": true
},
"nwsapi": {
......
......@@ -67,6 +67,6 @@
"typed-reflector": "^1.0.10"
},
"peerDependencies": {
"koishi": "^4.7.4"
"koishi": "^4.7.5"
}
}
......@@ -24,7 +24,6 @@ import {
Events,
FieldCollector,
I18n,
Selection,
Session,
} from 'koishi';
import { Metadata } from '../meta/metadata.decorators';
......@@ -34,7 +33,7 @@ import {
applyOptionToCommand,
registerTemplate,
} from '../utility';
import { Observable } from 'rxjs';
import { selectContext, Selection } from '../def/select';
// Register method
......@@ -123,7 +122,10 @@ export const OnPrivate = (...values: string[]) =>
OnContext((ctx) => ctx.private(...values));
export const OnSelection = (selection: Selection) =>
OnContext((ctx) => ctx.select(selection));
OnContext((ctx) => selectContext(ctx, selection));
export const Isolate = (...values: string[]) =>
OnContext((ctx) => ctx.isolate(values));
// Command definition
......
export * from './interfaces';
export * from './constants';
export * from './select';
......@@ -7,12 +7,12 @@ import {
Events,
I18n,
Plugin,
Selection,
} from 'koishi';
import type { DefaultContext, DefaultState, ParameterizedContext } from 'koa';
import type { RouterParamContext } from '@koa/router';
import { CommandPut } from '../registry';
import { Observable } from 'rxjs';
import { Selection } from './select';
export interface Type<T = any> extends Function {
new (...args: any[]): T;
......
import { Context, makeArray, MaybeArray } from 'koishi';
const selectors = [
'user',
'guild',
'channel',
'self',
'private',
'platform',
] as const;
export type SelectorType = typeof selectors[number];
export type SelectorValue = boolean | MaybeArray<string | number>;
export type BaseSelection = { [K in SelectorType]?: SelectorValue };
export interface Selection extends BaseSelection {
and?: Selection[];
or?: Selection[];
not?: Selection;
}
export function selectContext(root: Context, options: Selection) {
let ctx = root;
// basic selectors
for (const type of selectors) {
const value = options[type];
if (value === true) {
ctx = ctx[type]();
} else if (value === false) {
ctx = ctx.exclude(ctx[type]());
} else if (value !== undefined) {
// we turn everything into string
ctx = ctx[type](...makeArray(value).map((item) => '' + item));
}
}
// intersect
if (options.and) {
for (const selection of options.and) {
ctx = ctx.intersect(selectContext(root, selection));
}
}
// union
if (options.or) {
let ctx2: Context = ctx.never();
for (const selection of options.or) {
ctx2 = ctx2.union(selectContext(root, selection));
}
ctx = ctx.intersect(ctx2);
}
// exclude
if (options.not) {
ctx = ctx.exclude(selectContext(root, options.not));
}
return ctx;
}
export * from './utility';
export * from './native-type-mapping';
export * from '../def/select';
......@@ -4,6 +4,7 @@ import {
ContextSelector,
OnContextFunction,
ParamRenderer,
selectContext,
TemplateConfig,
} from '../def';
import { applyNativeTypeToArg } from './native-type-mapping';
......@@ -19,7 +20,7 @@ export function applySelector(
}
let targetCtx = ctx;
if (selector.select) {
targetCtx = targetCtx.select(selector.select);
targetCtx = selectContext(ctx, selector.select);
}
if (selector.useSelector) {
targetCtx = selector.useSelector(targetCtx) || targetCtx;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment