revojs 0.2.38 → 0.2.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { mimeType, mimeTypes } from "./assets/index.js";
2
- import { STATE_CONTEXT, VARIABLE_CONTEXT, createRegisterHooks, createScope, defineContext, defineHook, definePlugin, dispatchHook, dispatchHookSingle, getContext, registerHook, setContext, useVariableContext, useVariables } from "./scope/index.js";
2
+ import { STATE_CONTEXT, VARIABLE_CONTEXT, createScope, defineContext, defineHook, definePlugin, dispatchHook, dispatchHookSingle, getContext, registerHook, registerHooks, setContext, useVariableContext, useVariables } from "./scope/index.js";
3
3
  import { GROUP_MATCH, HOOK_MATCH, OPTIONAL_PARAMETER_MATCH, OPTIONAL_WILDCARD_MATCH, PARAMETER_MATCH, ROUTE_CONTEXT, ROUTE_RESOLVER_CONTEXT, Router, WILDCARD_MATCH, fillSegments, toSegments, useRoute, useRouteResolverContext } from "./router/index.js";
4
- export { GROUP_MATCH, HOOK_MATCH, OPTIONAL_PARAMETER_MATCH, OPTIONAL_WILDCARD_MATCH, PARAMETER_MATCH, ROUTE_CONTEXT, ROUTE_RESOLVER_CONTEXT, Router, STATE_CONTEXT, VARIABLE_CONTEXT, WILDCARD_MATCH, createRegisterHooks, createScope, defineContext, defineHook, definePlugin, dispatchHook, dispatchHookSingle, fillSegments, getContext, mimeType, mimeTypes, registerHook, setContext, toSegments, useRoute, useRouteResolverContext, useVariableContext, useVariables };
4
+ export { GROUP_MATCH, HOOK_MATCH, OPTIONAL_PARAMETER_MATCH, OPTIONAL_WILDCARD_MATCH, PARAMETER_MATCH, ROUTE_CONTEXT, ROUTE_RESOLVER_CONTEXT, Router, STATE_CONTEXT, VARIABLE_CONTEXT, WILDCARD_MATCH, createScope, defineContext, defineHook, definePlugin, dispatchHook, dispatchHookSingle, fillSegments, getContext, mimeType, mimeTypes, registerHook, registerHooks, setContext, toSegments, useRoute, useRouteResolverContext, useVariableContext, useVariables };
@@ -159,6 +159,12 @@ async function createConfig(inputConfig) {
159
159
  format: "object",
160
160
  consumers: ["client", "server"]
161
161
  },
