Skip to content

Commit 5ced3ef

Browse files
committed
automatic subscription on signal passed as hole
1 parent 989c0ca commit 5ced3ef

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

src/dom/rabbit.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { children } from './ish.js';
88
import { effect } from './signals.js';
99
import { isArray } from '../utils.js';
1010
import { PersistentFragment, diffFragment, nodes } from './persistent-fragment.js';
11-
import { ARRAY, COMMENT, COMPONENT, EVENT, KEY, REF, ref } from './update.js';
11+
import { ARRAY, COMMENT, COMPONENT, EVENT, KEY, REF, SIGNAL, ref } from './update.js';
1212

1313
import { _get as getDirect, _set as setDirect } from './direct.js';
1414
import { Signal, _get as getSignal, _set as setSignal } from './signals.js';
@@ -146,7 +146,7 @@ export class Hole {
146146
let commit = true;
147147
//@ts-ignore
148148
if (DEBUG && (type & ARRAY) && !isArray(value)) throw errors.invalid_interpolation(this.t[3], value);
149-
if (!direct && (type & COMMENT)) {
149+
if (!direct && (type & COMMENT) && !(type & SIGNAL)) {
150150
if (type & ARRAY) {
151151
commit = false;
152152
//@ts-ignore
@@ -172,7 +172,7 @@ export class Hole {
172172
}
173173
//@ts-ignore
174174
changes[length] = [type, update, value, node];
175-
if (direct && type === COMMENT) node.remove();
175+
if (direct && (type & COMMENT)) node.remove();
176176
}
177177
}
178178
if (refs) {
@@ -236,15 +236,7 @@ export class Hole {
236236
}
237237
else {
238238
let change = value;
239-
if (type === COMMENT) {
240-
if (prev instanceof Hole) {
241-
if (DEBUG && !(value instanceof Hole)) throw errors.invalid_interpolation([], value);
242-
value = getHole(prev, /** @type {Hole} */(value));
243-
//@ts-ignore
244-
change = value.n;
245-
}
246-
}
247-
else if (type & ARRAY) {
239+
if (type & ARRAY) {
248240
if (DEBUG && !isArray(value)) throw errors.invalid_interpolation([], value);
249241
if (type & COMMENT) {
250242
//@ts-ignore
@@ -268,6 +260,20 @@ export class Hole {
268260
//@ts-ignore
269261
else if ((type & EVENT) && (value[0] === prev[0])) continue;
270262
}
263+
else if (type & COMMENT) {
264+
if (type & SIGNAL) {
265+
if (value === prev) {
266+
update(entry[3], change);
267+
continue;
268+
}
269+
}
270+
else if (prev instanceof Hole) {
271+
if (DEBUG && !(value instanceof Hole)) throw errors.invalid_interpolation([], value);
272+
value = getHole(prev, /** @type {Hole} */(value));
273+
//@ts-ignore
274+
change = value.n;
275+
}
276+
}
271277
if (value !== prev) {
272278
entry[2] = value;
273279
update(entry[3], change);

src/dom/update.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const TEXT = 1 << 11;
3030
export const TOGGLE = 1 << 12;
3131
export const UNSAFE = 1 << 13;
3232
export const REF = 1 << 14;
33+
export const SIGNAL = 1 << 15;
3334

3435
// COMPONENT flags
3536
const COMPONENT_DIRECT = COMPONENT | DIRECT;
@@ -138,10 +139,9 @@ export const update = (node, type, path, name, hint) => {
138139
case TEMPLATE_COMPONENT: return [path, hint, COMPONENT];
139140
case TEMPLATE_COMMENT: {
140141
if (isArray(hint)) return [path, comment_array, COMMENT_ARRAY];
141-
return hint instanceof Unsafe ?
142-
[path, comment_unsafe(node.xml), UNSAFE] :
143-
[path, hint instanceof Signal ? comment_signal : comment_hole, COMMENT]
144-
;
142+
if (hint instanceof Unsafe) return [path, comment_unsafe(node.xml), UNSAFE];
143+
if (hint instanceof Signal) return [path, comment_signal, COMMENT | SIGNAL];
144+
return [path, comment_hole, COMMENT];
145145
}
146146
case TEMPLATE_TEXT: return [path, directFor('textContent'), TEXT];
147147
case TEMPLATE_ATTRIBUTE: {

types/dom/update.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const TEXT: number;
1313
export const TOGGLE: number;
1414
export const UNSAFE: number;
1515
export const REF: number;
16+
export const SIGNAL: number;
1617
export const fragment: (content: string, xml?: boolean) => DocumentFragment;
1718
export const ref: unique symbol;
1819
export function isKeyed(): boolean;

0 commit comments

Comments
 (0)