@@ -13,50 +13,36 @@ import {
1313function onLayerUpdated(
1414 event : VoxelLayerHookEvent
1515): void {
16- // Send information over the network
17- console .log (event );
16+ // Narrow on `action` to get a fully-typed `metadata`.
17+ if (event .action === " voxel-set" ) {
18+ console .log (event .metadata .position , event .metadata .blockId );
19+ }
1820}
1921
2022const vr = new VoxelRenderer ({
2123 onLayerUpdated ,
2224});
2325```
2426
25- ## Types
26-
27- ``` ts
28- export type VoxelLayerHookAction =
29- // Voxel-layer actions
30- | " added" // a voxel layer was created
31- | " removed" // a voxel layer was deleted
32- | " updated" // layer properties (visibility, …) changed
33- | " offset-updated" // layer world offset changed
34- | " voxel-set" // a voxel was placed in a layer
35- | " voxel-removed" // a voxel was removed from a layer
36- | " reordered" // layer render order changed
37- // Object-layer actions
38- | " object-layer-added" // a new object layer was created
39- | " object-layer-removed" // an object layer was deleted
40- | " object-layer-updated" // object layer properties (e.g. visibility) changed
41- | " object-added" // an object was added to an object layer
42- | " object-removed" // an object was removed from an object layer
43- | " object-updated" ; // an object's properties were patched
44-
45- // Describes a change related to a layer.
46- export interface VoxelLayerHookEvent {
47- // The name of the affected layer.
48- layerName: string ;
49-
50- // The action that occurred on the layer.
51- action: VoxelLayerHookAction ;
52-
53- // Additional data related to the action.
54- // Use `unknown` to encourage callers
55- // to validate the payload before use.
56- metadata: Record <string , unknown >;
57- }
58-
59- export type VoxelLayerHookListener = (
60- event : VoxelLayerHookEvent
61- ) => void ;
62- ```
27+ ## Event reference
28+
29+ ` VoxelLayerHookEvent ` is a discriminated union keyed on ` action ` . Narrowing on ` action `
30+ gives you a precise ` metadata ` type with no casting required.
31+
32+ | ` action ` | ` metadata ` shape |
33+ | ---| ---|
34+ | ` "added" ` | ` { options: VoxelLayerConfigurableOptions } ` |
35+ | ` "removed" ` | ` {} ` |
36+ | ` "updated" ` | ` { options: Partial<VoxelLayerConfigurableOptions> } ` |
37+ | ` "offset-updated" ` | ` { offset: VoxelCoord } ` or ` { delta: VoxelCoord } ` |
38+ | ` "voxel-set" ` | ` { position, blockId, rotation, flipX, flipZ, flipY } ` |
39+ | ` "voxel-removed" ` | ` { position: Vector3Like } ` |
40+ | ` "reordered" ` | ` { direction: "up" \| "down" } ` |
41+ | ` "object-layer-added" ` | ` {} ` |
42+ | ` "object-layer-removed" ` | ` {} ` |
43+ | ` "object-layer-updated" ` | ` { patch: { visible?: boolean } } ` |
44+ | ` "object-added" ` | ` { objectId: string } ` |
45+ | ` "object-removed" ` | ` { objectId: string } ` |
46+ | ` "object-updated" ` | ` { objectId: string; patch: Partial<VoxelObjectJSON> } ` |
47+
48+ ` VoxelLayerHookAction ` is a convenience alias for ` VoxelLayerHookEvent["action"] ` .
0 commit comments