@hybridly/core 0.0.1-alpha.16 → 0.0.1-alpha.18

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.cjs CHANGED
@@ -2,14 +2,14 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- const qs = require('qs');
6
5
  const utils = require('@hybridly/utils');
7
6
  const axios = require('axios');
7
+ const qs = require('qs');
8
8
 
9
9
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
10
10
 
11
- const qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
12
11
  const axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
12
+ const qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
13
13
 
14
14
  const STORAGE_EXTERNAL_KEY = "hybridly:external";
15
15
  const HYBRIDLY_HEADER = "x-hybrid";
@@ -89,7 +89,12 @@ async function runHooks(hook, requestHooks, ...args) {
89
89
  function appendCallbackToHooks(hook, fn) {
90
90
  const hooks = getRouterContext().hooks;
91
91
  hooks[hook] = [...hooks[hook] ?? [], fn];
92
- return () => hooks[hook]?.splice(hooks[hook].indexOf(fn), 1);
92
+ return () => {
93
+ const index = hooks[hook].indexOf(fn);
94
+ if (index !== -1) {
95
+ hooks[hook]?.splice(index, 1);
96
+ }
97
+ };
93
98
  }
94
99
  function registerHook(hook, fn, options) {
95
100
  if (options?.once) {
@@ -469,10 +474,7 @@ async function initializeContext(options) {
469
474
  hooks: {},
470
475
  state: {}
471
476
  };
472
- for (const plugin of state.context.plugins) {
473
- utils.debug.plugin(plugin.name, 'Calling "initialized" hook.');
474
- await plugin.initialized?.(state.context);
475
- }
477
+ await runHooks("initialized", {}, state.context);
476
478
  return getInternalRouterContext();
477
479
  }
478
480
  function setContext(merge = {}, options = {}) {
@@ -733,6 +735,7 @@ function isHybridResponse(response) {
733
735
  async function navigate(options) {
734
736
  const context = getRouterContext();
735
737
  utils.debug.router("Making an internal navigation:", { context, options });
738
+ await runHooks("navigating", {}, options, context);
736
739
  options.payload ?? (options.payload = payloadFromContext());
737
740
  const evaluateConditionalOption = (option) => typeof option === "function" ? option(options.payload) : option;
738
741
  const shouldPreserveState = evaluateConditionalOption(options.preserveState);
@@ -795,6 +798,7 @@ async function initializeRouter() {
795
798
  });
796
799
  }
797
800
  registerEventListeners();
801
+ await runHooks("ready", {}, context);
798
802
  return context;
799
803
  }
800
804
  async function performLocalNavigation(targetUrl, options) {
package/dist/index.d.ts CHANGED
@@ -3,14 +3,10 @@ import { AxiosResponse, AxiosProgressEvent, Axios } from 'axios';
3
3
 
4
4
  type MaybePromise<T> = T | Promise<T>;
5
5
 
6
- interface Hooks {
6
+ interface RequestHooks {
7
7
  /**
8
- * Called when a back-forward navigation occurs.
9
- */
10
- backForward: (state: any, context: InternalRouterContext) => MaybePromise<any>;
11
- /**
12
- * Called before anything when a navigation is going to happen.
13
- */
8
+ * Called before a navigation request is going to happen.
9
+ */
14
10
  before: (options: HybridRequestOptions, context: InternalRouterContext) => MaybePromise<any | boolean>;
15
11
  /**
16
12
  * Called before the request of a navigation is going to happen.
@@ -52,8 +48,26 @@ interface Hooks {
52
48
  * Called after a request has been made, even if it didn't succeed.
53
49
  */
54
50
  after: (context: InternalRouterContext) => MaybePromise<void>;
51
+ }
52
+ interface Hooks extends RequestHooks {
53
+ /**
54
+ * Called when Hybridly's context is initialized.
55
+ */
56
+ initialized: (context: InternalRouterContext) => MaybePromise<void>;
57
+ /**
58
+ * Called after Hybridly's initial page load.
59
+ */
60
+ ready: (context: InternalRouterContext) => MaybePromise<void>;
61
+ /**
62
+ * Called when a back-forward navigation occurs.
63
+ */
64
+ backForward: (state: any, context: InternalRouterContext) => MaybePromise<any>;
65
+ /**
66
+ * Called when a component navigation is being made.
67
+ */
68
+ navigating: (options: NavigationOptions, context: InternalRouterContext) => MaybePromise<void>;
55
69
  /**
56
- * Called when a navigation has been made and a page component has been navigated to.
70
+ * Called when a component has been navigated to.
57
71
  */
58
72
  navigated: (options: NavigationOptions, context: InternalRouterContext) => MaybePromise<void>;
59
73
  }
@@ -67,8 +81,8 @@ interface HookOptions {
67
81
  declare function registerHook<T extends keyof Hooks>(hook: T, fn: Hooks[T], options?: HookOptions): () => void;
68
82
 
69
83
  interface Plugin extends Partial<Hooks> {
84
+ /** Identifier of the plugin. */
70
85
  name: string;
71
- initialized?: (context: InternalRouterContext) => MaybePromise<void>;
72
86
  }
73
87
  declare function definePlugin(plugin: Plugin): Plugin;
74
88
 
@@ -153,7 +167,7 @@ interface HybridRequestOptions extends Omit<NavigationOptions, 'payload'> {
153
167
  /** The bag in which to put potential errors. */
154
168
  errorBag?: string;
155
169
  /** Hooks for this navigation. */
156
- hooks?: Partial<Hooks>;
170
+ hooks?: Partial<RequestHooks>;
157
171
  /** If `true`, force the usage of a `FormData` object. */
158
172
  useFormData?: boolean;
159
173
  /**
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import qs, { parse, stringify } from 'qs';
2
1
  import { debug, merge, debounce, random, hasFiles, objectToFormData, when, match, showResponseErrorModal } from '@hybridly/utils';
3
2
  import axios from 'axios';
3
+ import qs, { parse, stringify } from 'qs';
4
4
 
5
5
  const STORAGE_EXTERNAL_KEY = "hybridly:external";
6
6
  const HYBRIDLY_HEADER = "x-hybrid";
@@ -80,7 +80,12 @@ async function runHooks(hook, requestHooks, ...args) {
80
80
  function appendCallbackToHooks(hook, fn) {
81
81
  const hooks = getRouterContext().hooks;
82
82
  hooks[hook] = [...hooks[hook] ?? [], fn];
83
- return () => hooks[hook]?.splice(hooks[hook].indexOf(fn), 1);
83
+ return () => {
84
+ const index = hooks[hook].indexOf(fn);
85
+ if (index !== -1) {
86
+ hooks[hook]?.splice(index, 1);
87
+ }
88
+ };
84
89
  }
85
90
  function registerHook(hook, fn, options) {
86
91
  if (options?.once) {
@@ -460,10 +465,7 @@ async function initializeContext(options) {
460
465
  hooks: {},
461
466
  state: {}
462
467
  };
463
- for (const plugin of state.context.plugins) {
464
- debug.plugin(plugin.name, 'Calling "initialized" hook.');
465
- await plugin.initialized?.(state.context);
466
- }
468
+ await runHooks("initialized", {}, state.context);
467
469
  return getInternalRouterContext();
468
470
  }
469
471
  function setContext(merge = {}, options = {}) {
@@ -724,6 +726,7 @@ function isHybridResponse(response) {
724
726
  async function navigate(options) {
725
727
  const context = getRouterContext();
726
728
  debug.router("Making an internal navigation:", { context, options });
729
+ await runHooks("navigating", {}, options, context);
727
730
  options.payload ?? (options.payload = payloadFromContext());
728
731
  const evaluateConditionalOption = (option) => typeof option === "function" ? option(options.payload) : option;
729
732
  const shouldPreserveState = evaluateConditionalOption(options.preserveState);
@@ -786,6 +789,7 @@ async function initializeRouter() {
786
789
  });
787
790
  }
788
791
  registerEventListeners();
792
+ await runHooks("ready", {}, context);
789
793
  return context;
790
794
  }
791
795
  async function performLocalNavigation(targetUrl, options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hybridly/core",
3
- "version": "0.0.1-alpha.16",
3
+ "version": "0.0.1-alpha.18",
4
4
  "description": "A solution to develop server-driven, client-rendered applications",
5
5
  "keywords": [
6
6
  "hybridly",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "qs": "^6.11.0",
40
- "@hybridly/utils": "0.0.1-alpha.16"
40
+ "@hybridly/utils": "0.0.1-alpha.18"
41
41
  },
42
42
  "devDependencies": {
43
43
  "defu": "^6.1.1"