halfcab 16.0.0 → 16.0.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.
Files changed (3) hide show
  1. package/halfcab.mjs +15 -1
  2. package/package.json +1 -1
  3. package/test.js +48 -0
package/halfcab.mjs CHANGED
@@ -64,6 +64,8 @@ if (typeof window !== 'undefined') {
64
64
 
65
65
  const proxyCache = new WeakMap()
66
66
 
67
+ const ARRAY_MUTATING_METHODS = new Set(['push', 'pop', 'shift', 'unshift', 'splice', 'sort', 'reverse', 'fill', 'copyWithin'])
68
+
67
69
  function createState (target) {
68
70
  if (proxyCache.has(target)) {
69
71
  return proxyCache.get(target)
@@ -71,7 +73,14 @@ function createState (target) {
71
73
  const proxy = new Proxy(target, {
72
74
  get (obj, prop) {
73
75
  const value = obj[prop]
74
- if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
76
+ if (Array.isArray(obj) && typeof value === 'function' && ARRAY_MUTATING_METHODS.has(prop)) {
77
+ return function (...args) {
78
+ const result = Array.prototype[prop].apply(obj, args)
79
+ debounce(stateUpdated)
80
+ return result
81
+ }
82
+ }
83
+ if (value !== null && typeof value === 'object') {
75
84
  return createState(value)
76
85
  }
77
86
  return value
@@ -80,6 +89,11 @@ function createState (target) {
80
89
  obj[prop] = value
81
90
  debounce(stateUpdated)
82
91
  return true
92
+ },
93
+ deleteProperty (obj, prop) {
94
+ delete obj[prop]
95
+ debounce(stateUpdated)
96
+ return true
83
97
  }
84
98
  })
85
99
  proxyCache.set(target, proxy)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "halfcab",
3
- "version": "16.0.0",
3
+ "version": "16.0.1",
4
4
  "type": "module",
5
5
  "description": "A simple universal JavaScript framework focused on making use of es2015 template strings to build components.",
6
6
  "main": "halfcab.mjs",
package/test.js CHANGED
@@ -453,6 +453,54 @@ describe('halfcab', () => {
453
453
  })
454
454
  })
455
455
 
456
+ it('Triggers rerender when a property is deleted from state', (done) => {
457
+ halfcab({
458
+ el: '#root',
459
+ components () {
460
+ return html `<div></div>`
461
+ }
462
+ })
463
+ .then(({rootEl, state}) => {
464
+ updateState({ toDelete: 'bye' })
465
+
466
+ nextTick(() => {
467
+ expect(state.toDelete).to.equal('bye')
468
+ delete state.toDelete
469
+
470
+ nextTick(() => {
471
+ expect(state.toDelete).to.be.undefined()
472
+ done()
473
+ })
474
+ })
475
+ })
476
+ })
477
+
478
+ it('Triggers rerender when array mutation methods are used on state', (done) => {
479
+ halfcab({
480
+ el: '#root',
481
+ components () {
482
+ return html `<div></div>`
483
+ }
484
+ })
485
+ .then(({rootEl, state}) => {
486
+ updateState({ items: [1, 2, 3] })
487
+
488
+ nextTick(() => {
489
+ state.items.push(4)
490
+
491
+ nextTick(() => {
492
+ expect(state.items).to.deep.equal([1, 2, 3, 4])
493
+ state.items.pop()
494
+
495
+ nextTick(() => {
496
+ expect(state.items).to.deep.equal([1, 2, 3])
497
+ done()
498
+ })
499
+ })
500
+ })
501
+ })
502
+ })
503
+
456
504
  it(`Doesn't clone when merging`, (done) => {
457
505
  halfcab({
458
506
  el: '#root',