Skip to content

Conversation

@junsikshim
Copy link
Collaborator

Summary
CellArray was a wrapper(by extending Array) for Cell[] which provided additional methods such as filterCells(). However, it had conflicting signatures compared to the normal array that resulted in TypeScript warnings which were hard to avoid.
So instead, I removed the CellArray class, and added filterCells() methods into the Array prototype. This way, migrating to maxGraph is easier, since it is no longer necessary to convert [cell] to new CellArray(cell).

Description for the changelog
Fixes CellArray related warnings. #96

Other info
In #96, there's one more type of warning regarding GraphLayout's traverse() method. Currently, HierarchicalLayout and SwimlaneLayout both inherit from that method, but with different signatures. It is not possible to fix them easily, so I'll leave it as is.

@junsikshim junsikshim requested review from mcyph and tbouffard August 13, 2022 09:09
@mcyph
Copy link
Collaborator

mcyph commented Aug 13, 2022

I originally added the CellArray class to consolidate those methods into one place and make it so that operations involving multiple cells would be performed on the object itself. I didn't like having new CellArray([]) in so many places even at the time, though basically went for it just because the alternatives also had disadvantages.

Generally I've have tried to avoid adding methods to prototypes because of the side effects e.g. when going for (let i in o) for arrays:

let o = [];
Array.prototype.foo = function() {}
for (let k in o) {console.log(k);} // -> outputs `foo`

will output foo as one of the keys. Although I don't do it myself I've sometimes seen other people's code use [] instead of {} to define an associative object e.g. let o = []; o.property = 'value';.

I'm wondering whether it might be an idea to move those methods into a utility object e.g. cellArrayFns or cellArrayOps to avoid these kinds of bugs at the same time as removing the need for the CellArray class, as think they could be quite tricky bugs to track down.

@junsikshim
Copy link
Collaborator Author

Yeah, I agree it's an anti-pattern. I thought about hiding the methods with defineProperty(), but apparently it doesn't work with multiple argument setter functions.
If this were a brand new project, I'd probably go for a new data structure something like 'CellCollection' without extending Array. After all, inheritance can't be free from potential method shadowing.

As @mcyph mentioned, I think moving the methods to utility functions is a better idea. These methods are not used that frequently, so migration wouldn't be too overwhelming.

@tbouffard
Copy link
Member

Hi guys, I am also ok to move the methods to utility functions.

@junsikshim
Copy link
Collaborator Author

I've created a file named cellArrayUtils.ts inside util directory and transferred all the CellArray related functions.
Also updated the examples as well, but let me know if something doesn't work.

@junsikshim junsikshim merged commit 02ea6f1 into development Sep 19, 2022
@junsikshim junsikshim deleted the cellarray branch September 19, 2022 04:24
@tbouffard tbouffard added the bug Something isn't working label Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants