sprae 2.2.2 → 2.2.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": "sprae",
3
3
  "description": "DOM microhydration.",
4
- "version": "2.2.2",
4
+ "version": "2.2.3",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",
7
7
  "type": "module",
package/src/directives.js CHANGED
@@ -207,14 +207,16 @@ directives['on'] = (el, expr, state) => {
207
207
  const evts = evt.split('-')
208
208
  if (evts.length===1) el.addEventListener(evt, listeners[evt]);
209
209
  else {
210
+ const startFn = listeners[evt]
210
211
  const nextEvt = (fn, cur=0) => {
211
212
  el.addEventListener(evts[cur], listeners[evt] = e => {
212
213
  fn = fn(e)
213
214
  el.removeEventListener(evts[cur], listeners[evt])
214
- nextEvt(fn, (cur+1)%evts.length)
215
+ if (++cur < evts.length && typeof fn === 'function') nextEvt(fn, cur)
216
+ else nextEvt(startFn)
215
217
  })
216
218
  }
217
- nextEvt(listeners[evt])
219
+ nextEvt(startFn)
218
220
  }
219
221
  };
220
222
  })
package/test/test.js CHANGED
@@ -370,6 +370,36 @@ test('on: in-out events', e => {
370
370
  is(state.log, ['mousedown','mouseup'])
371
371
  })
372
372
 
373
+ test('on: in-out side-effects', e => {
374
+ let log = []
375
+
376
+ // 1. skip in event and do directly out
377
+ let el = h`<x :onin-onout="io"></x>`
378
+ sprae(el, { io(e) {
379
+ log.push(e.type)
380
+ return (e) => (log.push(e.type), [1,2,3])
381
+ } })
382
+
383
+ el.dispatchEvent(new window.Event('out'));
384
+ is(log, [])
385
+
386
+ // 2. Some nonsensical return is fine
387
+ el.dispatchEvent(new window.Event('in'));
388
+ is(log, ['in'])
389
+ el.dispatchEvent(new window.Event('out'));
390
+ is(log, ['in','out'], 'out triggers right')
391
+ el.dispatchEvent(new window.Event('out'));
392
+ is(log, ['in','out'])
393
+ el.dispatchEvent(new window.Event('in'));
394
+ is(log, ['in','out','in'])
395
+ el.dispatchEvent(new window.Event('in'));
396
+ is(log, ['in','out','in'])
397
+ el.dispatchEvent(new window.Event('out'));
398
+ is(log, ['in','out','in','out'])
399
+ el.dispatchEvent(new window.Event('out'));
400
+ is(log, ['in','out','in','out'])
401
+ })
402
+
373
403
  test('on: chain of events', e => {
374
404
  let el = h`<div :onmousedown-onmousemove-onmouseup="e=>(log.push(e.type),e=>(log.push(e.type),e=>log.push(e.type)))"></div>`
375
405
  let state = sprae(el, {log:[]})