koota 0.2.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/react/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import {
2
2
  $internal,
3
- cacheQuery,
4
- createWorld
5
- } from "../dist/chunk-ZHBGFPP2.js";
3
+ cacheQuery
4
+ } from "../dist/chunk-OAPTAVXT.js";
6
5
 
7
6
  // ../react/src/hooks/use-query.ts
8
7
  import { useEffect, useMemo, useState } from "react";
@@ -14,41 +13,48 @@ import { useContext } from "react";
14
13
  import { createContext } from "react";
15
14
  var WorldContext = createContext(null);
16
15
 
17
- // ../react/src/world/default-world.ts
18
- var defaultWorld = createWorld();
19
- var getDefaultWorld = () => defaultWorld;
20
-
21
16
  // ../react/src/world/use-world.ts
22
17
  function useWorld() {
23
- const world = useContext(WorldContext) ?? defaultWorld;
18
+ const world = useContext(WorldContext);
19
+ if (!world) {
20
+ throw new Error("Koota: useWorld must be used within a WorldProvider");
21
+ }
24
22
  return world;
25
23
  }
26
24
 
27
25
  // ../react/src/hooks/use-query.ts
28
26
  function useQuery(...parameters) {
29
27
  const world = useWorld();
30
- const [hash, initialVersion] = useMemo(() => {
28
+ const [version, setVersion] = useState(0);
29
+ const [hash, initialQueryVersion] = useMemo(() => {
31
30
  const hash2 = cacheQuery(...parameters);
32
31
  const query = world[$internal].queriesHashMap.get(hash2);
33
32
  return [hash2, query.version];
34
- }, [parameters]);
35
- const [entities, setEntities] = useState(() => world.query(hash));
33
+ }, [parameters, world]);
34
+ const [entities, setEntities] = useState(() => world.query(hash).sort());
36
35
  useEffect(() => {
37
36
  const unsubAdd = world.onAdd(parameters, () => {
38
- setEntities(world.query(hash));
37
+ setEntities(world.query(hash).sort());
39
38
  });
40
39
  const unsubRemove = world.onRemove(parameters, () => {
41
- setEntities(world.query(hash));
40
+ setEntities(world.query(hash).sort());
42
41
  });
43
42
  const query = world[$internal].queriesHashMap.get(hash);
44
- if (query.version !== initialVersion) {
45
- setEntities(world.query(hash));
43
+ if (query.version !== initialQueryVersion) {
44
+ setEntities(world.query(hash).sort());
46
45
  }
47
46
  return () => {
48
47
  unsubAdd();
49
48
  unsubRemove();
50
49
  };
51
- }, [world, hash]);
50
+ }, [world, hash, version]);
51
+ useEffect(() => {
52
+ const handler = () => setVersion((v) => v + 1);
53
+ world[$internal].resetSubscriptions.add(handler);
54
+ return () => {
55
+ world[$internal].resetSubscriptions.delete(handler);
56
+ };
57
+ }, [world]);
52
58
  return entities;
53
59
  }
54
60
 
@@ -148,7 +154,6 @@ function useTraitEffect(target, trait, callback) {
148
154
  }
149
155
  export {
150
156
  WorldProvider,
151
- getDefaultWorld,
152
157
  useActions,
153
158
  useQuery,
154
159
  useQueryFirst,