Skip to content

Commit

Permalink
feat: cancel an expired raf
Browse files Browse the repository at this point in the history
  • Loading branch information
zjxxxxxxxxx committed Sep 22, 2023
1 parent 67274fd commit 7f6990e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-hairs-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@open-editor/client': patch
---

cancel an expired raf
21 changes: 15 additions & 6 deletions packages/client/src/elements/defineOverlayElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,25 @@ export function defineOverlayElement() {
this.#update_RAF();
};

#last_RAF?: number;
#update_RAF = () => {
requestAnimationFrame(() => {
const styles = this.#activeElement
? getComputedStyles(this.#activeElement)
: emptyComputedStyles;
this.#updateStyles(styles);
this.#tooltip.update(this.#activeElement, styles.posttion);
if (this.#last_RAF) {
cancelAnimationFrame(this.#last_RAF);
}
this.#last_RAF = requestAnimationFrame(() => {
this.#last_RAF = undefined;
this.#update();
});
};

#update() {
const styles = this.#activeElement
? getComputedStyles(this.#activeElement)
: emptyComputedStyles;
this.#updateStyles(styles);
this.#tooltip.update(this.#activeElement, styles.posttion);
}

#updateStyles(styles: Record<string, ComputedStyle>) {
applyStyle(this.#posttion, {
width: CSS_util.px(styles.posttion.width),
Expand Down
18 changes: 9 additions & 9 deletions packages/client/src/resolve/createReactResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import type { ElementSourceMeta } from './resolveSource';
import { isValidFileName } from './util';

export interface ReactResolverOptions<T = any> {
isValid(target?: T | null): boolean;
getOwner(target?: T | null): T | null | undefined;
getSource(target?: T | null): any;
getName(target?: T | null): string | undefined;
isValid(current?: T | null): boolean;
getOwner(current?: T | null): T | null | undefined;
getSource(current?: T | null): any;
getName(current?: T | null): string | undefined;
}

export function createReactResolver<T = any>(options: ReactResolverOptions<T>) {
const { isValid, getOwner, getSource, getName } = options;

return function reactResolver(
target: T | null | undefined,
current: T | null | undefined,
tree: Partial<ElementSourceMeta>[],
deep: boolean,
) {
while (target) {
let owner = getOwner(target);
while (current) {
let owner = getOwner(current);

const source = getSource(target);
const source = getSource(current);
if (isValidFileName(source?.fileName)) {
while (!isValid(owner)) {
if (!owner) return;
Expand All @@ -36,7 +36,7 @@ export function createReactResolver<T = any>(options: ReactResolverOptions<T>) {
if (!deep) return;
}

target = owner;
current = owner;
}
};
}
2 changes: 1 addition & 1 deletion packages/client/src/resolve/createVueResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface VueResolverOptions<T = any> {
isValid(instance: T): boolean;
isValidNext(instance: T): boolean;
getNext(instance: T): T | null | undefined;
getVueSource(instance: T): string;
getVueSource(instance: T): string | undefined;
getFile(instance: T): string;
getName(instance: T): string | undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/resolve/framework/react15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ function createResolver() {
}
return false;
},
getOwner: (target) => target?._currentElement._owner,
getSource: (target) => target?._currentElement._source,
getOwner: (instance) => instance?._currentElement._owner,
getSource: (instance) => instance?._currentElement._source,
getName(owner) {
if (owner) {
const component = isFunc(owner._currentElement.type)
Expand Down
8 changes: 4 additions & 4 deletions packages/client/src/resolve/framework/react18.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function createResolver() {
}
return false;
},
getOwner: (target) => target?._debugOwner,
getSource: (target) => target?._debugSource,
getOwner: (fiber) => fiber?._debugOwner,
getSource: (fiber) => fiber?._debugSource,
getName(owner) {
if (owner) {
const component = isFunc(owner.type)
Expand All @@ -37,9 +37,9 @@ export function fiberResolver(
}

export function resolveReact18(
debug: ResolveDebug<Fiber>,
{ value: fiber }: ResolveDebug<Fiber>,
tree: Partial<ElementSourceMeta>[],
deep = false,
) {
fiberResolver(debug.value, tree, deep);
fiberResolver(fiber, tree, deep);
}
8 changes: 5 additions & 3 deletions packages/client/src/resolve/framework/vue2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ let resolver: ReturnType<typeof createVueResolver<any>>;
function createResolver() {
resolver = createVueResolver({
isValid: (instance) => Boolean(instance?.$vnode),
isValidNext: (instance) => Boolean(instance.$parent.$vnode),
isValidNext: (instance) => Boolean(instance.$parent?.$vnode),
getNext: (instance) => instance.$parent,
getVueSource: (instance) =>
<string>instance.$vnode.componentInstance?.$props?.__source,
getVueSource(instance) {
const { $props } = instance.$vnode.componentInstance;
return $props?.__source;
},
getFile(instance) {
const { Ctor } = instance.$vnode.componentOptions;
return Ctor?.__file ?? Ctor.options?.__file;
Expand Down

0 comments on commit 7f6990e

Please sign in to comment.