Skip to content

Commit ea97fe2

Browse files
authored
chore: move legacy code (#10775)
* move event modifier code * move more legacy code * oops --------- Co-authored-by: Rich Harris <[email protected]>
1 parent e36a8d0 commit ea97fe2

File tree

5 files changed

+209
-207
lines changed

5 files changed

+209
-207
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @param {(event: Event, ...args: Array<unknown>) => void} fn
3+
* @returns {(event: Event, ...args: unknown[]) => void}
4+
*/
5+
export function trusted(fn) {
6+
return function (...args) {
7+
var event = /** @type {Event} */ (args[0]);
8+
if (event.isTrusted) {
9+
// @ts-ignore
10+
fn.apply(this, args);
11+
}
12+
};
13+
}
14+
15+
/**
16+
* @param {(event: Event, ...args: Array<unknown>) => void} fn
17+
* @returns {(event: Event, ...args: unknown[]) => void}
18+
*/
19+
export function self(fn) {
20+
return function (...args) {
21+
var event = /** @type {Event} */ (args[0]);
22+
// @ts-ignore
23+
if (event.target === this) {
24+
// @ts-ignore
25+
fn.apply(this, args);
26+
}
27+
};
28+
}
29+
30+
/**
31+
* @param {(event: Event, ...args: Array<unknown>) => void} fn
32+
* @returns {(event: Event, ...args: unknown[]) => void}
33+
*/
34+
export function stopPropagation(fn) {
35+
return function (...args) {
36+
var event = /** @type {Event} */ (args[0]);
37+
event.stopPropagation();
38+
// @ts-ignore
39+
return fn.apply(this, args);
40+
};
41+
}
42+
43+
/**
44+
* @param {(event: Event, ...args: Array<unknown>) => void} fn
45+
* @returns {(event: Event, ...args: unknown[]) => void}
46+
*/
47+
export function once(fn) {
48+
var ran = false;
49+
50+
return function (...args) {
51+
if (ran) return;
52+
ran = true;
53+
54+
// @ts-ignore
55+
return fn.apply(this, args);
56+
};
57+
}
58+
59+
/**
60+
* @param {(event: Event, ...args: Array<unknown>) => void} fn
61+
* @returns {(event: Event, ...args: unknown[]) => void}
62+
*/
63+
export function stopImmediatePropagation(fn) {
64+
return function (...args) {
65+
var event = /** @type {Event} */ (args[0]);
66+
event.stopImmediatePropagation();
67+
// @ts-ignore
68+
return fn.apply(this, args);
69+
};
70+
}
71+
72+
/**
73+
* @param {(event: Event, ...args: Array<unknown>) => void} fn
74+
* @returns {(event: Event, ...args: unknown[]) => void}
75+
*/
76+
export function preventDefault(fn) {
77+
return function (...args) {
78+
var event = /** @type {Event} */ (args[0]);
79+
event.preventDefault();
80+
// @ts-ignore
81+
return fn.apply(this, args);
82+
};
83+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { run } from '../../../common.js';
2+
import { pre_effect, user_effect } from '../../reactivity/effects.js';
3+
import { current_component_context, deep_read_state, get, untrack } from '../../runtime.js';
4+
5+
/**
6+
* Legacy-mode only: Call `onMount` callbacks and set up `beforeUpdate`/`afterUpdate` effects
7+
*/
8+
export function init() {
9+
const context = /** @type {import('#client').ComponentContext} */ (current_component_context);
10+
const callbacks = context.u;
11+
12+
if (!callbacks) return;
13+
14+
// beforeUpdate
15+
if (callbacks.b.length) {
16+
pre_effect(() => {
17+
observe_all(context);
18+
callbacks.b.forEach(run);
19+
});
20+
}
21+
22+
// onMount (must run before afterUpdate)
23+
user_effect(() => {
24+
const fns = untrack(() => callbacks.m.map(run));
25+
return () => {
26+
for (const fn of fns) {
27+
if (typeof fn === 'function') {
28+
fn();
29+
}
30+
}
31+
};
32+
});
33+
34+
// afterUpdate
35+
if (callbacks.a.length) {
36+
user_effect(() => {
37+
observe_all(context);
38+
callbacks.a.forEach(run);
39+
});
40+
}
41+
}
42+
43+
/**
44+
* Invoke the getter of all signals associated with a component
45+
* so they can be registered to the effect this function is called in.
46+
* @param {import('#client').ComponentContext} context
47+
*/
48+
function observe_all(context) {
49+
if (context.d) {
50+
for (const signal of context.d) get(signal);
51+
}
52+
53+
deep_read_state(context.s);
54+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { set, source } from '../../reactivity/sources.js';
2+
import { get } from '../../runtime.js';
3+
import { is_array } from '../../utils.js';
4+
5+
/**
6+
* Under some circumstances, imports may be reactive in legacy mode. In that case,
7+
* they should be using `reactive_import` as part of the transformation
8+
* @param {() => any} fn
9+
*/
10+
export function reactive_import(fn) {
11+
var s = source(0);
12+
13+
return function () {
14+
if (arguments.length === 1) {
15+
set(s, get(s) + 1);
16+
return arguments[0];
17+
} else {
18+
get(s);
19+
return fn();
20+
}
21+
};
22+
}
23+
24+
/**
25+
* @this {any}
26+
* @param {Record<string, unknown>} $$props
27+
* @param {Event} event
28+
* @returns {void}
29+
*/
30+
export function bubble_event($$props, event) {
31+
var events = /** @type {Record<string, Function[] | Function>} */ ($$props.$$events)?.[
32+
event.type
33+
];
34+
35+
var callbacks = is_array(events) ? events.slice() : events == null ? [] : [events];
36+
37+
for (var fn of callbacks) {
38+
// Preserve "this" context
39+
fn.call(this, event);
40+
}
41+
}
42+
43+
/**
44+
* Used to simulate `$on` on a component instance when `legacy.componentApi` is `true`
45+
* @param {Record<string, any>} $$props
46+
* @param {string} event_name
47+
* @param {Function} event_callback
48+
*/
49+
export function add_legacy_event_listener($$props, event_name, event_callback) {
50+
$$props.$$events ||= {};
51+
$$props.$$events[event_name] ||= [];
52+
$$props.$$events[event_name].push(event_callback);
53+
}
54+
55+
/**
56+
* Used to simulate `$set` on a component instance when `legacy.componentApi` is `true`.
57+
* Needs component accessors so that it can call the setter of the prop. Therefore doesn't
58+
* work for updating props in `$$props` or `$$restProps`.
59+
* @this {Record<string, any>}
60+
* @param {Record<string, any>} $$new_props
61+
*/
62+
export function update_legacy_props($$new_props) {
63+
for (var key in $$new_props) {
64+
if (key in this) {
65+
this[key] = $$new_props[key];
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)