adapt-authoring-core 2.3.0 → 2.3.1

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/lib/Hook.js CHANGED
@@ -96,13 +96,19 @@ class Hook {
96
96
  * @return {Promise}
97
97
  */
98
98
  async _invokeMiddleware (coreFn, ...args) {
99
- let fn = coreFn
99
+ let coreResult
100
+ const wrappedCoreFn = async (...a) => {
101
+ coreResult = await coreFn(...a)
102
+ return coreResult
103
+ }
104
+ let fn = wrappedCoreFn
100
105
  for (let i = this._hookObservers.length - 1; i >= 0; i--) {
101
106
  const observer = this._hookObservers[i]
102
107
  const next = fn
103
108
  fn = (...a) => observer(next, ...a)
104
109
  }
105
- return fn(...args)
110
+ const result = await fn(...args)
111
+ return result !== undefined ? result : coreResult
106
112
  }
107
113
  }
108
114
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-core",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "A bundle of reusable 'core' functionality",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-core",
6
6
  "license": "GPL-3.0",
@@ -483,6 +483,36 @@ describe('Hook', () => {
483
483
  const result = await hook.invoke(core, 1)
484
484
  assert.equal(result, 2)
485
485
  })
486
+
487
+ it('should fall back to core result when observer calls next() without returning', async () => {
488
+ const hook = new Hook({ type: Hook.Types.Middleware })
489
+ hook.tap(async (next, val) => {
490
+ await next(val) // calls next but doesn't return the result
491
+ })
492
+ const core = async (val) => val * 3
493
+ const result = await hook.invoke(core, 7)
494
+ assert.equal(result, 21)
495
+ })
496
+
497
+ it('should fall back to core result through multiple non-returning observers', async () => {
498
+ const hook = new Hook({ type: Hook.Types.Middleware })
499
+ hook.tap(async (next, val) => { await next(val) })
500
+ hook.tap(async (next, val) => { await next(val) })
501
+ const core = async (val) => ({ id: val })
502
+ const result = await hook.invoke(core, 42)
503
+ assert.deepEqual(result, { id: 42 })
504
+ })
505
+
506
+ it('should prefer explicit observer return over core result fallback', async () => {
507
+ const hook = new Hook({ type: Hook.Types.Middleware })
508
+ hook.tap(async (next, val) => {
509
+ await next(val)
510
+ return 'transformed'
511
+ })
512
+ const core = async (val) => val
513
+ const result = await hook.invoke(core, 'original')
514
+ assert.equal(result, 'transformed')
515
+ })
486
516
  })
487
517
  })
488
518