mirror of https://github.com/sveltejs/svelte
feat: attachments `fromAction` utility (#15933)
* feat: attachments `fromAction` utility * fix: typing of the utility Co-authored-by: Aidan Bleser <117548273+ieedan@users.noreply.github.com> * simplify implementation - create action first, then only create update/destroy effects if necessary * add since * regenerate * remove FromAction interface * overload * fix: use typedef instead of exported interface * get rid of the arg0, arg1 stuff * oops * god i hate overloads * defer to the reference documentation * damn ur weird typescript * gah --------- Co-authored-by: Aidan Bleser <117548273+ieedan@users.noreply.github.com> Co-authored-by: Rich Harris <rich.harris@vercel.com>pull/15972/head
parent
22a0211cbb
commit
7183886a73
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'svelte': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
feat: attachments `fromAction` utility
|
@ -0,0 +1,116 @@
|
|||||||
|
import { ok, test } from '../../test';
|
||||||
|
import { flushSync } from 'svelte';
|
||||||
|
|
||||||
|
export default test({
|
||||||
|
async test({ assert, target, logs }) {
|
||||||
|
const [btn, btn2, btn3] = target.querySelectorAll('button');
|
||||||
|
|
||||||
|
// both logs on creation it will not log on change
|
||||||
|
assert.deepEqual(logs, ['create', 0, 'action', 'create', 0, 'attachment']);
|
||||||
|
|
||||||
|
// clicking the first button logs the right value
|
||||||
|
flushSync(() => {
|
||||||
|
btn?.click();
|
||||||
|
});
|
||||||
|
assert.deepEqual(logs, ['create', 0, 'action', 'create', 0, 'attachment', 0]);
|
||||||
|
|
||||||
|
// clicking the second button logs the right value
|
||||||
|
flushSync(() => {
|
||||||
|
btn2?.click();
|
||||||
|
});
|
||||||
|
assert.deepEqual(logs, ['create', 0, 'action', 'create', 0, 'attachment', 0, 0]);
|
||||||
|
|
||||||
|
// updating the arguments logs the update function for both
|
||||||
|
flushSync(() => {
|
||||||
|
btn3?.click();
|
||||||
|
});
|
||||||
|
assert.deepEqual(logs, [
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'action',
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'attachment',
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'action',
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'attachment'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// clicking the first button again shows the right value
|
||||||
|
flushSync(() => {
|
||||||
|
btn?.click();
|
||||||
|
});
|
||||||
|
assert.deepEqual(logs, [
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'action',
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'attachment',
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'action',
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'attachment',
|
||||||
|
1
|
||||||
|
]);
|
||||||
|
|
||||||
|
// clicking the second button again shows the right value
|
||||||
|
flushSync(() => {
|
||||||
|
btn2?.click();
|
||||||
|
});
|
||||||
|
assert.deepEqual(logs, [
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'action',
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'attachment',
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'action',
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'attachment',
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
]);
|
||||||
|
|
||||||
|
// unmounting logs the destroy function for both
|
||||||
|
flushSync(() => {
|
||||||
|
btn3?.click();
|
||||||
|
});
|
||||||
|
assert.deepEqual(logs, [
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'action',
|
||||||
|
'create',
|
||||||
|
0,
|
||||||
|
'attachment',
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'action',
|
||||||
|
'update',
|
||||||
|
1,
|
||||||
|
'attachment',
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
'destroy',
|
||||||
|
'action',
|
||||||
|
'destroy',
|
||||||
|
'attachment'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
@ -0,0 +1,37 @@
|
|||||||
|
<script>
|
||||||
|
import { fromAction } from 'svelte/attachments';
|
||||||
|
let { count = 0 } = $props();
|
||||||
|
|
||||||
|
function test(node, thing) {
|
||||||
|
const kind = node.dataset.kind;
|
||||||
|
console.log('create', thing, kind);
|
||||||
|
let t = thing;
|
||||||
|
const controller = new AbortController();
|
||||||
|
node.addEventListener(
|
||||||
|
'click',
|
||||||
|
() => {
|
||||||
|
console.log(t);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
signal: controller.signal
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
update(new_thing) {
|
||||||
|
console.log('update', new_thing, kind);
|
||||||
|
t = new_thing;
|
||||||
|
},
|
||||||
|
destroy() {
|
||||||
|
console.log('destroy', kind);
|
||||||
|
controller.abort();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if count < 2}
|
||||||
|
<button data-kind="action" use:test={count}></button>
|
||||||
|
<button data-kind="attachment" {@attach fromAction(test, ()=>count)}></button>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<button onclick={()=> count++}></button>
|
Loading…
Reference in new issue