Skip to content

Conversation

@faizanu94
Copy link
Contributor

This Pull Request addresses #8 by introducing a new ReturnFormat option to the CSSStyleObserver. This enhancement allows users to choose between receiving only the current values of observed CSS properties or detailed information that includes previous values and change statuses

Changes

  • Introduced the ReturnFormat enum with options VALUE_ONLY (default) and OBJECT
  • Updated the CSSStyleObserver class to process observed properties based on the selected ReturnFormat
  • Updated the README.md to reflect the new ReturnFormat option and provide usage examples

Copy link
Owner

@bramus bramus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the extended return format I’d like to retain the original object structure instead of changing to an array. So it’d be something like this:

    {
        "--my-variable": {
            "propertyName": "--my-variable",
            "value": "1.0",
            "previousValue": "0.0",
            "changed": true
        },
        "display": {
            "propertyName": "display",
            "value": "block",
            "previousValue": "block",
            "changed": false
        },
    }

That way, it’s easy to switch between return formats. If authors want to easily loop, they can always pass this result into Object.entries/Object.values.

Does that make sense?

@faizanu94
Copy link
Contributor Author

Absolutely, that makes sense and would simplify switching between return formats - I have made the changes accordingly!

I did notice that the propertyName is duplicated both as the key and within each CSSPropertyInfo object. To avoid redundancy, perhaps we could omit the propertyName field inside the object since the key already provides that information. This would make the structure cleaner:

{
    "--my-variable": {
        "value": "1.0",
        "previousValue": "0.0",
        "changed": true
    },
    "display": {
        "value": "block",
        "previousValue": "block",
        "changed": false
    }
}

your thoughts on this?

@bramus
Copy link
Owner

bramus commented Oct 3, 2024

Yeah, makes sense. Devs should use Object.entries() when looping the results.

faizanu94 and others added 6 commits October 6, 2024 06:33
Co-authored-by: Bramus <bramus@bram.us>
Co-authored-by: Bramus <bramus@bram.us>
Co-authored-by: Bramus <bramus@bram.us>
Co-authored-by: Bramus <bramus@bram.us>
Co-authored-by: Bramus <bramus@bram.us>
@faizanu94 faizanu94 requested a review from bramus October 6, 2024 02:59
Copy link
Owner

@bramus bramus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates!

There is still some unnecessary dependency chain between the functions here, namely _returnFormatHandlers calling _processObservedVariables, which I think can be avoided.

Ideally these two functions would be decoupled, so that they are standalone:

  • _processObservedVariables to only return a list of changes
  • _returnFormatHandlers to only return a formatting function

The _handleUpdate function can then tie them together, by passing the list of changes into the handler, after which it passed those formattedChanges into the _callback.

src/index.ts Outdated
const hasChanged = currentValue !== previousValue;

if (this._notificationMode === NotificationMode.ALL || hasChanged) {
changes.push(processChange(propertyName, currentValue, previousValue, hasChanged));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor this so that it does not call processChange here, but that it only stores the change object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored as requested

src/index.ts Outdated
/**
* Handlers for return formats
*/
private _returnFormatHandlers: Record<ReturnFormat, (computedStyle: CSSStyleDeclaration) => CSSDeclarations | { [key: string]: CSSPropertyInfo }> = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, these would only return a processing function, not actually call _processObservedVariables

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored as requested

faizanu94 and others added 2 commits October 7, 2024 09:33
Co-authored-by: Bramus <bramus@bram.us>
@faizanu94 faizanu94 requested a review from bramus October 7, 2024 05:50
@bramus bramus merged commit bc89590 into bramus:main Oct 7, 2024
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

Successfully merging this pull request may close these issues.

2 participants