Skip to content

refactor(ScrollToTarget): implement debounced update for active target handling #2404

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

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
⚡ refactor(ScrollToTarget): implement debounced update for active tar…
…get handling
  • Loading branch information
capdiem committed May 13, 2025
commit e3d6cb8d3508dc0d35b88f8cdcaeca553e3405e9
15 changes: 11 additions & 4 deletions src/Masa.Blazor.JS/src/components/scroll-to-target/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import debounceIt from "just-debounce-it";

import { formatToStandardOptions, IntersectionObserverOptions } from "../../mixins/intersect";

class ScrollToTargetJSInterop {
activeStack: string[] = [];
handle: DotNet.DotNetObject;
options?: IntersectionObserverInit | null;
debouncedUpdate: () => void;

constructor(
handle: DotNet.DotNetObject,
options?: IntersectionObserverOptions
) {
this.handle = handle;
this.options = formatToStandardOptions(options);

this.debouncedUpdate = debounceIt(async () => {
await this.handle.invokeMethodAsync(
"UpdateActiveTarget",
this.activeStack[this.activeStack.length - 1]
);
}, 16);
}

observe(id: string) {
Expand All @@ -36,10 +46,7 @@ class ScrollToTargetJSInterop {
this.activeStack.splice(this.activeStack.indexOf(id), 1);
}

await this.handle.invokeMethodAsync(
"UpdateActiveTarget",
this.activeStack[this.activeStack.length - 1]
);
this.debouncedUpdate();
},
this.options
);
Expand Down
10 changes: 6 additions & 4 deletions src/Masa.Blazor/Components/ScrollToTarget/MScrollToTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ public class MScrollToTarget : ComponentBase, IAsyncDisposable

[Parameter] public bool DisableIntersectAfterTriggering { get; set; }

private bool _task;

private TargetContext _targetContext = new();
private List<string> _activeStack = new();
private bool _hasRendered;
private List<Action>? _tasks;
private string? lastTarget;

private DotNetObjectReference<ScrollToTargetJSInteropHandle>? _jsInteropHandle;
private ScrollToTargetJSObjectReference? _scrollToTargetJSObjectReference;
Expand Down Expand Up @@ -178,10 +177,13 @@ private async ValueTask ObserverAsync(string target)
_scrollToTargetJSObjectReference?.ObserveAsync(target);
}

private string lastTarget;

internal async ValueTask UpdateActiveTarget(string target)
{
if (_targetContext.ActiveTarget == target)
Copy link
Preview

Copilot AI May 13, 2025

Choose a reason for hiding this comment

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

[nitpick] The method now compares _targetContext.ActiveTarget with the parameter value before updating a separate lastTarget field. To improve maintainability, consider using a single source of truth for the active target state—either consistently update _targetContext or replace its use with lastTarget.

Copilot uses AI. Check for mistakes.

{
return;
}

lastTarget = target;

if (_disableIntersecting)
Expand Down

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Masa.Blazor/wwwroot/js/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"components/overlay/scroll-strategy.js": "components/overlay/scroll-strategy-dc362cab.js",
"components/page-stack/index.js": "components/page-stack/index-c7271ebd.js",
"components/page-stack/touch.js": "components/page-stack/touch-64592f9e.js",
"components/scroll-to-target/index.js": "components/scroll-to-target/index-0c459122.js",
"components/scroll-to-target/index.js": "components/scroll-to-target/index-d22e7344.js",
"components/transition/index.js": "components/transition/index-339f8848.js",
"components/window/touch.js": "components/window/touch-f601a168.js",
"mixins/activatable/index.js": "mixins/activatable/index-82cb7376.js",
Expand Down
Loading