Don't sync dev tools service data between tabs (#10980)

This commit is contained in:
Bram Kragten 2022-01-24 15:43:28 +01:00 committed by GitHub
parent b0b953bfac
commit 648c02e622
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 10 deletions

View File

@ -5,7 +5,10 @@ import type { ClassElement } from "../../types";
type Callback = (oldValue: any, newValue: any) => void;
class Storage {
constructor() {
constructor(subscribe = true) {
if (!subscribe) {
return;
}
window.addEventListener("storage", (ev: StorageEvent) => {
if (ev.key && this.hasKey(ev.key)) {
this._storage[ev.key] = ev.newValue
@ -80,15 +83,18 @@ class Storage {
}
}
const storage = new Storage();
const subscribeStorage = new Storage();
export const LocalStorage =
(
storageKey?: string,
property?: boolean,
subscribe = true,
propertyOptions?: PropertyDeclaration
): any =>
(clsElement: ClassElement) => {
const storage = subscribe ? subscribeStorage : new Storage(false);
const key = String(clsElement.key);
storageKey = storageKey || String(clsElement.key);
const initVal = clsElement.initializer
@ -97,7 +103,7 @@ export const LocalStorage =
storage.addFromStorage(storageKey);
const subscribe = (el: ReactiveElement): UnsubscribeFunc =>
const subscribeChanges = (el: ReactiveElement): UnsubscribeFunc =>
storage.subscribeChanges(storageKey!, (oldValue) => {
el.requestUpdate(clsElement.key, oldValue);
});
@ -131,17 +137,19 @@ export const LocalStorage =
configurable: true,
},
finisher(cls: typeof ReactiveElement) {
if (property) {
if (property && subscribe) {
const connectedCallback = cls.prototype.connectedCallback;
const disconnectedCallback = cls.prototype.disconnectedCallback;
cls.prototype.connectedCallback = function () {
connectedCallback.call(this);
this[`__unbsubLocalStorage${key}`] = subscribe(this);
this[`__unbsubLocalStorage${key}`] = subscribeChanges(this);
};
cls.prototype.disconnectedCallback = function () {
disconnectedCallback.call(this);
this[`__unbsubLocalStorage${key}`]();
};
}
if (property) {
cls.createProperty(clsElement.key, {
noAccessor: true,
...propertyOptions,

View File

@ -31,9 +31,9 @@ export class DialogTryTts extends LitElement {
@query("#message") private _messageInput?: PaperTextareaElement;
@LocalStorage("cloudTtsTryMessage") private _message!: string;
@LocalStorage("cloudTtsTryMessage", false, false) private _message!: string;
@LocalStorage("cloudTtsTryTarget") private _target!: string;
@LocalStorage("cloudTtsTryTarget", false, false) private _target!: string;
public showDialog(params: TryTtsDialogParams) {
this._params = params;

View File

@ -38,10 +38,10 @@ class HaPanelDevService extends LitElement {
@state() private _uiAvailable = true;
@LocalStorage("panel-dev-service-state-service-data", true)
@LocalStorage("panel-dev-service-state-service-data", true, false)
private _serviceData?: ServiceAction = { service: "", target: {}, data: {} };
@LocalStorage("panel-dev-service-state-yaml-mode", true)
@LocalStorage("panel-dev-service-state-yaml-mode", true, false)
private _yamlMode = false;
@query("ha-yaml-editor") private _yamlEditor?: HaYamlEditor;

View File

@ -38,7 +38,7 @@ class PanelMediaBrowser extends LitElement {
},
];
@LocalStorage("mediaBrowseEntityId")
@LocalStorage("mediaBrowseEntityId", true, false)
private _entityId = BROWSER_PLAYER;
protected render(): TemplateResult {