Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider data model immutability (and one-step construction) #23

Closed
travisleithead opened this issue Aug 19, 2021 · 4 comments
Closed

Comments

@travisleithead
Copy link
Member

I think it would be much easier to use the API if we can pass the whole model to the FormattedText constructor as an array instead of setting each run stylemap on separate statements. Is there also potential perf gain with that idea?

@nhelfman

@travisleithead
Copy link
Member Author

@nhelfman could you provide a bit of example code that demonstrates this?

@nhelfman
Copy link
Contributor

nhelfman commented Aug 19, 2021

For example, instead of:

let text = new FormattedText();
text.textruns.push (new FormattedTextRun({ text: "hello" });
text.textruns.push (new FormattedTextRun(" world");

text.textruns[1].tstyleMap.set("text-decoration", "underline");

It could be declared as:

let text = new FormattedText([
    { text: "hello" },
    { text: " world", style: { "text-decoration": "underline" }
  ]
);

Then, if we define FormattedText to be immutable it can make code using it much safer. Obviously, the downside here is increased GC in case it has to be recreated frequently with minor changes, but we should first identify such use cases.

@travisleithead
Copy link
Member Author

Your second example is almost fully supported by the current data model (see the 3rd constructor overload in the WebIDL):

  constructor(sequence<FormattedTextRunInit> textRunInit);

however, it expects a single string with the style declarations--there is some internal discussion about whether that should be used (e.g., requires the CSS parser to run on the string). Your approach with a record type of <css name string/ css value string> seems better.

The comment about making the data model immutable is more interesting. I'd been always thinking about FormattedText as a mutable data structure (like DOM nodes) that produces an immutable metrics--however if both the data model and associated metrics are immutable, then cross-referencing between them is no problem.

Mutability seems convenient if you use the FormattedText as the source data model. If you don't, and the formatted text is only the output of some other processing code (which seems very likely), then perhaps just re-creating it each time makes sense. I'd like to continue to explore this idea. If other folks have use cases where this makes sense, please share!

@travisleithead travisleithead changed the title Consider one-step construction Consider data model immutability (and one-step construction) Aug 19, 2021
@travisleithead
Copy link
Member Author

Closing this as PR #39 does away with the intermediary "data model" and now depends on application logic to host their own data model, which must be arranged correctly to a call to format to engage the API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants