Skip to content

array events

Tyler Long edited this page Oct 11, 2018 · 1 revision

Array events

In JavaScript, array is also object (typeof [] === 'object'). This library works with array as well.

const list = SubX.create([1,2,3])
list.$.subscribe(console.log)
list.push(4)
list.shift()

Console output

{ type: 'SET', path: ['3'], val: 4, oldVal: undefined }
{ type: 'SET', path: ['length'], val: 4, oldVal: 4 }
{ type: 'SET', path: ['0'], val: 2, oldVal: 1 }
{ type: 'SET', path: ['1'], val: 3, oldVal: 2 }
{ type: 'SET', path: ['2'], val: 4, oldVal: 3 }
{ type: 'DELETE', path: ['3'], val: 4 }
{ type: 'SET', path: ['length'], val: 3, oldVal: 4 }

You can see that a single method call list.shift() could trigger multiple events. The behavior is idential to Proxy handler.