Skip to content

Commit

Permalink
[gem] Remove GemElement.attributeChangedCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Jun 17, 2024
1 parent 2c8f7d5 commit 82fb9ec
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 24 deletions.
1 change: 0 additions & 1 deletion packages/gem-devtools/src/scripts/get-gem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export const getSelectedGem = function (data: PanelStore, gemElementSymbols: str
const attrs: Set<string> = $0.attributes ? new Set([...$0.attributes].map((attr) => attr.nodeName)) : new Set();
const elementMethod = new Set([
'connectedCallback',
'attributeChangedCallback',
'adoptedCallback',
'disconnectedCallback',
'setAttribute',
Expand Down
1 change: 0 additions & 1 deletion packages/gem/docs/en/001-guide/011-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ SSR is currently not available. Although the [Declarative Shadow DOM](https://gi
The following methods will cause `GemElement` to not work properly when overridden:

- `GemElement.connectedCallback`
- `GemElement.attributeChangedCallback`
- `GemElement.disconnectedCallback`
- `GemElement.internals`

Expand Down
1 change: 0 additions & 1 deletion packages/gem/docs/zh/001-guide/011-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Gem 只保证支持较新版的 Chrome/Firefox/Safari。
下面的方法被覆盖时将导致 `GemElement` 不能正常工作:

- `GemElement.connectedCallback`
- `GemElement.attributeChangedCallback`
- `GemElement.disconnectedCallback`
- `GemElement.internals`

Expand Down
4 changes: 2 additions & 2 deletions packages/gem/src/helper/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UpdateHistoryParams } from '../lib/history';
import { html, TemplateResult } from '../lib/element';
import { html, TemplateResult, updateSymbol } from '../lib/element';
import { GemError } from '../lib/utils';
import type { RouteTrigger } from '../elements/base/route';

Expand Down Expand Up @@ -210,7 +210,7 @@ export class I18n<T = Record<string, Msg>> implements RouteTrigger {
const temp: Element[] = [document.documentElement];
while (!!temp.length) {
const element = temp.pop()!;
if ('attributeChangedCallback' in element) (element as any).attributeChangedCallback();
(element as any)[updateSymbol]?.();
if (element.shadowRoot?.firstElementChild) temp.push(element.shadowRoot.firstElementChild);
if (element.firstElementChild) temp.push(element.firstElementChild);
if (element.nextElementSibling) temp.push(element.nextElementSibling);
Expand Down
29 changes: 10 additions & 19 deletions packages/gem/src/lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type EffectItem<T> = {
preCallback?: () => void;
};

const updateSymbol = Symbol('update');
export const updateSymbol = Symbol('update');

export interface GemElementOptions extends Partial<ShadowRootInit> {
isLight?: boolean;
Expand Down Expand Up @@ -118,15 +118,12 @@ export abstract class GemElement<T = Record<string, unknown>> extends HTMLElemen
#memoList?: EffectItem<any>[];
#unmountCallback?: any;

[updateSymbol]() {
if (this.#isMounted) {
addMicrotask(this.#update);
}
}

constructor(options: GemElementOptions = {}) {
super();

// expose private Methods
Reflect.set(this, updateSymbol, this.#asyncUpdate);

this.#isAsync = options.isAsync;
this.#renderRoot = options.isLight
? this
Expand Down Expand Up @@ -357,6 +354,12 @@ export abstract class GemElement<T = Record<string, unknown>> extends HTMLElemen
}
};

#asyncUpdate = () => {
if (this.#isMounted) {
addMicrotask(this.#update);
}
};

/**
* @helper
* async
Expand All @@ -378,18 +381,6 @@ export abstract class GemElement<T = Record<string, unknown>> extends HTMLElemen
*/
unmounted?(): void | Promise<void>;

/**
* @private
* @final
* use `effect`
*/
attributeChangedCallback() {
if (this.#isMounted) {
addMicrotask(this.#update);
}
return GemElement.#final;
}

#disconnectStore?: (() => void)[];
#connectedCallback = () => {
if (this.#isAppendReason) {
Expand Down

0 comments on commit 82fb9ec

Please sign in to comment.