mirror of https://github.com/sveltejs/svelte
parent
a5a0b49003
commit
3672e29e68
@ -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