Skip to content

Commit 9cd07af

Browse files
authored
Merge pull request #3250 from SnaveSutit/feature/improved-eventsystem-typing
✨ Improved EventSystem typing
2 parents ce2ceec + 585ea33 commit 9cd07af

File tree

3 files changed

+130
-122
lines changed

3 files changed

+130
-122
lines changed

js/api.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ export const Blockbench = {
332332
return this.flags[flag];
333333
},
334334
//Events
335-
dispatchEvent(event_name: EventName, data: any): any[] {
335+
dispatchEvent<T extends BlockbenchEventName, D extends BlockbenchEventMap[T]>(event_name: T, data: D): any[] {
336336
let list = this.events[event_name];
337337
let results: any[];
338338
if (list) {
@@ -349,16 +349,16 @@ export const Blockbench = {
349349
}
350350
return results;
351351
},
352-
on(event_name: EventName, cb) {
352+
on<T extends BlockbenchEventName, D extends BlockbenchEventMap[T]>(event_name: T, cb: (data: D) => any): void {
353353
return EventSystem.prototype.on.call(this, event_name, cb);
354354
},
355-
once(event_name: EventName, cb) {
355+
once<T extends BlockbenchEventName, D extends BlockbenchEventMap[T]>(event_name: T, cb: (data: D) => any): void {
356356
return EventSystem.prototype.once.call(this, event_name, cb);
357357
},
358-
addListener(event_name: EventName, cb) {
358+
addListener<T extends BlockbenchEventName, D extends BlockbenchEventMap[T]>(event_name: T, cb: (data: D) => any): void {
359359
return EventSystem.prototype.addListener.call(this, event_name, cb);
360360
},
361-
removeListener(event_name: EventName, cb) {
361+
removeListener<T extends BlockbenchEventName, D extends BlockbenchEventMap[T]>(event_name: T, cb: (data: D) => any): void {
362362
return EventSystem.prototype.removeListener.call(this, event_name, cb);
363363
},
364364
// Update
@@ -411,6 +411,12 @@ if (isApp) {
411411
if (Blockbench.platform.includes('win32') === true) window.osfs = '\\';
412412
}
413413

414+
declare global {
415+
interface window {
416+
Blockbench: typeof Blockbench
417+
}
418+
}
419+
414420
const global = {
415421
LastVersion,
416422
Blockbench,

types/custom/misc.d.ts

Lines changed: 116 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ declare class Deletable {
99
}
1010
type UUID = string
1111

12-
1312
declare global {
1413
const settings: typeof settings
1514
}
@@ -21,122 +20,125 @@ declare const isApp: boolean
2120

2221
declare const VuePrismEditor: Vue.Component
2322

24-
type EventName =
25-
| 'remove_animation'
26-
| 'display_animation_frame'
27-
| 'display_default_pose'
28-
| 'interpolate_keyframes'
29-
| 'before_closing'
30-
| 'create_session'
31-
| 'join_session'
32-
| 'quit_session'
33-
| 'send_session_data'
34-
| 'receive_session_data'
35-
| 'user_joins_session'
36-
| 'user_leaves_session'
37-
| 'process_chat_message'
38-
| 'loaded_plugin'
39-
| 'unloaded_plugin'
40-
| 'installed_plugin'
41-
| 'uninstalled_plugin'
42-
| 'update_settings'
43-
| 'update_project_settings'
44-
| 'save_project'
45-
| 'load_project'
46-
| 'new_project'
47-
| 'reset_project'
48-
| 'close_project'
49-
| 'saved_state_changed'
50-
| 'save_model_action'
51-
| 'add_cube'
52-
| 'add_mesh'
53-
| 'add_group'
54-
| 'add_texture_mesh'
55-
| 'add_armature'
56-
| 'add_armature_bone'
57-
| 'add_bounding_box'
58-
| 'group_elements'
59-
| 'update_selection'
60-
| 'compile_bedrock_animations'
61-
| 'load_animation'
62-
| 'load_animation_controller'
63-
| 'update_keyframe_selection'
64-
| 'select_all'
65-
| 'added_to_selection'
66-
| 'invert_selection'
67-
| 'canvas_select'
68-
| 'canvas_click'
69-
| 'change_texture_path'
70-
| 'add_texture'
71-
| 'generate_texture_template'
72-
| 'update_texture_selection'
73-
| 'init_edit'
74-
| 'finish_edit'
75-
| 'finished_edit'
76-
| 'undo'
77-
| 'redo'
78-
| 'load_undo_save'
79-
| 'create_undo_save'
80-
| 'drop_text'
81-
| 'paste_text'
82-
| 'change_color'
83-
| 'select_mode'
84-
| 'unselect_mode'
85-
| 'change_active_panel'
86-
| 'resize_window'
87-
| 'press_key'
88-
| 'select_format'
89-
| 'convert_format'
90-
| 'construct_format'
91-
| 'delete_format'
92-
| 'select_project'
93-
| 'unselect_project'
94-
| 'setup_project'
95-
| 'update_project_resolution'
96-
| 'merge_project'
97-
| 'display_model_stats'
98-
| 'update_view'
99-
| 'update_camera_position'
100-
| 'render_frame'
101-
| 'construct_model_loader'
102-
| 'delete_model_loader'
103-
| 'update_recent_project_data'
104-
| 'update_recent_project_thumbnail'
105-
| 'load_from_recent_project_data'
106-
| 'edit_animation_properties'
107-
| 'select_preview_scene'
108-
| 'unselect_preview_scene'
109-
| 'compile_bedrock_animation_controller_state'
110-
| 'select_animation_controller_state'
111-
| 'add_animation_controller_animation'
112-
| 'add_animation_controller_transition'
113-
| 'add_animation_controller_particle'
114-
| 'add_animation_controller_sound'
115-
| 'compile_bedrock_animation_controller'
116-
| 'add_animation_controller'
117-
| 'edit_animation_controller_properties'
118-
| 'timeline_play'
119-
| 'timeline_pause'
120-
| 'unselect_interface'
121-
| 'reset_layout'
122-
| 'update_pressed_modifier_keys'
123-
| 'open_bar_menu'
124-
| 'unselect_all'
125-
| 'quick_save_model'
126-
| 'save_editor_state'
127-
| 'load_editor_state'
128-
| 'select_no_project'
129-
| 'flip_node_name'
130-
| 'update_scene_shading'
131-
| 'edit_layer_properties'
132-
| 'select_texture'
133-
| 'compile_texture_mcmeta'
134-
| 'register_element_type'
135-
| 'edit_collection_properties'
23+
interface BlockbenchEventMap {
24+
remove_animation: any
25+
display_animation_frame: any
26+
display_default_pose: any
27+
interpolate_keyframes: any
28+
before_closing: any
29+
create_session: any
30+
join_session: any
31+
quit_session: any
32+
send_session_data: any
33+
receive_session_data: any
34+
user_joins_session: any
35+
user_leaves_session: any
36+
process_chat_message: any
37+
loaded_plugin: { plugin: BBPlugin }
38+
unloaded_plugin: { plugin: BBPlugin }
39+
installed_plugin: { plugin: BBPlugin }
40+
uninstalled_plugin: { plugin: BBPlugin }
41+
update_settings: any
42+
update_project_settings: Record<string, any>
43+
save_project: any
44+
load_project: any
45+
new_project: any
46+
reset_project: any
47+
close_project: any
48+
saved_state_changed: any
49+
save_model_action: any
50+
add_cube: any
51+
add_mesh: any
52+
add_group: any
53+
add_texture_mesh: any
54+
add_armature: any
55+
add_armature_bone: any
56+
add_bounding_box: any
57+
group_elements: any
58+
update_selection: any
59+
compile_bedrock_animations: any
60+
load_animation: any
61+
load_animation_controller: any
62+
update_keyframe_selection: any
63+
select_all: any
64+
added_to_selection: any
65+
invert_selection: any
66+
canvas_select: any
67+
canvas_click: any
68+
change_texture_path: any
69+
add_texture: any
70+
generate_texture_template: any
71+
update_texture_selection: any
72+
init_edit: any
73+
finish_edit: any
74+
finished_edit: any
75+
undo: { entry: UndoEntry }
76+
redo: { entry: UndoEntry }
77+
load_undo_save: any
78+
create_undo_save: any
79+
drop_text: { text: string }
80+
paste_text: { text: string }
81+
change_color: any
82+
select_mode: { mode: Mode }
83+
unselect_mode: { mode: Mode }
84+
change_active_panel: any
85+
resize_window: any
86+
press_key: any
87+
select_format: any
88+
convert_format: any
89+
construct_format: any
90+
delete_format: any
91+
select_project: { project: Project }
92+
unselect_project: { project: Project }
93+
setup_project: any
94+
update_project_resolution: any
95+
merge_project: any
96+
display_model_stats: any
97+
update_view: any
98+
update_camera_position: any
99+
render_frame: any
100+
construct_model_loader: any
101+
delete_model_loader: any
102+
update_recent_project_data: any
103+
update_recent_project_thumbnail: any
104+
load_from_recent_project_data: any
105+
edit_animation_properties: any
106+
select_preview_scene: any
107+
unselect_preview_scene: any
108+
compile_bedrock_animation_controller_state: any
109+
select_animation_controller_state: any
110+
add_animation_controller_animation: any
111+
add_animation_controller_transition: any
112+
add_animation_controller_particle: any
113+
add_animation_controller_sound: any
114+
compile_bedrock_animation_controller: any
115+
add_animation_controller: any
116+
edit_animation_controller_properties: any
117+
timeline_play: any
118+
timeline_pause: any
119+
unselect_interface: any
120+
reset_layout: any
121+
update_pressed_modifier_keys: any
122+
open_bar_menu: any
123+
unselect_all: any
124+
quick_save_model: any
125+
save_editor_state: any
126+
load_editor_state: any
127+
select_no_project: any
128+
flip_node_name: any
129+
update_scene_shading: any
130+
edit_layer_properties: any
131+
select_texture: any
132+
compile_texture_mcmeta: any
133+
register_element_type: any
134+
edit_collection_properties: any
135+
}
136+
137+
type BlockbenchEventName = keyof BlockbenchEventMap
136138

137139
type IconString = string
138140

139-
declare const osfs: '\\' | '/';
141+
declare const osfs: '\\' | '/'
140142

141143
declare function updateSelection(): void
142144

types/custom/validator.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare namespace Validator {
1111
* Run the validator
1212
* @param trigger ID of the Blockbench event that triggered the call
1313
*/
14-
function validate(trigger?: EventName): void
14+
function validate<T extends BlockbenchEventName>(trigger?: T): void
1515
/**
1616
* Opens the Validator dialog
1717
*/
@@ -20,7 +20,7 @@ declare namespace Validator {
2020
/**
2121
* Cached trigger IDs
2222
*/
23-
const triggers: EventName[]
23+
const triggers: BlockbenchEventName[]
2424
/**
2525
* Update the cached triggers list
2626
*/
@@ -35,7 +35,7 @@ interface ValidatorCheckOptions {
3535
/**
3636
* Names of events that automatically trigger this check
3737
*/
38-
update_triggers?: EventName[]
38+
update_triggers?: BlockbenchEventName[]
3939
condition?: ConditionResolvable
4040
}
4141
interface WarningOrError {

0 commit comments

Comments
 (0)