mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-03 18:39:19 +00:00 
			
		
		
		
	Hook typing improvements (#236)
	
		
			
	
		
	
	
		
	
		
			Some checks are pending
		
		
	
	
	
				
					
				
			
		
			Some checks are pending
		
		
	
	
* Fix hook callback typings * Update docs * Add specialization * Simplify overloads * Remove generic
This commit is contained in:
		
							parent
							
								
									6ce796e7fd
								
							
						
					
					
						commit
						35b5f04a7c
					
				
					 2 changed files with 31 additions and 15 deletions
				
			
		| 
						 | 
					@ -132,26 +132,26 @@ Component data generally need to adhere to a specific interface, and sometimes r
 | 
				
			||||||
::: code-group
 | 
					::: code-group
 | 
				
			||||||
```luau [luau]
 | 
					```luau [luau]
 | 
				
			||||||
local Transform = world:component()
 | 
					local Transform = world:component()
 | 
				
			||||||
world:set(Transform, OnAdd, function(entity)
 | 
					world:set(Transform, OnAdd, function(entity, id, data)
 | 
				
			||||||
    -- A transform component has been added to an entity
 | 
					    -- A transform component `id` has been added with `data` to `entity`
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
world:set(Transform, OnRemove, function(entity)
 | 
					world:set(Transform, OnRemove, function(entity, id)
 | 
				
			||||||
    -- A transform component has been removed from the entity
 | 
					    -- A transform component `id` has been removed from `entity`
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
world:set(Transform, OnChange, function(entity, value)
 | 
					world:set(Transform, OnChange, function(entity, id, data)
 | 
				
			||||||
    -- A transform component has been changed to value on the entity
 | 
					    -- A transform component `id` has been changed to `data` on `entity`
 | 
				
			||||||
end)
 | 
					end)
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
```typescript [typescript]
 | 
					```typescript [typescript]
 | 
				
			||||||
const Transform = world.component();
 | 
					const Transform = world.component();
 | 
				
			||||||
world.set(Transform, OnAdd, (entity) => {
 | 
					world.set(Transform, OnAdd, (entity, id, data) => {
 | 
				
			||||||
	// A transform component has been added to an entity
 | 
						// A transform component `id` has been added with `data` to `entity`
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
world.set(Transform, OnRemove, (entity) => {
 | 
					world.set(Transform, OnRemove, (entity, id) => {
 | 
				
			||||||
	// A transform component has been removed from the entity
 | 
						// A transform component `id` has been removed from `entity`
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
world.set(Transform, OnChange, (entity, value) => {
 | 
					world.set(Transform, OnChange, (entity, id, data) => {
 | 
				
			||||||
	// A transform component has been changed to value on the entity
 | 
						// A transform component `id` has been changed to `data` on `entity`
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
:::
 | 
					:::
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										22
									
								
								jecs.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										22
									
								
								jecs.d.ts
									
									
									
									
										vendored
									
									
								
							| 
						 | 
					@ -151,6 +151,15 @@ export class World {
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	add<C>(entity: Entity, component: undefined extends InferComponent<C> ? C : Id<undefined>): void;
 | 
						add<C>(entity: Entity, component: undefined extends InferComponent<C> ? C : Id<undefined>): void;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Installs a hook on the given component.
 | 
				
			||||||
 | 
						 * @param component The target component.
 | 
				
			||||||
 | 
						 * @param hook The hook to install.
 | 
				
			||||||
 | 
						 * @param value The hook callback.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						set<T>(component: Entity<T>, hook: StatefulHook, value: (e: Entity<T>, id: Id<T>, data: T) => void): void;
 | 
				
			||||||
 | 
						set<T>(component: Entity<T>, hook: StatelessHook, value: (e: Entity<T>, id: Id<T>) => void): void;
 | 
				
			||||||
 | 
						
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Assigns a value to a component on the given entity.
 | 
						 * Assigns a value to a component on the given entity.
 | 
				
			||||||
	 * @param entity The target entity.
 | 
						 * @param entity The target entity.
 | 
				
			||||||
| 
						 | 
					@ -280,9 +289,16 @@ export function pair_first<P, O>(world: World, p: Pair<P, O>): Entity<P>;
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export function pair_second<P, O>(world: World, p: Pair<P, O>): Entity<O>;
 | 
					export function pair_second<P, O>(world: World, p: Pair<P, O>): Entity<O>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export declare const OnAdd: Entity<(e: Entity) => void>;
 | 
					type StatefulHook = Entity<<T>(e: Entity<T>, id: Id<T>, data: T) => void> & {
 | 
				
			||||||
export declare const OnRemove: Entity<(e: Entity) => void>;
 | 
						readonly __nominal_StatefulHook: unique symbol,
 | 
				
			||||||
export declare const OnChange: Entity<(e: Entity, value: unknown) => void>;
 | 
					}
 | 
				
			||||||
 | 
					type StatelessHook = Entity<<T>(e: Entity<T>, id: Id<T>) => void> & {
 | 
				
			||||||
 | 
						readonly __nominal_StatelessHook: unique symbol,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export declare const OnAdd: StatefulHook;
 | 
				
			||||||
 | 
					export declare const OnRemove: StatelessHook;
 | 
				
			||||||
 | 
					export declare const OnChange: StatefulHook;
 | 
				
			||||||
export declare const ChildOf: Tag;
 | 
					export declare const ChildOf: Tag;
 | 
				
			||||||
export declare const Wildcard: Entity;
 | 
					export declare const Wildcard: Entity;
 | 
				
			||||||
export declare const w: Entity;
 | 
					export declare const w: Entity;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue