koota 0.0.4 → 0.0.5

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/README.md CHANGED
@@ -449,4 +449,20 @@ const id = entity.id()
449
449
 
450
450
  // Destroys the entity making its number no longer valid
451
451
  entity.destroy()
452
+ ```
453
+
454
+ ### React
455
+
456
+ `useEntityRef` is a safe way to spawn an entity per React primitive and add traits. It is usually used for adding traits that capture the ref to the entity. The entity will be stable for the lifetime of the React component, except in cases like HMR.
457
+
458
+ ```js
459
+ const Ref = trait({ value: null! })
460
+
461
+ function Rocket() {
462
+ const entityRef = useEntityRef((node, entity) => {
463
+ entity.add(Ref({ value: node }))
464
+ })
465
+
466
+ return <div ref={entityRef}>🚀</div>
467
+ }
452
468
  ```
package/dist/index.cjs CHANGED
@@ -297,10 +297,12 @@ function addTrait(world, entity, ...traits) {
297
297
  }
298
298
  }
299
299
  }
300
- const defaults = data.schema;
301
- for (const key in defaults) {
302
- if (typeof defaults[key] === "function") {
303
- defaults[key] = defaults[key]();
300
+ const defaults = {};
301
+ for (const key in data.schema) {
302
+ if (typeof data.schema[key] === "function") {
303
+ defaults[key] = data.schema[key]();
304
+ } else {
305
+ defaults[key] = data.schema[key];
304
306
  }
305
307
  }
306
308
  entity.set(trait2, { ...defaults, ...params }, false);
@@ -350,7 +352,6 @@ function hasTrait(world, entity, trait2) {
350
352
  }
351
353
  function getStore(world, trait2) {
352
354
  const ctx = world[$internal];
353
- if (!ctx.traitData.has(trait2)) registerTrait(world, trait2);
354
355
  const data = ctx.traitData.get(trait2);
355
356
  const store = data.store;
356
357
  return store;
package/dist/index.js CHANGED
@@ -258,10 +258,12 @@ function addTrait(world, entity, ...traits) {
258
258
  }
259
259
  }
260
260
  }
261
- const defaults = data.schema;
262
- for (const key in defaults) {
263
- if (typeof defaults[key] === "function") {
264
- defaults[key] = defaults[key]();
261
+ const defaults = {};
262
+ for (const key in data.schema) {
263
+ if (typeof data.schema[key] === "function") {
264
+ defaults[key] = data.schema[key]();
265
+ } else {
266
+ defaults[key] = data.schema[key];
265
267
  }
266
268
  }
267
269
  entity.set(trait2, { ...defaults, ...params }, false);
@@ -311,7 +313,6 @@ function hasTrait(world, entity, trait2) {
311
313
  }
312
314
  function getStore(world, trait2) {
313
315
  const ctx = world[$internal];
314
- if (!ctx.traitData.has(trait2)) registerTrait(world, trait2);
315
316
  const data = ctx.traitData.get(trait2);
316
317
  const store = data.store;
317
318
  return store;
package/dist/react.cjs CHANGED
@@ -22,13 +22,14 @@ var react_exports = {};
22
22
  __export(react_exports, {
23
23
  WorldProvider: () => WorldProvider,
24
24
  createActions: () => createActions,
25
+ useEntityRef: () => useEntityRef,
25
26
  useObserve: () => useObserve,
26
27
  useQuery: () => useQuery,
27
28
  useWorld: () => useWorld
28
29
  });
29
30
  module.exports = __toCommonJS(react_exports);
30
31
 
31
- // ../react/src/query/use-query.ts
32
+ // ../react/src/hooks/use-query.ts
32
33
  var import_react3 = require("react");
33
34
 
34
35
  // ../react/src/world/use-world.ts
@@ -45,7 +46,7 @@ function useWorld() {
45
46
  return world;
46
47
  }
47
48
 
48
- // ../react/src/query/use-query.ts
49
+ // ../react/src/hooks/use-query.ts
49
50
  function useQuery(...parameters) {
50
51
  const world = useWorld();
51
52
  const [entities, setEntities] = (0, import_react3.useState)([]);
@@ -83,7 +84,7 @@ function createActions(actionSet) {
83
84
  );
84
85
  }
85
86
 
86
- // ../react/src/trait/use-observe.tsx
87
+ // ../react/src/hooks/use-observe.ts
87
88
  var import_react4 = require("react");
88
89
  function useObserve(entity, trait) {
89
90
  const world = useWorld();
@@ -101,10 +102,30 @@ function useObserve(entity, trait) {
101
102
  }, [entity, trait]);
102
103
  return value;
103
104
  }
105
+
106
+ // ../react/src/hooks/use-entity-ref.ts
107
+ var import_react5 = require("react");
108
+ function useEntityRef(callback) {
109
+ const world = useWorld();
110
+ const entityRef = (0, import_react5.useRef)(null);
111
+ return (0, import_react5.useCallback)(
112
+ (node) => {
113
+ if (node) {
114
+ if (entityRef.current) entityRef.current.destroy();
115
+ entityRef.current = world.spawn();
116
+ callback(node, entityRef.current);
117
+ } else if (entityRef.current) {
118
+ entityRef.current.destroy();
119
+ }
120
+ },
121
+ [world]
122
+ );
123
+ }
104
124
  // Annotate the CommonJS export names for ESM import in node:
105
125
  0 && (module.exports = {
106
126
  WorldProvider,
107
127
  createActions,
128
+ useEntityRef,
108
129
  useObserve,
109
130
  useQuery,
110
131
  useWorld
package/dist/react.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { Q as QueryParameter, W as World, T as Trait, j as Entity, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.cjs';
1
+ import { Q as QueryParameter, j as Entity, W as World, T as Trait, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.cjs';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
 
4
- declare function useQuery(...parameters: QueryParameter[]): number[];
4
+ declare function useQuery(...parameters: QueryParameter[]): Entity[];
5
5
 
6
6
  declare function WorldProvider({ children, world }: {
7
7
  children: React.ReactNode;
@@ -16,4 +16,6 @@ declare function createActions<T extends Record<string, (...args: any[]) => any>
16
16
 
17
17
  declare function useObserve<T extends Trait>(entity: Entity, trait: T): TraitInstanceFromSchema<ExtractSchema<T>> | undefined;
18
18
 
19
- export { WorldProvider, createActions, useObserve, useQuery, useWorld };
19
+ declare function useEntityRef<T = any>(callback: (node: T, entity: Entity) => void): (node: T) => void;
20
+
21
+ export { WorldProvider, createActions, useEntityRef, useObserve, useQuery, useWorld };
package/dist/react.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Q as QueryParameter, W as World, T as Trait, j as Entity, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.js';
1
+ import { Q as QueryParameter, j as Entity, W as World, T as Trait, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.js';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
 
4
- declare function useQuery(...parameters: QueryParameter[]): number[];
4
+ declare function useQuery(...parameters: QueryParameter[]): Entity[];
5
5
 
6
6
  declare function WorldProvider({ children, world }: {
7
7
  children: React.ReactNode;
@@ -16,4 +16,6 @@ declare function createActions<T extends Record<string, (...args: any[]) => any>
16
16
 
17
17
  declare function useObserve<T extends Trait>(entity: Entity, trait: T): TraitInstanceFromSchema<ExtractSchema<T>> | undefined;
18
18
 
19
- export { WorldProvider, createActions, useObserve, useQuery, useWorld };
19
+ declare function useEntityRef<T = any>(callback: (node: T, entity: Entity) => void): (node: T) => void;
20
+
21
+ export { WorldProvider, createActions, useEntityRef, useObserve, useQuery, useWorld };
package/dist/react.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./chunk-AQ5VUG5P.js";
2
2
 
3
- // ../react/src/query/use-query.ts
3
+ // ../react/src/hooks/use-query.ts
4
4
  import { useEffect, useState } from "react";
5
5
 
6
6
  // ../react/src/world/use-world.ts
@@ -17,7 +17,7 @@ function useWorld() {
17
17
  return world;
18
18
  }
19
19
 
20
- // ../react/src/query/use-query.ts
20
+ // ../react/src/hooks/use-query.ts
21
21
  function useQuery(...parameters) {
22
22
  const world = useWorld();
23
23
  const [entities, setEntities] = useState([]);
@@ -55,7 +55,7 @@ function createActions(actionSet) {
55
55
  );
56
56
  }
57
57
 
58
- // ../react/src/trait/use-observe.tsx
58
+ // ../react/src/hooks/use-observe.ts
59
59
  import { useEffect as useEffect2, useState as useState2 } from "react";
60
60
  function useObserve(entity, trait) {
61
61
  const world = useWorld();
@@ -73,9 +73,29 @@ function useObserve(entity, trait) {
73
73
  }, [entity, trait]);
74
74
  return value;
75
75
  }
76
+
77
+ // ../react/src/hooks/use-entity-ref.ts
78
+ import { useCallback, useRef } from "react";
79
+ function useEntityRef(callback) {
80
+ const world = useWorld();
81
+ const entityRef = useRef(null);
82
+ return useCallback(
83
+ (node) => {
84
+ if (node) {
85
+ if (entityRef.current) entityRef.current.destroy();
86
+ entityRef.current = world.spawn();
87
+ callback(node, entityRef.current);
88
+ } else if (entityRef.current) {
89
+ entityRef.current.destroy();
90
+ }
91
+ },
92
+ [world]
93
+ );
94
+ }
76
95
  export {
77
96
  WorldProvider,
78
97
  createActions,
98
+ useEntityRef,
79
99
  useObserve,
80
100
  useQuery,
81
101
  useWorld
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koota",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "🌎 Performant data-oriented state management for React and TypeScript.",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -38,9 +38,9 @@
38
38
  "react": "^18.3.1",
39
39
  "react-dom": "^18.3.1",
40
40
  "tsup": "^8.3.0",
41
+ "@koota/core": "0.0.1",
41
42
  "@koota/react": "0.0.1",
42
- "tsconfig": "0.1.0",
43
- "@koota/core": "0.0.1"
43
+ "tsconfig": "0.1.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "@react-three/fiber": "^8.17.5",
package/react/index.cjs CHANGED
@@ -22,13 +22,14 @@ var react_exports = {};
22
22
  __export(react_exports, {
23
23
  WorldProvider: () => WorldProvider,
24
24
  createActions: () => createActions,
25
+ useEntityRef: () => useEntityRef,
25
26
  useObserve: () => useObserve,
26
27
  useQuery: () => useQuery,
27
28
  useWorld: () => useWorld
28
29
  });
29
30
  module.exports = __toCommonJS(react_exports);
30
31
 
31
- // ../react/src/query/use-query.ts
32
+ // ../react/src/hooks/use-query.ts
32
33
  var import_react3 = require("react");
33
34
 
34
35
  // ../react/src/world/use-world.ts
@@ -45,7 +46,7 @@ function useWorld() {
45
46
  return world;
46
47
  }
47
48
 
48
- // ../react/src/query/use-query.ts
49
+ // ../react/src/hooks/use-query.ts
49
50
  function useQuery(...parameters) {
50
51
  const world = useWorld();
51
52
  const [entities, setEntities] = (0, import_react3.useState)([]);
@@ -83,7 +84,7 @@ function createActions(actionSet) {
83
84
  );
84
85
  }
85
86
 
86
- // ../react/src/trait/use-observe.tsx
87
+ // ../react/src/hooks/use-observe.ts
87
88
  var import_react4 = require("react");
88
89
  function useObserve(entity, trait) {
89
90
  const world = useWorld();
@@ -101,10 +102,30 @@ function useObserve(entity, trait) {
101
102
  }, [entity, trait]);
102
103
  return value;
103
104
  }
105
+
106
+ // ../react/src/hooks/use-entity-ref.ts
107
+ var import_react5 = require("react");
108
+ function useEntityRef(callback) {
109
+ const world = useWorld();
110
+ const entityRef = (0, import_react5.useRef)(null);
111
+ return (0, import_react5.useCallback)(
112
+ (node) => {
113
+ if (node) {
114
+ if (entityRef.current) entityRef.current.destroy();
115
+ entityRef.current = world.spawn();
116
+ callback(node, entityRef.current);
117
+ } else if (entityRef.current) {
118
+ entityRef.current.destroy();
119
+ }
120
+ },
121
+ [world]
122
+ );
123
+ }
104
124
  // Annotate the CommonJS export names for ESM import in node:
105
125
  0 && (module.exports = {
106
126
  WorldProvider,
107
127
  createActions,
128
+ useEntityRef,
108
129
  useObserve,
109
130
  useQuery,
110
131
  useWorld
package/react/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { Q as QueryParameter, W as World, T as Trait, j as Entity, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.cjs';
1
+ import { Q as QueryParameter, j as Entity, W as World, T as Trait, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.cjs';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
 
4
- declare function useQuery(...parameters: QueryParameter[]): number[];
4
+ declare function useQuery(...parameters: QueryParameter[]): Entity[];
5
5
 
6
6
  declare function WorldProvider({ children, world }: {
7
7
  children: React.ReactNode;
@@ -16,4 +16,6 @@ declare function createActions<T extends Record<string, (...args: any[]) => any>
16
16
 
17
17
  declare function useObserve<T extends Trait>(entity: Entity, trait: T): TraitInstanceFromSchema<ExtractSchema<T>> | undefined;
18
18
 
19
- export { WorldProvider, createActions, useObserve, useQuery, useWorld };
19
+ declare function useEntityRef<T = any>(callback: (node: T, entity: Entity) => void): (node: T) => void;
20
+
21
+ export { WorldProvider, createActions, useEntityRef, useObserve, useQuery, useWorld };
package/react/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Q as QueryParameter, W as World, T as Trait, j as Entity, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.js';
1
+ import { Q as QueryParameter, j as Entity, W as World, T as Trait, d as TraitInstanceFromSchema, E as ExtractSchema } from './world-BRz2NI5_.js';
2
2
  import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
 
4
- declare function useQuery(...parameters: QueryParameter[]): number[];
4
+ declare function useQuery(...parameters: QueryParameter[]): Entity[];
5
5
 
6
6
  declare function WorldProvider({ children, world }: {
7
7
  children: React.ReactNode;
@@ -16,4 +16,6 @@ declare function createActions<T extends Record<string, (...args: any[]) => any>
16
16
 
17
17
  declare function useObserve<T extends Trait>(entity: Entity, trait: T): TraitInstanceFromSchema<ExtractSchema<T>> | undefined;
18
18
 
19
- export { WorldProvider, createActions, useObserve, useQuery, useWorld };
19
+ declare function useEntityRef<T = any>(callback: (node: T, entity: Entity) => void): (node: T) => void;
20
+
21
+ export { WorldProvider, createActions, useEntityRef, useObserve, useQuery, useWorld };
package/react/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import "./chunk-AQ5VUG5P.js";
2
2
 
3
- // ../react/src/query/use-query.ts
3
+ // ../react/src/hooks/use-query.ts
4
4
  import { useEffect, useState } from "react";
5
5
 
6
6
  // ../react/src/world/use-world.ts
@@ -17,7 +17,7 @@ function useWorld() {
17
17
  return world;
18
18
  }
19
19
 
20
- // ../react/src/query/use-query.ts
20
+ // ../react/src/hooks/use-query.ts
21
21
  function useQuery(...parameters) {
22
22
  const world = useWorld();
23
23
  const [entities, setEntities] = useState([]);
@@ -55,7 +55,7 @@ function createActions(actionSet) {
55
55
  );
56
56
  }
57
57
 
58
- // ../react/src/trait/use-observe.tsx
58
+ // ../react/src/hooks/use-observe.ts
59
59
  import { useEffect as useEffect2, useState as useState2 } from "react";
60
60
  function useObserve(entity, trait) {
61
61
  const world = useWorld();
@@ -73,9 +73,29 @@ function useObserve(entity, trait) {
73
73
  }, [entity, trait]);
74
74
  return value;
75
75
  }
76
+
77
+ // ../react/src/hooks/use-entity-ref.ts
78
+ import { useCallback, useRef } from "react";
79
+ function useEntityRef(callback) {
80
+ const world = useWorld();
81
+ const entityRef = useRef(null);
82
+ return useCallback(
83
+ (node) => {
84
+ if (node) {
85
+ if (entityRef.current) entityRef.current.destroy();
86
+ entityRef.current = world.spawn();
87
+ callback(node, entityRef.current);
88
+ } else if (entityRef.current) {
89
+ entityRef.current.destroy();
90
+ }
91
+ },
92
+ [world]
93
+ );
94
+ }
76
95
  export {
77
96
  WorldProvider,
78
97
  createActions,
98
+ useEntityRef,
79
99
  useObserve,
80
100
  useQuery,
81
101
  useWorld