-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update and maintain __tres parent/objects graph (#741)
- Loading branch information
1 parent
9b50538
commit 88150e3
Showing
6 changed files
with
236 additions
and
30 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
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,62 @@ | ||
import * as utils from './index' | ||
|
||
describe('filterInPlace', () => { | ||
it('returns the passed array', () => { | ||
const arr = [1, 2, 3] | ||
const result = utils.filterInPlace(arr, v => v !== 0) | ||
expect(result).toBe(arr) | ||
}) | ||
it('removes a single occurence', () => { | ||
const arr = [1, 2, 3] | ||
utils.filterInPlace(arr, v => v !== 1) | ||
expect(arr).toStrictEqual([2, 3]) | ||
}) | ||
it('removes every occurence 0', () => { | ||
const arr = [1, 1, 2, 1, 3, 1] | ||
utils.filterInPlace(arr, v => v !== 1) | ||
expect(arr).toStrictEqual([2, 3]) | ||
}) | ||
|
||
it('removes every occurence 1', () => { | ||
const [a, b, c] = [{}, {}, {}] | ||
const COUNT = 400 | ||
const arr = [] | ||
for (const val of [a, b, c]) { | ||
for (let i = 0; i < COUNT; i++) { | ||
arr.push(val) | ||
} | ||
} | ||
shuffle(arr) | ||
|
||
let filtered = [...arr] | ||
utils.filterInPlace(arr, v => v !== b) | ||
filtered = filtered.filter(v => v !== b) | ||
expect(arr).toStrictEqual(filtered) | ||
|
||
utils.filterInPlace(arr, v => v !== c) | ||
filtered = filtered.filter(v => v !== c) | ||
expect(arr).toStrictEqual(filtered) | ||
|
||
utils.filterInPlace(arr, v => v !== a) | ||
expect(arr).toStrictEqual([]) | ||
}) | ||
|
||
it('sends an index to the callbackFn', () => { | ||
const arr = 'abcdefghi'.split('') | ||
utils.filterInPlace(arr, (_, i) => i % 2 === 0) | ||
expect(arr).toStrictEqual('acegi'.split('')) | ||
}) | ||
}) | ||
|
||
function shuffle(array: any[]) { | ||
let currentIndex = array.length | ||
while (currentIndex !== 0) { | ||
const randomIndex = Math.floor(Math.random() * currentIndex) | ||
currentIndex--; | ||
[array[currentIndex], array[randomIndex]] = [ | ||
array[randomIndex], | ||
array[currentIndex], | ||
] | ||
} | ||
return array | ||
}; |
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,32 @@ | ||
import type { TresContext } from '../composables/useTresContextProvider' | ||
import type { LocalState, TresInstance, TresObject } from '../types' | ||
|
||
export function prepareTresInstance<T extends TresObject>(obj: T, state: Partial<LocalState>, context: TresContext): TresInstance { | ||
const instance = obj as unknown as TresInstance | ||
instance.__tres = { | ||
type: 'unknown', | ||
eventCount: 0, | ||
root: context, | ||
handlers: {}, | ||
memoizedProps: {}, | ||
objects: [], | ||
parent: null, | ||
...state, | ||
} | ||
return instance | ||
} | ||
|
||
export function invalidateInstance(instance: TresObject) { | ||
const ctx = instance?.__tres?.root | ||
|
||
if (!ctx) { return } | ||
|
||
if (ctx.render && ctx.render.canBeInvalidated.value) { | ||
ctx.invalidate() | ||
} | ||
} | ||
|
||
export function noop(fn: string): any { | ||
// eslint-disable-next-line no-unused-expressions | ||
fn | ||
} |