You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
We support serializing events to JSON so that they can be sent over the wire and run in another instance of Blockly.
The current implementation has the deserialization method (fromJson) specified as an instance method on the event class. This requires constructing an instance of the event class, before you can actually deserialize it. Which in turn requires that every event support an empty constructor. Which in turn requires that every property of every event be nullable (nothing can be required at construction time).
Having every property of a class be nullable is not good TypeScript. It means you have to add lots of unnecessary non-null assertions or checks, which clutter up your main logic.
Describe the solution you'd like
I would like to refactor the events system to support a staticfromJson method, rather than an instance one. This would remove the need for an empty constructor, and nullable properties.
The Blockly.Event.fromJson function would be changed to check if the static method exists. If it does, then it would be used. If it doesn't, then we would fall back to the empty constructor + instance method strategy. This makes the refactor backwards compatible.
The detection logic would look something like this (as recommended by Christopher):
if (Object.getOwnPropertyDescriptors(C).m && typeof C.m === 'function')) {
// Use static method
} else {
// Fall back to empty constructor + instance method
}
Note that the above code doesn't mix quoted and dot accessors, so this is compatible with closure compiler.
In this proposal, existing events would maintain:
Their empty constructors
Their fromJson instance methods
The nullability of their properties.
Existing events would gain:
A fromJson static method
And new events (including the new procedure events) would have:
A fromJson static method
No empty constructors
No fromJson instance methods
No nullable properties
Describe alternatives you've considered
N/A
Additional context
N/A
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
We support serializing events to JSON so that they can be sent over the wire and run in another instance of Blockly.
The current implementation has the deserialization method (
fromJson
) specified as an instance method on the event class. This requires constructing an instance of the event class, before you can actually deserialize it. Which in turn requires that every event support an empty constructor. Which in turn requires that every property of every event be nullable (nothing can be required at construction time).Having every property of a class be nullable is not good TypeScript. It means you have to add lots of unnecessary non-null assertions or checks, which clutter up your main logic.
Describe the solution you'd like
I would like to refactor the events system to support a static
fromJson
method, rather than an instance one. This would remove the need for an empty constructor, and nullable properties.The
Blockly.Event.fromJson
function would be changed to check if the static method exists. If it does, then it would be used. If it doesn't, then we would fall back to the empty constructor + instance method strategy. This makes the refactor backwards compatible.The detection logic would look something like this (as recommended by Christopher):
Note that the above code doesn't mix quoted and dot accessors, so this is compatible with closure compiler.
In this proposal, existing events would maintain:
fromJson
instance methodsExisting events would gain:
fromJson
static methodAnd new events (including the new procedure events) would have:
fromJson
static methodfromJson
instance methodsDescribe alternatives you've considered
N/A
Additional context
N/A
The text was updated successfully, but these errors were encountered: