5htp-core 0.1.1-2 → 0.1.1-3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp-core",
3
3
  "description": "5-HTP, scientifically called 5-Hydroxytryptophan, is the precursor of happiness neurotransmitter.",
4
- "version": "0.1.1-2",
4
+ "version": "0.1.1-3",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp-core.git",
7
7
  "license": "MIT",
@@ -86,6 +86,8 @@ export type TFrontRenderer<TControllerData extends TFetcherList = {}> = (
86
86
  context: ClientContext
87
87
  ) => ComponentChild
88
88
 
89
+ export type THookCallback = (request: ClientRequest) => void;
90
+
89
91
  // Simple link
90
92
  export const Link = ({ to, ...props }: {
91
93
  to: string,
@@ -112,6 +114,7 @@ export const Link = ({ to, ...props }: {
112
114
  }
113
115
 
114
116
  const debug = true;
117
+ const LogPrefix = '[router]'
115
118
 
116
119
  /*----------------------------------
117
120
  - ROUTER
@@ -122,6 +125,28 @@ class Router extends BaseRouter {
122
125
 
123
126
  public routes: (TClientRoute | TUnresolvedRoute)[] = [];
124
127
 
128
+ private hooks: {[hookname: string]: THookCallback[]} = {}
129
+ public on( hookName: string, callback: THookCallback ) {
130
+
131
+ debug && console.info(LogPrefix, `Register hook ${hookName}`);
132
+
133
+ let cbIndex: number;
134
+ if (!( hookName in this.hooks )) {
135
+ cbIndex = 0;
136
+ this.hooks[ hookName ] = [callback]
137
+ } else {
138
+ cbIndex = this.hooks[ hookName ].length;
139
+ this.hooks[ hookName ].push(callback);
140
+ }
141
+
142
+ // Listener remover
143
+ return () => {
144
+ debug && console.info(LogPrefix, `De-register hook ${hookName} (index ${cbIndex})`);
145
+ delete this.hooks[ hookName ][ cbIndex ];
146
+ }
147
+
148
+ }
149
+
125
150
  public set(data: TObjetDonnees) {
126
151
  throw new Error(`router.set was not attached to the router component.`);
127
152
  }
@@ -171,6 +196,9 @@ class Router extends BaseRouter {
171
196
  public async resolve( request: ClientRequest, context: ClientContext ): Promise<PageResponse | undefined | null> {
172
197
  debug && console.log('Resolving request', request.path, Object.keys(request.data));
173
198
 
199
+ for (const callbacks of this.hooks.onLocationChange)
200
+ callbacks(request);
201
+
174
202
  for (let iRoute = 0; iRoute < this.routes.length; iRoute++) {
175
203
 
176
204
  let route = this.routes[iRoute];