Skip to content

Commit 905556b

Browse files
committed
feedback
1 parent defa646 commit 905556b

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { get } from '../internal/client/runtime.js';
2-
import { set, source } from '../internal/client/reactivity/sources.js';
2+
import { source } from '../internal/client/reactivity/sources.js';
33
import { effect_tracking } from '../internal/client/reactivity/effects.js';
44
import { createStartStopNotifier } from './start-stop-notifier.js';
5+
import { on } from '../events/index.js';
6+
import { increment } from './utils.js';
57

68
/**
79
* Creates a media query and provides a `current` property that reflects whether or not it matches.
810
*/
911
export class MediaQuery {
10-
#version = source(0);
1112
#query;
12-
#notify;
13+
#version = source(0);
14+
#notify = createStartStopNotifier(() => {
15+
return on(this.#query, 'change', () => increment(this.#version));
16+
});
1317

1418
get current() {
1519
if (effect_tracking()) {
@@ -21,15 +25,11 @@ export class MediaQuery {
2125
}
2226

2327
/**
24-
* @param {string} query A media query string (don't forget the braces)
28+
* @param {string} query A media query string
2529
* @param {boolean} [matches] Fallback value for the server
2630
*/
2731
constructor(query, matches) {
28-
this.#query = window.matchMedia(query);
29-
this.#notify = createStartStopNotifier(() => {
30-
const listener = () => set(this.#version, this.#version.v + 1);
31-
this.#query.addEventListener('change', listener);
32-
return () => this.#query.removeEventListener('change', listener);
33-
});
32+
// For convenience (and because people likely forget them) we add the parentheses; double parantheses are not a problem
33+
this.#query = window.matchMedia(`(${query})`);
3434
}
3535
}

0 commit comments

Comments
 (0)