moost 0.3.44 → 0.4.0

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
@@ -4,11 +4,11 @@ var eventCore = require('@wooksjs/event-core');
4
4
  var infact$1 = require('@prostojs/infact');
5
5
  var mate$1 = require('@prostojs/mate');
6
6
  var logger = require('@prostojs/logger');
7
+ var hookable = require('hookable');
7
8
 
8
- async function runPipes(pipes, initialValue, metas, level, restoreCtx) {
9
+ async function runPipes(pipes, initialValue, metas, level) {
9
10
  let v = initialValue;
10
11
  for (const pipe of pipes) {
11
- restoreCtx?.();
12
12
  v = await pipe.handler(v, metas, level);
13
13
  }
14
14
  return v;
@@ -83,13 +83,13 @@ function getNewMoostInfact() {
83
83
  }
84
84
 
85
85
  function setControllerContext(controller, method) {
86
- const { store } = eventCore.useEventContext();
86
+ const { store } = eventCore.useAsyncEventContext();
87
87
  const { set } = store('controller');
88
88
  set('instance', controller);
89
89
  set('method', method);
90
90
  }
91
91
  function useControllerContext() {
92
- const { store } = eventCore.useEventContext();
92
+ const { store } = eventCore.useAsyncEventContext();
93
93
  const { get } = store('controller');
94
94
  const getController = () => get('instance');
95
95
  const getMethod = () => get('method');
@@ -120,13 +120,11 @@ function registerEventScope(scopeId) {
120
120
  }
121
121
  function defineMoostEventHandler(options) {
122
122
  return async () => {
123
- const { restoreCtx } = eventCore.useEventContext(options.contextType);
124
123
  const scopeId = eventCore.useEventId().getId();
125
124
  const logger = eventCore.useEventLogger(options.loggerTitle);
126
125
  const unscope = registerEventScope(scopeId);
127
126
  let response;
128
127
  const hookOptions = {
129
- restoreCtx,
130
128
  scopeId,
131
129
  logger,
132
130
  unscope,
@@ -136,16 +134,13 @@ function defineMoostEventHandler(options) {
136
134
  };
137
135
  if (options.hooks?.init) {
138
136
  await options.hooks.init(hookOptions);
139
- restoreCtx();
140
137
  }
141
138
  const instance = await options.getControllerInstance();
142
- restoreCtx();
143
139
  if (instance) {
144
140
  setControllerContext(instance, options.controllerMethod || '');
145
141
  }
146
142
  const interceptorHandler = await options.getIterceptorHandler();
147
143
  if (interceptorHandler) {
148
- restoreCtx();
149
144
  try {
150
145
  response = await interceptorHandler.init();
151
146
  if (response !== undefined) {
@@ -160,7 +155,6 @@ function defineMoostEventHandler(options) {
160
155
  }
161
156
  let args = [];
162
157
  if (options.resolveArgs) {
163
- restoreCtx();
164
158
  try {
165
159
  args = await options.resolveArgs();
166
160
  }
@@ -171,14 +165,12 @@ function defineMoostEventHandler(options) {
171
165
  }
172
166
  }
173
167
  if (interceptorHandler) {
174
- restoreCtx();
175
168
  response = await interceptorHandler.fireBefore(response);
176
169
  if (response !== undefined) {
177
170
  return endWithResponse();
178
171
  }
179
172
  }
180
173
  const callControllerMethod = () => {
181
- restoreCtx();
182
174
  if (options.callControllerMethod) {
183
175
  return options.callControllerMethod(args);
184
176
  }
@@ -198,7 +190,6 @@ function defineMoostEventHandler(options) {
198
190
  }
199
191
  async function endWithResponse(raise = false) {
200
192
  if (interceptorHandler) {
201
- restoreCtx();
202
193
  try {
203
194
  response = await interceptorHandler.fireAfter(response);
204
195
  }
@@ -264,16 +255,13 @@ function getDefaultLogger(topic) {
264
255
  }, topic);
265
256
  }
266
257
 
267
- async function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
258
+ async function getCallableFn(targetInstance, fn, pipes, logger) {
268
259
  const mate = getMoostMate();
269
260
  const meta = mate.read(fn);
270
261
  if (meta?.injectable) {
271
262
  const infact = getMoostInfact();
272
263
  infact.silent(true);
273
264
  const instance = (await infact.getForInstance(targetInstance, fn, {
274
- syncContextFn: () => {
275
- restoreCtx();
276
- },
277
265
  customData: {
278
266
  pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority),
279
267
  },
@@ -302,9 +290,7 @@ class InterceptorHandler {
302
290
  this.responseOverwritten = true;
303
291
  }
304
292
  async init() {
305
- const { restoreCtx } = eventCore.useEventContext();
306
293
  for (const handler of this.handlers) {
307
- restoreCtx();
308
294
  const response = await handler(fn => {
309
295
  this.before.push(fn);
310
296
  }, fn => {
@@ -318,10 +304,8 @@ class InterceptorHandler {
318
304
  }
319
305
  }
320
306
  async fireBefore(response) {
321
- const { restoreCtx } = eventCore.useEventContext();
322
307
  this.response = response;
323
308
  for (const handler of this.before) {
324
- restoreCtx();
325
309
  await handler(this.replyFn.bind(this));
326
310
  if (this.responseOverwritten) {
327
311
  break;
@@ -330,17 +314,14 @@ class InterceptorHandler {
330
314
  return this.response;
331
315
  }
332
316
  async fireAfter(response) {
333
- const { restoreCtx } = eventCore.useEventContext();
334
317
  this.response = response;
335
318
  if (response instanceof Error) {
336
319
  for (const handler of this.onError) {
337
- restoreCtx();
338
320
  await handler(response, this.replyFn.bind(this));
339
321
  }
340
322
  }
341
323
  else {
342
324
  for (const handler of this.after) {
343
- restoreCtx();
344
325
  await handler(response, this.replyFn.bind(this));
345
326
  }
346
327
  }
@@ -356,10 +337,8 @@ function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, log
356
337
  const interceptorMeta = mate.read(handler);
357
338
  if (interceptorMeta?.injectable) {
358
339
  interceptorHandlers.push(async (...args) => {
359
- const { restoreCtx } = eventCore.useEventContext();
360
340
  const targetInstance = await getTargetInstance();
361
- restoreCtx();
362
- return (await getCallableFn(targetInstance, handler, restoreCtx, pipes, logger))(...args);
341
+ return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
363
342
  });
364
343
  }
365
344
  else {
@@ -412,7 +391,6 @@ async function bindControllerMethods(options) {
412
391
  }
413
392
  const resolveArgs = async () => {
414
393
  const args = [];
415
- const { restoreCtx } = eventCore.useEventContext();
416
394
  for (const [i, { pipes, meta: paramMeta }] of argsPipes.entries()) {
417
395
  args[i] = await runPipes(pipes, undefined, {
418
396
  classMeta: meta,
@@ -422,7 +400,7 @@ async function bindControllerMethods(options) {
422
400
  key: method,
423
401
  index: i,
424
402
  targetMeta: paramMeta,
425
- }, 'PARAM', restoreCtx);
403
+ }, 'PARAM');
426
404
  }
427
405
  return args;
428
406
  };
@@ -645,8 +623,9 @@ const sharedPipes = [
645
623
  },
646
624
  ];
647
625
 
648
- class Moost {
626
+ class Moost extends hookable.Hookable {
649
627
  constructor(options) {
628
+ super();
650
629
  this.options = options;
651
630
  this.pipes = Array.from(sharedPipes);
652
631
  this.interceptors = [];
@@ -660,6 +639,12 @@ class Moost {
660
639
  const mate = getMoostMate();
661
640
  Object.assign(mate, { logger: this.getLogger('mate') });
662
641
  }
642
+ _fireEventStart(source) {
643
+ this.callHook('event-start', source);
644
+ }
645
+ _fireEventEnd(source) {
646
+ this.callHook('event-end', source);
647
+ }
663
648
  getLogger(topic) {
664
649
  if (this.logger instanceof logger.ProstoLogger) {
665
650
  return this.logger.createTopic(topic);
@@ -718,12 +703,13 @@ class Moost {
718
703
  const infactOpts = { provide, replace, customData: { pipes } };
719
704
  if (isControllerConsructor &&
720
705
  (classMeta?.injectable === 'SINGLETON' || classMeta?.injectable === true)) {
721
- eventCore.createEventContext({
706
+ await eventCore.createAsyncEventContext({
722
707
  event: { type: 'init' },
723
708
  options: {},
709
+ })(async () => {
710
+ setControllerContext(this, 'bindController');
711
+ instance = (await infact.get(controller, infactOpts));
724
712
  });
725
- setControllerContext(this, 'bindController');
726
- instance = (await infact.get(controller, infactOpts));
727
713
  }
728
714
  else if (!isControllerConsructor) {
729
715
  instance = controller;
@@ -733,10 +719,8 @@ class Moost {
733
719
  ? () => Promise.resolve(instance)
734
720
  : async () => {
735
721
  infact.silent(true);
736
- const { restoreCtx } = eventCore.useEventContext();
737
722
  const instance = (await infact.get(controller, {
738
723
  ...infactOpts,
739
- syncContextFn: restoreCtx,
740
724
  }));
741
725
  infact.silent(false);
742
726
  return instance;
@@ -839,9 +823,13 @@ Object.defineProperty(exports, "EventLogger", {
839
823
  enumerable: true,
840
824
  get: function () { return eventCore.EventLogger; }
841
825
  });
842
- Object.defineProperty(exports, "useEventContext", {
826
+ Object.defineProperty(exports, "eventContextHooks", {
827
+ enumerable: true,
828
+ get: function () { return eventCore.eventContextHooks; }
829
+ });
830
+ Object.defineProperty(exports, "useAsyncEventContext", {
843
831
  enumerable: true,
844
- get: function () { return eventCore.useEventContext; }
832
+ get: function () { return eventCore.useAsyncEventContext; }
845
833
  });
846
834
  Object.defineProperty(exports, "useEventLogger", {
847
835
  enumerable: true,
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { useEventLogger } from '@wooksjs/event-core';
2
- export { EventLogger, THook, useEventContext, useEventLogger } from '@wooksjs/event-core';
2
+ export { EventLogger, THook, eventContextHooks, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
3
3
  import { TProvideRegistry, Infact, TReplaceRegistry, TProvideFn } from '@prostojs/infact';
4
4
  export { TProvideRegistry, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
5
5
  import { TConsoleBase } from '@prostojs/logger';
6
+ import { Hookable } from 'hookable';
6
7
  import * as _prostojs_mate from '@prostojs/mate';
7
8
  import { TMateParamMeta, Mate } from '@prostojs/mate';
8
9
  export { getConstructor, isConstructor } from '@prostojs/mate';
@@ -332,7 +333,6 @@ declare class InterceptorHandler {
332
333
  }
333
334
 
334
335
  interface TMoostEventHandlerHookOptions<T> {
335
- restoreCtx: () => void;
336
336
  scopeId: string;
337
337
  logger: ReturnType<typeof useEventLogger>;
338
338
  unscope: () => void;
@@ -437,7 +437,7 @@ interface TMoostOptions {
437
437
  * │ app.init()
438
438
  * ```
439
439
  */
440
- declare class Moost {
440
+ declare class Moost extends Hookable {
441
441
  protected options?: TMoostOptions | undefined;
442
442
  protected logger: TConsoleBase;
443
443
  protected pipes: TPipeData[];
@@ -448,6 +448,8 @@ declare class Moost {
448
448
  protected replace: TReplaceRegistry;
449
449
  protected unregisteredControllers: Array<TObject | TFunction | [string, TObject | TFunction]>;
450
450
  constructor(options?: TMoostOptions | undefined);
451
+ _fireEventStart(source: TMoostAdapter<unknown>): void;
452
+ _fireEventEnd(source: TMoostAdapter<unknown>): void;
451
453
  /**
452
454
  * ### getLogger
453
455
  * Provides application logger
@@ -510,6 +512,7 @@ interface TMoostAdapterOptions<H, T> {
510
512
  register: (handler: TMoostHandler<TEmpty>, path: string, args: string[]) => void;
511
513
  }
512
514
  interface TMoostAdapter<H> {
515
+ name: string;
513
516
  bindHandler: <T extends TObject = TObject>(options: TMoostAdapterOptions<H, T>) => void | Promise<void>;
514
517
  onInit?: (moost: Moost) => void | Promise<void>;
515
518
  getProvideRegistry?: () => TProvideRegistry;
package/dist/index.mjs CHANGED
@@ -1,15 +1,15 @@
1
- import { useEventId, useEventContext, useEventLogger, useRouteParams, createEventContext } from '@wooksjs/event-core';
2
- export { EventLogger, useEventContext, useEventLogger } from '@wooksjs/event-core';
1
+ import { useEventId, useAsyncEventContext, useEventLogger, useRouteParams, createAsyncEventContext } from '@wooksjs/event-core';
2
+ export { EventLogger, eventContextHooks, useAsyncEventContext, useEventLogger } from '@wooksjs/event-core';
3
3
  import { Infact, createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
4
4
  export { createProvideRegistry, createReplaceRegistry } from '@prostojs/infact';
5
5
  import { Mate, getConstructor, isConstructor } from '@prostojs/mate';
6
6
  export { getConstructor, isConstructor } from '@prostojs/mate';
7
7
  import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
8
+ import { Hookable } from 'hookable';
8
9
 
9
- async function runPipes(pipes, initialValue, metas, level, restoreCtx) {
10
+ async function runPipes(pipes, initialValue, metas, level) {
10
11
  let v = initialValue;
11
12
  for (const pipe of pipes) {
12
- restoreCtx?.();
13
13
  v = await pipe.handler(v, metas, level);
14
14
  }
15
15
  return v;
@@ -84,13 +84,13 @@ function getNewMoostInfact() {
84
84
  }
85
85
 
86
86
  function setControllerContext(controller, method) {
87
- const { store } = useEventContext();
87
+ const { store } = useAsyncEventContext();
88
88
  const { set } = store('controller');
89
89
  set('instance', controller);
90
90
  set('method', method);
91
91
  }
92
92
  function useControllerContext() {
93
- const { store } = useEventContext();
93
+ const { store } = useAsyncEventContext();
94
94
  const { get } = store('controller');
95
95
  const getController = () => get('instance');
96
96
  const getMethod = () => get('method');
@@ -121,13 +121,11 @@ function registerEventScope(scopeId) {
121
121
  }
122
122
  function defineMoostEventHandler(options) {
123
123
  return async () => {
124
- const { restoreCtx } = useEventContext(options.contextType);
125
124
  const scopeId = useEventId().getId();
126
125
  const logger = useEventLogger(options.loggerTitle);
127
126
  const unscope = registerEventScope(scopeId);
128
127
  let response;
129
128
  const hookOptions = {
130
- restoreCtx,
131
129
  scopeId,
132
130
  logger,
133
131
  unscope,
@@ -137,16 +135,13 @@ function defineMoostEventHandler(options) {
137
135
  };
138
136
  if (options.hooks?.init) {
139
137
  await options.hooks.init(hookOptions);
140
- restoreCtx();
141
138
  }
142
139
  const instance = await options.getControllerInstance();
143
- restoreCtx();
144
140
  if (instance) {
145
141
  setControllerContext(instance, options.controllerMethod || '');
146
142
  }
147
143
  const interceptorHandler = await options.getIterceptorHandler();
148
144
  if (interceptorHandler) {
149
- restoreCtx();
150
145
  try {
151
146
  response = await interceptorHandler.init();
152
147
  if (response !== undefined) {
@@ -161,7 +156,6 @@ function defineMoostEventHandler(options) {
161
156
  }
162
157
  let args = [];
163
158
  if (options.resolveArgs) {
164
- restoreCtx();
165
159
  try {
166
160
  args = await options.resolveArgs();
167
161
  }
@@ -172,14 +166,12 @@ function defineMoostEventHandler(options) {
172
166
  }
173
167
  }
174
168
  if (interceptorHandler) {
175
- restoreCtx();
176
169
  response = await interceptorHandler.fireBefore(response);
177
170
  if (response !== undefined) {
178
171
  return endWithResponse();
179
172
  }
180
173
  }
181
174
  const callControllerMethod = () => {
182
- restoreCtx();
183
175
  if (options.callControllerMethod) {
184
176
  return options.callControllerMethod(args);
185
177
  }
@@ -199,7 +191,6 @@ function defineMoostEventHandler(options) {
199
191
  }
200
192
  async function endWithResponse(raise = false) {
201
193
  if (interceptorHandler) {
202
- restoreCtx();
203
194
  try {
204
195
  response = await interceptorHandler.fireAfter(response);
205
196
  }
@@ -265,16 +256,13 @@ function getDefaultLogger(topic) {
265
256
  }, topic);
266
257
  }
267
258
 
268
- async function getCallableFn(targetInstance, fn, restoreCtx, pipes, logger) {
259
+ async function getCallableFn(targetInstance, fn, pipes, logger) {
269
260
  const mate = getMoostMate();
270
261
  const meta = mate.read(fn);
271
262
  if (meta?.injectable) {
272
263
  const infact = getMoostInfact();
273
264
  infact.silent(true);
274
265
  const instance = (await infact.getForInstance(targetInstance, fn, {
275
- syncContextFn: () => {
276
- restoreCtx();
277
- },
278
266
  customData: {
279
267
  pipes: [...(pipes || []), ...(meta.pipes || [])].sort((a, b) => a.priority - b.priority),
280
268
  },
@@ -303,9 +291,7 @@ class InterceptorHandler {
303
291
  this.responseOverwritten = true;
304
292
  }
305
293
  async init() {
306
- const { restoreCtx } = useEventContext();
307
294
  for (const handler of this.handlers) {
308
- restoreCtx();
309
295
  const response = await handler(fn => {
310
296
  this.before.push(fn);
311
297
  }, fn => {
@@ -319,10 +305,8 @@ class InterceptorHandler {
319
305
  }
320
306
  }
321
307
  async fireBefore(response) {
322
- const { restoreCtx } = useEventContext();
323
308
  this.response = response;
324
309
  for (const handler of this.before) {
325
- restoreCtx();
326
310
  await handler(this.replyFn.bind(this));
327
311
  if (this.responseOverwritten) {
328
312
  break;
@@ -331,17 +315,14 @@ class InterceptorHandler {
331
315
  return this.response;
332
316
  }
333
317
  async fireAfter(response) {
334
- const { restoreCtx } = useEventContext();
335
318
  this.response = response;
336
319
  if (response instanceof Error) {
337
320
  for (const handler of this.onError) {
338
- restoreCtx();
339
321
  await handler(response, this.replyFn.bind(this));
340
322
  }
341
323
  }
342
324
  else {
343
325
  for (const handler of this.after) {
344
- restoreCtx();
345
326
  await handler(response, this.replyFn.bind(this));
346
327
  }
347
328
  }
@@ -357,10 +338,8 @@ function getIterceptorHandlerFactory(interceptors, getTargetInstance, pipes, log
357
338
  const interceptorMeta = mate.read(handler);
358
339
  if (interceptorMeta?.injectable) {
359
340
  interceptorHandlers.push(async (...args) => {
360
- const { restoreCtx } = useEventContext();
361
341
  const targetInstance = await getTargetInstance();
362
- restoreCtx();
363
- return (await getCallableFn(targetInstance, handler, restoreCtx, pipes, logger))(...args);
342
+ return (await getCallableFn(targetInstance, handler, pipes, logger))(...args);
364
343
  });
365
344
  }
366
345
  else {
@@ -413,7 +392,6 @@ async function bindControllerMethods(options) {
413
392
  }
414
393
  const resolveArgs = async () => {
415
394
  const args = [];
416
- const { restoreCtx } = useEventContext();
417
395
  for (const [i, { pipes, meta: paramMeta }] of argsPipes.entries()) {
418
396
  args[i] = await runPipes(pipes, undefined, {
419
397
  classMeta: meta,
@@ -423,7 +401,7 @@ async function bindControllerMethods(options) {
423
401
  key: method,
424
402
  index: i,
425
403
  targetMeta: paramMeta,
426
- }, 'PARAM', restoreCtx);
404
+ }, 'PARAM');
427
405
  }
428
406
  return args;
429
407
  };
@@ -646,8 +624,9 @@ const sharedPipes = [
646
624
  },
647
625
  ];
648
626
 
649
- class Moost {
627
+ class Moost extends Hookable {
650
628
  constructor(options) {
629
+ super();
651
630
  this.options = options;
652
631
  this.pipes = Array.from(sharedPipes);
653
632
  this.interceptors = [];
@@ -661,6 +640,12 @@ class Moost {
661
640
  const mate = getMoostMate();
662
641
  Object.assign(mate, { logger: this.getLogger('mate') });
663
642
  }
643
+ _fireEventStart(source) {
644
+ this.callHook('event-start', source);
645
+ }
646
+ _fireEventEnd(source) {
647
+ this.callHook('event-end', source);
648
+ }
664
649
  getLogger(topic) {
665
650
  if (this.logger instanceof ProstoLogger) {
666
651
  return this.logger.createTopic(topic);
@@ -719,12 +704,13 @@ class Moost {
719
704
  const infactOpts = { provide, replace, customData: { pipes } };
720
705
  if (isControllerConsructor &&
721
706
  (classMeta?.injectable === 'SINGLETON' || classMeta?.injectable === true)) {
722
- createEventContext({
707
+ await createAsyncEventContext({
723
708
  event: { type: 'init' },
724
709
  options: {},
710
+ })(async () => {
711
+ setControllerContext(this, 'bindController');
712
+ instance = (await infact.get(controller, infactOpts));
725
713
  });
726
- setControllerContext(this, 'bindController');
727
- instance = (await infact.get(controller, infactOpts));
728
714
  }
729
715
  else if (!isControllerConsructor) {
730
716
  instance = controller;
@@ -734,10 +720,8 @@ class Moost {
734
720
  ? () => Promise.resolve(instance)
735
721
  : async () => {
736
722
  infact.silent(true);
737
- const { restoreCtx } = useEventContext();
738
723
  const instance = (await infact.get(controller, {
739
724
  ...infactOpts,
740
- syncContextFn: restoreCtx,
741
725
  }));
742
726
  infact.silent(false);
743
727
  return instance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moost",
3
- "version": "0.3.44",
3
+ "version": "0.4.0",
4
4
  "description": "moost",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -34,8 +34,9 @@
34
34
  "@prostojs/infact": "^0.2.3",
35
35
  "@prostojs/mate": "^0.3.2",
36
36
  "@prostojs/logger": "^0.4.0",
37
- "@wooksjs/event-core": "^0.4.35",
38
- "wooks": "^0.4.35"
37
+ "@wooksjs/event-core": "^0.5.0",
38
+ "hookable": "^5.5.3",
39
+ "wooks": "^0.5.0"
39
40
  },
40
41
  "author": "Artem Maltsev",
41
42
  "license": "MIT",