Skip to content

Commit

Permalink
feat(TypedArrays): add reactivity support for typed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
solkimicreb committed Mar 8, 2018
1 parent 48cb553 commit aaa212a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 35 deletions.
13 changes: 11 additions & 2 deletions src/builtIns/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import collectionHandlers from './collections'

// eslint-disable-next-line
const globalObj = Function('return this')()
const globalObj = Function("return this")();

// built-in object can not be wrapped by Proxies
// their methods expect the object instance as the 'this' instead of the Proxy wrapper
Expand All @@ -13,7 +13,16 @@ const handlers = new Map([
[WeakMap, collectionHandlers],
[WeakSet, collectionHandlers],
[Object, false],
[Array, false]
[Array, false],
[Int8Array, false],
[Uint8Array, false],
[Uint8ClampedArray, false],
[Int16Array, false],
[Uint16Array, false],
[Int32Array, false],
[Uint32Array, false],
[Float32Array, false],
[Float64Array, false]
])

export function shouldInstrument ({ constructor }) {
Expand Down
11 changes: 0 additions & 11 deletions tests/builtIns/Date.test.js

This file was deleted.

11 changes: 0 additions & 11 deletions tests/builtIns/Node.test.js

This file was deleted.

11 changes: 0 additions & 11 deletions tests/builtIns/RegExp.test.js

This file was deleted.

55 changes: 55 additions & 0 deletions tests/builtIns/builtIns.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { expect } from 'chai'
import { observable, isObservable } from '@nx-js/observer-util'

describe('none observable built-ins', () => {
it('objects with global constructors should not be converted to observables', () => {
window.MyClass = class MyClass {}
const obj = new window.MyClass()
const obs = observable(obj)
expect(obs).to.equal(obj)
expect(isObservable(obs)).to.equal(false)
})

it('objects with local constructors should be converted to observables', () => {
class MyClass {}
const obj = new MyClass()
const obs = observable(obj)
expect(obs).to.not.equal(obj)
expect(isObservable(obs)).to.equal(true)
})

it('global objects should be converted to observables', () => {
window.obj = {}
const obs = observable(window.obj)
expect(obs).to.not.equal(window.obj)
expect(isObservable(obs)).to.equal(true)
})

it('Date should not be converted to observable', () => {
const date = new Date()
const obsDate = observable(date)
expect(obsDate).to.equal(date)
expect(isObservable(obsDate)).to.equal(false)
})

it('RegExp should not be converted to observable', () => {
const regex = new RegExp()
const obsRegex = observable(regex)
expect(obsRegex).to.equal(regex)
expect(isObservable(obsRegex)).to.equal(false)
})

it('Node should not be converted to observable', () => {
const node = document
const obsNode = observable(node)
expect(obsNode).to.equal(node)
expect(isObservable(obsNode)).to.equal(false)
})

it('WebAudio should not be converted to observable', () => {
const audio = new AudioContext()
const obsAudio = observable(audio)
expect(obsAudio).to.equal(audio)
expect(isObservable(obsAudio)).to.equal(false)
})
})
30 changes: 30 additions & 0 deletions tests/builtIns/typedArrays.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from 'chai'
import { observable, isObservable, observe } from '@nx-js/observer-util'

const TypedArrays = [
Int8Array,
Uint8Array,
Uint8ClampedArray,
Int16Array,
Uint16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array
]

describe('typed arrays', () => {
for (const TypedArray of TypedArrays) {
it(`${TypedArray.name} should observe mutations`, () => {
let dummy
const array = observable(new TypedArray(2))
expect(isObservable(array)).to.equal(true)

observe(() => (dummy = array[0]))

expect(dummy).to.equal(0)
array[0] = 12
expect(dummy).to.equal(12)
})
}
})

0 comments on commit aaa212a

Please sign in to comment.