Skip to content

Commit

Permalink
Merge pull request #2101 from sveltejs/gh-2018
Browse files Browse the repository at this point in the history
Use initCustomEvent instead of new CustomEvent
Rich-Harris authored Feb 19, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents d31d857 + 7c9c8ce commit eb90755
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/internal/dom.js
Original file line number Diff line number Diff line change
@@ -250,3 +250,9 @@ export function addResizeListener(element, fn) {
export function toggleClass(element, name, toggle) {
element.classList[toggle ? 'add' : 'remove'](name);
}

export function custom_event(type, detail) {
const e = document.createEvent('CustomEvent');
e.initCustomEvent(type, false, false, detail);
return e;
}
4 changes: 3 additions & 1 deletion src/internal/lifecycle.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { custom_event } from './dom';

export let current_component;

export function set_current_component(component) {
@@ -34,7 +36,7 @@ export function createEventDispatcher() {
if (callbacks) {
// TODO are there situations where events could be dispatched
// in a server (non-DOM) environment?
const event = new window.CustomEvent(type, { detail });
const event = custom_event(type, detail);
callbacks.slice().forEach(fn => {
fn.call(component, event);
});
7 changes: 4 additions & 3 deletions src/internal/transitions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { identity as linear, noop, run_all } from './utils.js';
import { loop } from './loop.js';
import { create_rule, delete_rule } from './style_manager.js';
import { custom_event } from './dom.js';

let promise;

@@ -238,14 +239,14 @@ export function create_bidirectional_transition(node, fn, params, intro) {
if (b) tick(0, 1);

running_program = init(program, duration);
node.dispatchEvent(new window.CustomEvent(`${running_program.b ? 'intro' : 'outro'}start`));
node.dispatchEvent(custom_event(`${running_program.b ? 'intro' : 'outro'}start`));

loop(now => {
if (pending_program && now > pending_program.start) {
running_program = init(pending_program, duration);
pending_program = null;

node.dispatchEvent(new window.CustomEvent(`${running_program.b ? 'intro' : 'outro'}start`));
node.dispatchEvent(custom_event(`${running_program.b ? 'intro' : 'outro'}start`));

if (css) {
clear_animation();
@@ -256,7 +257,7 @@ export function create_bidirectional_transition(node, fn, params, intro) {
if (running_program) {
if (now >= running_program.end) {
tick(t = running_program.b, 1 - t);
node.dispatchEvent(new window.CustomEvent(`${running_program.b ? 'intro' : 'outro'}end`));
node.dispatchEvent(custom_event(`${running_program.b ? 'intro' : 'outro'}end`));

if (!pending_program) {
// we're done

0 comments on commit eb90755

Please sign in to comment.