This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Guarantee immediate scheduling of async hook effects (#10252)
* Throw error when useComponent is given UndefinedEntity This is always a mistake, as it causes a reactor to suspend forever. * Gaurantee immediate scheduling of async hook effects * license data * Update MeshComponent.ts * Update ComponentFunctions.ts * Update resourceLoaderHooks.test.tsx * Update QueryFunctions.tsx * simplify useQuery * Update QueryFunctions.test.tsx * Update QueryFunctions.tsx * Move useImmediateEffect into hyperflux * Update QueryFunctions.tsx * Cleanup
- Loading branch information
Showing
14 changed files
with
354 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
CPAL-1.0 License | ||
The contents of this file are subject to the Common Public Attribution License | ||
Version 1.0. (the "License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. | ||
The License is based on the Mozilla Public License Version 1.1, but Sections 14 | ||
and 15 have been added to cover use of software over a computer network and | ||
provide for limited attribution for the Original Developer. In addition, | ||
Exhibit A has been modified to be consistent with Exhibit B. | ||
Software distributed under the License is distributed on an "AS IS" basis, | ||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the | ||
specific language governing rights and limitations under the License. | ||
The Original Code is Ethereal Engine. | ||
The Original Developer is the Initial Developer. The Initial Developer of the | ||
Original Code is the Ethereal Engine team. | ||
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 | ||
Ethereal Engine. All Rights Reserved. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react' | ||
import assert from 'assert' | ||
import { ComponentMap, defineComponent, hasComponent, removeComponent, setComponent } from './ComponentFunctions' | ||
import { destroyEngine, startEngine } from './Engine' | ||
import { createEntity } from './EntityFunctions' | ||
import { defineQuery, useQuery } from './QueryFunctions' | ||
|
||
const ComponentA = defineComponent({ name: 'ComponentA' }) | ||
const ComponentB = defineComponent({ name: 'ComponentB' }) | ||
|
||
describe('QueryFunctions', () => { | ||
beforeEach(() => { | ||
startEngine() | ||
}) | ||
|
||
afterEach(() => { | ||
ComponentMap.clear() | ||
return destroyEngine() | ||
}) | ||
|
||
describe('defineQuery', () => { | ||
it('should define a query with the given components', () => { | ||
const query = defineQuery([ComponentA, ComponentB]) | ||
assert.ok(query) | ||
let entities = query() | ||
assert.ok(entities) | ||
assert.strictEqual(entities.length, 0) // No entities yet | ||
|
||
const e1 = createEntity() | ||
const e2 = createEntity() | ||
setComponent(e1, ComponentA) | ||
setComponent(e1, ComponentB) | ||
setComponent(e2, ComponentA) | ||
setComponent(e2, ComponentB) | ||
setComponent(createEntity(), ComponentA) | ||
setComponent(createEntity(), ComponentB) | ||
|
||
entities = query() | ||
assert.strictEqual(entities.length, 2) | ||
assert.strictEqual(entities[0], e1) | ||
assert.strictEqual(entities[1], e2) | ||
assert.ok(hasComponent(entities[0], ComponentA)) | ||
assert.ok(hasComponent(entities[0], ComponentB)) | ||
}) | ||
}) | ||
|
||
describe('useQuery', () => { | ||
it('should return entities that match the query', () => { | ||
const e1 = createEntity() | ||
const e2 = createEntity() | ||
setComponent(e1, ComponentA) | ||
setComponent(e1, ComponentB) | ||
setComponent(e2, ComponentA) | ||
setComponent(e2, ComponentB) | ||
const { result } = renderHook(() => useQuery([ComponentA, ComponentB])) // return correct results the first time | ||
const entities = result.current | ||
assert.strictEqual(entities.length, 2) | ||
assert.strictEqual(entities[0], e1) | ||
assert.strictEqual(entities[1], e2) | ||
assert.ok(hasComponent(entities[0], ComponentA)) | ||
assert.ok(hasComponent(entities[0], ComponentB)) | ||
assert.ok(hasComponent(entities[1], ComponentA)) | ||
assert.ok(hasComponent(entities[1], ComponentB)) | ||
}) | ||
|
||
it('should update the entities when components change', () => { | ||
const e1 = createEntity() | ||
const e2 = createEntity() | ||
setComponent(e1, ComponentA) | ||
setComponent(e1, ComponentB) | ||
setComponent(e2, ComponentA) | ||
setComponent(e2, ComponentB) | ||
const { result, rerender } = renderHook(() => useQuery([ComponentA, ComponentB])) | ||
let entities = result.current | ||
assert.strictEqual(entities.length, 2) | ||
assert.strictEqual(entities[0], e1) | ||
assert.strictEqual(entities[1], e2) | ||
assert.ok(hasComponent(entities[0], ComponentA)) | ||
assert.ok(hasComponent(entities[0], ComponentB)) | ||
assert.ok(hasComponent(entities[1], ComponentA)) | ||
assert.ok(hasComponent(entities[1], ComponentB)) | ||
removeComponent(e1, ComponentB) | ||
rerender() | ||
entities = result.current | ||
assert.strictEqual(entities.length, 1) | ||
assert.strictEqual(entities[0], e2) | ||
assert.ok(hasComponent(entities[0], ComponentA)) | ||
assert.ok(hasComponent(entities[0], ComponentB)) | ||
}) | ||
}) | ||
}) |
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
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,79 @@ | ||
/* | ||
CPAL-1.0 License | ||
The contents of this file are subject to the Common Public Attribution License | ||
Version 1.0. (the "License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at | ||
https://github.com/EtherealEngine/etherealengine/blob/dev/LICENSE. | ||
The License is based on the Mozilla Public License Version 1.1, but Sections 14 | ||
and 15 have been added to cover use of software over a computer network and | ||
provide for limited attribution for the Original Developer. In addition, | ||
Exhibit A has been modified to be consistent with Exhibit B. | ||
Software distributed under the License is distributed on an "AS IS" basis, | ||
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the | ||
specific language governing rights and limitations under the License. | ||
The Original Code is Ethereal Engine. | ||
The Original Developer is the Initial Developer. The Initial Developer of the | ||
Original Code is the Ethereal Engine team. | ||
All portions of the code written by the Ethereal Engine team are Copyright © 2021-2023 | ||
Ethereal Engine. All Rights Reserved. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react' | ||
import assert from 'assert' | ||
import { useImmediateEffect } from './useImmediateEffect' | ||
|
||
describe('useImmediateEffect', () => { | ||
it('should call the effect function immediately', () => { | ||
let effectCalled = false | ||
const effect = () => { | ||
effectCalled = true | ||
} | ||
|
||
const { rerender } = renderHook((deps: number[]) => useImmediateEffect(effect, deps), { | ||
initialProps: [] | ||
}) | ||
|
||
rerender([]) | ||
|
||
assert(effectCalled) | ||
}) | ||
|
||
it('should call the cleanup function when dependencies change', () => { | ||
let cleanupCalled = false | ||
const effect = () => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
} | ||
|
||
const { rerender } = renderHook((deps: number[]) => useImmediateEffect(effect, deps), { | ||
initialProps: [] | ||
}) | ||
|
||
rerender([1, 2, 3]) | ||
|
||
assert(cleanupCalled) | ||
}) | ||
|
||
it('should not call the cleanup function when dependencies do not change', () => { | ||
let cleanupCalled = false | ||
const effect = () => { | ||
return () => { | ||
cleanupCalled = true | ||
} | ||
} | ||
|
||
const { rerender } = renderHook((deps: number[]) => useImmediateEffect(effect, deps), { | ||
initialProps: [1, 2, 3] | ||
}) | ||
|
||
rerender([1, 2, 3]) | ||
|
||
assert(!cleanupCalled) | ||
}) | ||
}) |
Oops, something went wrong.