refactor(js): remove ds:page:update event

This commit is contained in:
Paul Chavard 2022-04-21 18:51:03 +02:00
parent b585808924
commit 62dca1c7b0
5 changed files with 31 additions and 47 deletions

View file

@ -0,0 +1,25 @@
import { Controller } from '@hotwired/stimulus';
import { debounce } from '@utils';
export type Detail = Record<string, unknown>;
export class ApplicationController extends Controller {
#debounced = new Map<() => void, () => void>();
protected debounce(fn: () => void, interval: number): void {
let debounced = this.#debounced.get(fn);
if (!debounced) {
debounced = debounce(fn.bind(this), interval);
this.#debounced.set(fn, debounced);
}
debounced();
}
protected globalDispatch(type: string, detail: Detail): void {
this.dispatch(type, {
detail,
prefix: '',
target: document.documentElement
});
}
}

View file

@ -1,16 +1,12 @@
import { Controller } from '@hotwired/stimulus';
import { debounce } from '@utils';
import { ApplicationController } from './application_controller';
type Detail = Record<string, unknown>;
export class GeoAreaController extends Controller {
export class GeoAreaController extends ApplicationController {
static values = {
id: String,
description: String
id: Number
};
static targets = ['description'];
declare readonly idValue: string;
declare readonly idValue: number;
declare readonly descriptionTarget: HTMLInputElement;
onFocus() {
@ -32,22 +28,4 @@ export class GeoAreaController extends Controller {
properties: { description: this.descriptionTarget.value.trim() }
});
}
#debounced = new Map<() => void, () => void>();
private debounce(fn: () => void, interval: number): void {
let debounced = this.#debounced.get(fn);
if (!debounced) {
debounced = debounce(fn.bind(this), interval);
this.#debounced.set(fn, debounced);
}
debounced();
}
private globalDispatch(type: string, detail: Detail): void {
this.dispatch(type, {
detail,
prefix: '',
target: document.documentElement
});
}
}

View file

@ -1,10 +1,9 @@
import { Controller } from '@hotwired/stimulus';
import invariant from 'tiny-invariant';
import { z } from 'zod';
type Detail = Record<string, unknown>;
import { ApplicationController, Detail } from './application_controller';
export class TurboEventController extends Controller {
export class TurboEventController extends ApplicationController {
static values = {
type: String,
detail: Object
@ -17,14 +16,6 @@ export class TurboEventController extends Controller {
this.globalDispatch(this.typeValue, this.detailValue);
this.element.remove();
}
private globalDispatch(type: string, detail: Detail): void {
this.dispatch(type, {
detail,
prefix: '',
target: document.documentElement
});
}
}
const MutationAction = z.enum(['show', 'hide', 'focus']);

View file

@ -5,7 +5,6 @@ import 'whatwg-fetch'; // window.fetch polyfill
import { Application } from '@hotwired/stimulus';
import { Turbo } from '@hotwired/turbo-rails';
import '../shared/page-update-event';
import '../shared/activestorage/ujs';
import '../shared/remote-poller';
import '../shared/safari-11-file-xhr-workaround';

View file

@ -1,9 +0,0 @@
import { fire } from '@utils';
addEventListener('DOMContentLoaded', function () {
fire(document, 'ds:page:update');
});
addEventListener('ajax:success', function () {
fire(document, 'ds:page:update');
});