Refactoring and Dividing to files
Currently, after extracting the current line the validation functions are executed one after the other. Like so:
It means that if someone were to add another feature he will need to add his validation function to this call sequence of validation functions.
This may lead to long sequence of function calls and also requires him to access our code directly.
So the goal is to allow other people to add new features to our extension in an easy and reuseable way.
We decided to make a new object (class) that holds a list of functions will be invoked one by one without the object knowing anything about the functions besides there signature.
If someone wants to add their validation function to the sequence all he needs is to know this new validation object.
This new object is named "validationObject" and it has 3 functions:
1. invokeValidationFunctions - this function goes over the functions in the list (" validateFunctionArray") and execute each one.
2. addValidateFunction - this function receives a function from the following signature: validatePotentialUrl(foundUrl: {url: string| undefined}) : void. AddValidateFunction pushes the input function to the list.
3.removeValidateFunction - this function receives a function from the same signature as before (2) and removes it from the list.
We used these stackOverflow questions: how to send a function as parameter, List of functions.
We decided to make a class for each format we currently support (image, css, youtube).
Each of these classes implements the IPreviewObject interface and also a validation function.
The first thing was to add a validation function to the interface so new features will have to implement it as well.
So now, each class implements a validation function, a markdown function and an html function.
Also, we made a constructor for each class which adds the validation function to the validateFunctionArray.
We hold a static IPreviewObject variable which contains the current format class.
All format classes are implementing the IPreviewObject interface so the relevent functions will be decided at run time according to the class that represents the current url format.
In addition, we defined a new static IPreviewObject dictionary which is initialized at the begining of the extension and create one instance of each format class.
If we want to add a new feature we will need to add one line to this function (in addition to creating a new format class. we will add a tutorial soon).
We also updated the currentPreviewObject to the relevent preview object in previewObjectList like so:
YouTube validation:
Image validation:
Css validation:
Lastly, we divided the classes into different files, we made a seperate file to the interface and a utils file.
Css file:
Image file:
YouTube file:
Extention file:
Utils file:
IPreviewObject file:
We published the updated extension to the vscode marketplace:
Comments
Post a Comment