-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(playcanvas): remove on load behavior of playcanvas adapter
- Loading branch information
Showing
5 changed files
with
74 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { cachedValueOf } from '../operator'; | ||
|
||
export const hijackArray = (ArrayClass) => { | ||
const { prototype } = ArrayClass; | ||
|
||
Object.defineProperty(prototype, 'x', { | ||
get() { | ||
return this[0] || 0; | ||
}, | ||
set(x) { | ||
this[0] = x; | ||
} | ||
}); | ||
|
||
Object.defineProperty(prototype, 'y', { | ||
get() { | ||
return this[1] || 0; | ||
}, | ||
set(y) { | ||
this[1] = y; | ||
} | ||
}); | ||
|
||
Object.defineProperty(prototype, 'z', { | ||
get() { | ||
return this[2] || 0; | ||
}, | ||
set(z) { | ||
this[2] = z; | ||
} | ||
}); | ||
|
||
cachedValueOf(ArrayClass); | ||
|
||
Object.defineProperty(prototype, 'len', { | ||
get() { | ||
return Math.sqrt((this[0] ** 2) + (this[1] ** 2) + (this[2] ** 2)) ** (1 / 2); | ||
}, | ||
set() { | ||
throw new Error('set len not allowed'); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { assert } from 'chai'; | ||
import { hijackArray } from '../../src/adapter/array'; | ||
import { calc } from '../../src/index'; | ||
|
||
describe('override valueOf of Array', () => { | ||
it('calculations with the arrays created by [] operator', () => { | ||
hijackArray(Array); | ||
|
||
const t1 = [3, 4, 5]; | ||
const t2 = [6, 7, 8]; | ||
const pos = calc(() => t1 + t2 * 2); | ||
|
||
assert.instanceOf(pos, Array); | ||
assert.equal(pos.x, 15); | ||
assert.equal(pos.y, 18); | ||
assert.equal(pos.z, 21); | ||
}); | ||
}); |