162
+ hooks: {
163
+ from: ["./hooks"],
164
+ match: ["**/*.{js,ts}", "!**/*.d.ts"],
165
+ format: "object",
166
+ consumers: ["client", "server"]
167
+ },
162
168
  commands: {
163
169
  from: ["./commands"],
164
170
  match: ["**/*.{js,ts}", "!**/*.d.ts"],
package/dist/package.js CHANGED
@@ -1,6 +1,6 @@
1
1
  //#region package.json
2
2
  var name = "revojs";
3
- var version = "0.2.38";
3
+ var version = "0.2.39";
4
4
  var repository = {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/tellua/revojs.git"
@@ -3,10 +3,7 @@ export type Descriptor<T> = string & {
3
3
  [descriptor]: T;
4
4
  };
5
5
  export type InferDescriptor<T> = T extends Descriptor<infer U> ? U : never;
6
- export type Hook = (...inputs: Array<any>) => any;
7
- export type Hooks<T extends Record<string, Descriptor<Hook>>> = {
8
- [Key in keyof T]: InferDescriptor<T[Key]>;
9
- };
6
+ export type Hook<TContent = any, TResult = any> = (content: TContent) => TResult;
10
7
  export type Intersection<T> = (T extends any ? (arg: T) => void : never) extends (arg: infer I) => void ? I : never;
11
8
  export type Resolvable<T> = (scope: Scope) => T;
12
9
  export type Source<T = unknown> = Record<string, Record<string, () => Promise<T>>>;
@@ -20,19 +17,24 @@ export type SourceTypeProperty<TSource extends Source, TScope extends SourceScop
20
17
  export interface Plugin {
21
18
  setup: (scope: Scope) => Awaitable<void>;
22
19
  }
20
+ export type Hooks<T extends Record<string, Descriptor<Hook>>> = {
21
+ [Key in keyof T]: {
22
+ type: Key;
23
+ content: Parameters<InferDescriptor<T[Key]>>[0];
24
+ };
25
+ }[keyof T];
23
26
  export interface Scope {
24
27
  hooks: Record<string, Array<Hook>>;
25
28
  context: Record<string, unknown>;
26
29
  }
27
30
  export declare function registerHook<T extends Hook>(scope: Scope, name: Descriptor<T>, invoke: T): () => void;
28
- export declare function createRegisterHooks<T extends Record<string, Descriptor<Hook>>>(descriptors: T): (scope: Scope, hooks: Partial<Hooks<T>>) => void;
29
- export declare function dispatchHook<T extends Hook>(scope: Scope, name: Descriptor<T>, ...inputs: Parameters<T>): Promise<Awaited<ReturnType<T>>[]>;
30
- export declare function dispatchHookSingle<T extends Hook>(scope: Scope, name: Descriptor<T>, ...inputs: Parameters<T>): Promise<ReturnType<T> | undefined>;
31
+ export declare function dispatchHook<T extends Hook>(scope: Scope, name: Descriptor<T>, ...[content]: Parameters<T>): Promise<Awaited<ReturnType<T>>[]>;
32
+ export declare function dispatchHookSingle<T extends Hook>(scope: Scope, name: Descriptor<T>, ...content: Parameters<T>): Promise<ReturnType<T> | undefined>;
31
33
  export declare function getContext<T>(scope: Scope, input: Descriptor<T>): T;
32
34
  export declare function setContext<T>(scope: Scope, input: Descriptor<T>, value: T): T;
33
35
  export declare function createScope(inputScope?: Mergeable<Scope>): Promise<{
34
36
  hooks?: {
35
- [x: string]: ArrayLike<Hook> | undefined;
37
+ [x: string]: ArrayLike<Hook<any, any>> | undefined;
36
38
  } | undefined;
37
39
  context?: {
38
40
  [x: string]: {} | undefined;
@@ -41,7 +43,11 @@ export declare function createScope(inputScope?: Mergeable<Scope>): Promise<{
41
43
  export declare function useVariableContext(scope: Scope): unknown;
42
44
  export declare function useVariables<T = typeof import("#variables")>(scope: Scope): T;
43
45
  export declare function definePlugin<T extends Plugin>(plugin: T): T;
44
- export declare function defineHook<T extends Hook>(name: string): Descriptor<T>;
46
+ export declare function defineHook<TContent = void, TResult = void>(name: string): Descriptor<Hook<TContent, TResult>>;
47
+ export declare function registerHooks<T extends Record<string, Descriptor<Hook>>>(hooks: T, invoke: (scope: Scope, event: Hooks<T>) => ReturnType<InferDescriptor<T[keyof T]>>): {
48
+ hooks: T;
49
+ invoke: (scope: Scope, event: Hooks<T>) => ReturnType<InferDescriptor<T[keyof T]>>;
50
+ };
45
51
  export declare function defineContext<T>(name: string): Descriptor<T>;
46
52
  export declare const VARIABLE_CONTEXT: Descriptor<unknown>;
47
53
  export declare const STATE_CONTEXT: Descriptor<Record<string, unknown>>;
@@ -1,4 +1,5 @@
1
1
  import { mergeObjects } from "../shared/index.js";
2
+ import hooks from "#hooks";
2
3
  import plugins from "#plugins";
3
4
  import variables from "#variables";
4
5
  //#region src/scope/index.ts
@@ -12,21 +13,13 @@ function registerHook(scope, name, invoke) {
12
13
  if (scope.hooks[name]?.length === 0) delete scope.hooks[name];
13
14
  };
14
15
  }
15
- function createRegisterHooks(descriptors) {
16
- return (scope, hooks) => {
17
- for (const name in descriptors) {
18
- const hook = hooks[name];
19
- if (hook) registerHook(scope, descriptors[name], hook);
20
- }
21
- };
22
- }
23
- async function dispatchHook(scope, name, ...inputs) {
16
+ async function dispatchHook(scope, name, ...[content]) {
24
17
  const results = new Array();
25
- for (const invoke of scope.hooks[name] ?? []) results.push(await invoke(...inputs));
18
+ for (const invoke of scope.hooks[name] ?? []) results.push(await invoke(content));
26
19
  return results;
27
20
  }
28
- async function dispatchHookSingle(scope, name, ...inputs) {
29
- return await dispatchHook(scope, name, ...inputs).then((results) => results.at(0));
21
+ async function dispatchHookSingle(scope, name, ...content) {
22
+ return await dispatchHook(scope, name, ...content).then((results) => results.at(0));
30
23
  }
31
24
  function getContext(scope, input) {
32
25
  if (Object.hasOwn(scope.context, input)) return scope.context[input];
@@ -44,6 +37,12 @@ async function createScope(inputScope) {
44
37
  const pending = new Array();
45
38
  for (const source of Object.values(plugins)) for (const resolve of Object.values(source)) pending.push(resolve());
46
39
  for (const plugin of await Promise.all(pending)) if (plugin) await plugin.setup(scope);
40
+ const handlers = new Array();
41
+ for (const source of Object.values(hooks)) for (const resolve of Object.values(source)) handlers.push(resolve());
42
+ for (const handler of await Promise.all(handlers)) if (handler) for (const [type, name] of Object.entries(handler.hooks)) registerHook(scope, name, (content) => handler.invoke(scope, {
43
+ type,
44
+ content
45
+ }));
47
46
  return scope;
48
47
  }
49
48
  function useVariableContext(scope) {
@@ -71,10 +70,16 @@ function definePlugin(plugin) {
71
70
  function defineHook(name) {
72
71
  return name;
73
72
  }
73
+ function registerHooks(hooks, invoke) {
74
+ return {
75
+ hooks,
76
+ invoke
77
+ };
78
+ }
74
79
  function defineContext(name) {
75
80
  return name;
76
81
  }
77
82
  var VARIABLE_CONTEXT = defineContext("VARIABLE_CONTEXT");
78
83
  var STATE_CONTEXT = defineContext("STATE_CONTEXT");
79
84
  //#endregion
80
- export { STATE_CONTEXT, VARIABLE_CONTEXT, createRegisterHooks, createScope, defineContext, defineHook, definePlugin, dispatchHook, dispatchHookSingle, getContext, registerHook, setContext, useVariableContext, useVariables };
85
+ export { STATE_CONTEXT, VARIABLE_CONTEXT, createScope, defineContext, defineHook, definePlugin, dispatchHook, dispatchHookSingle, getContext, registerHook, registerHooks, setContext, useVariableContext, useVariables };
@@ -12,6 +12,13 @@ declare module "#plugins" {
12
12
  export default entries;
13
13
  }
14
14
 
15
+ declare module "#hooks" {
16
+ import type { Descriptor, Hook, registerHooks, Source } from "revojs";
17
+
18
+ const entries: Source<ReturnType<typeof registerHooks<Record<string, Descriptor<Hook>>>>>;
19
+ export default entries;
20
+ }
21
+
15
22
  declare module "#commands" {
16
23
  import type { Source } from "revojs";
17
24
  import type { Command } from "revojs/module";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.2.38",
3
+ "version": "0.2.39",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,6 +12,13 @@ declare module "#plugins" {
12
12
  export default entries;
13
13
  }
14
14
 
15
+ declare module "#hooks" {
16
+ import type { Descriptor, Hook, registerHooks, Source } from "revojs";
17
+
18
+ const entries: Source<ReturnType<typeof registerHooks<Record<string, Descriptor<Hook>>>>>;
19
+ export default entries;
20
+ }
21
+
15
22
  declare module "#commands" {
16
23
  import type { Source } from "revojs";
17
24
  import type { Command } from "revojs/module";