|
1 | 1 | <script>
|
2 | 2 | document.title = "nostrpul.se";
|
3 | 3 |
|
4 |
| - // import "websocket-polyfill"; |
5 |
| -
|
6 | 4 | import { onMount, afterUpdate, tick } from "svelte";
|
7 | 5 | import { fade } from "svelte/transition";
|
8 | 6 | import { writable } from "svelte/store";
|
9 | 7 |
|
10 | 8 | import Modal from "./Modal.svelte";
|
11 | 9 |
|
12 |
| - import { NostrFetcher } from "nostr-fetch" |
| 10 | + import * as Tone from "tone"; |
| 11 | + import ToneGenerator from "./tone"; |
| 12 | +
|
| 13 | + import { NostrFetcher } from "nostr-fetch"; |
13 | 14 | import { Relay } from "nostr-tools";
|
14 |
| - // import { simplePoolAdapter } from "@nostr-fetch/adapter-nostr-tools"; |
15 | 15 |
|
16 | 16 | import Masonry from "masonry-layout";
|
17 | 17 |
|
18 |
| - import { Sound } from "svelte-sound"; |
19 |
| - import tone_file from "./assets/tone.mp3"; |
20 |
| -
|
21 |
| - const tone = new Sound(tone_file); |
22 |
| -
|
| 18 | + let toneGenerator; |
23 | 19 |
|
24 | 20 | import { generateBackground } from "./utils";
|
25 | 21 |
|
|
37 | 33 | const loading30166 = writable(false);
|
38 | 34 | const event30166 = writable(null);
|
39 | 35 | let activeTab = writable(0); // Currently selected tab index
|
40 |
| - let focusRelay = writable(null) |
| 36 | + let focusRelay = writable(null); |
41 | 37 |
|
42 | 38 | let loading = true;
|
43 | 39 | let masonry;
|
44 | 40 | let initialSyncComplete = false;
|
45 | 41 | let since = Math.round(Date.now() / 1000) - 60 * 60 * 1.1;
|
46 | 42 |
|
| 43 | + const newEvents = []; |
| 44 | + |
| 45 | + let eventRunner |
| 46 | +
|
47 | 47 | $: if ($activeTab) {
|
48 | 48 | event30166.set(null);
|
49 | 49 | const ev = get30166($focusRelay)
|
|
57 | 57 | // let currentRelayModal = null;
|
58 | 58 |
|
59 | 59 | function hideModals() {
|
60 |
| - activeTab.set(0) |
61 |
| - focusRelay.set(null) |
62 |
| - event30166.set(null) |
| 60 | + activeTab.set(0); |
| 61 | + focusRelay.set(null); |
| 62 | + event30166.set(null); |
63 | 63 | hideGenericModal();
|
64 | 64 | hideRelayModal();
|
65 | 65 | }
|
|
73 | 73 | }
|
74 | 74 |
|
75 | 75 | function showRelayModal(ev) {
|
76 |
| - focusRelay.set(getUrlFromEvent(ev)) |
| 76 | + focusRelay.set(getUrlFromEvent(ev)); |
77 | 77 | currentRelayModal = ev.id;
|
78 | 78 | }
|
79 | 79 |
|
|
93 | 93 | };
|
94 | 94 | }
|
95 | 95 |
|
96 |
| - const on_event_handler = (event) => { |
97 |
| - if (initialSyncComplete && event.created_at < since) return; |
98 |
| - const processedEvent = processEvent(event); |
99 |
| - since = event.created_at; |
| 96 | + const updateEvents = (processedEvent) => { |
100 | 97 | if (processedEvent) {
|
101 | 98 | k30066.update((currentk30066) => {
|
102 | 99 | return [processedEvent, ...currentk30066].filter(
|
103 | 100 | (v, i, a) => a.findIndex((t) => t.dTag === v.dTag) === i,
|
104 | 101 | );
|
105 | 102 | });
|
106 | 103 | }
|
107 |
| - if (!initialSyncComplete) return; |
| 104 | + }; |
| 105 | +
|
| 106 | + const queueEvent = (event) => { |
| 107 | + newEvents.push(event); |
| 108 | + }; |
| 109 | +
|
| 110 | + const populateNextEvent = () => { |
| 111 | + if(!newEvents.length) return |
| 112 | + const event = newEvents.shift() |
| 113 | + updateEvents(event); |
108 | 114 | processBatch();
|
109 |
| - if(!isMuted) tone.play() |
110 |
| - // console.log(event.id) |
| 115 | + if (!isMuted) toneGenerator.playNextNote(); |
| 116 | + }; |
| 117 | +
|
| 118 | + const on_event_handler = (event) => { |
| 119 | + if (initialSyncComplete && event.created_at < since) return; |
| 120 | + const processedEvent = processEvent(event); |
| 121 | + since = event.created_at; |
| 122 | + if (!initialSyncComplete) |
| 123 | + updateEvents(processedEvent); |
| 124 | + else |
| 125 | + queueEvent(processedEvent); |
111 | 126 | };
|
112 | 127 |
|
113 | 128 | async function initialSync() {
|
114 |
| - const fetcher = NostrFetcher.init() |
| 129 | + const fetcher = NostrFetcher.init(); |
115 | 130 | const iter = fetcher.allEventsIterator(
|
116 | 131 | ["wss://history.nostr.watch"],
|
117 | 132 | {
|
|
130 | 145 | }
|
131 | 146 |
|
132 | 147 | const continuousSync = async () => {
|
133 |
| - RelaySocket = await Relay.connect("wss://history.nostr.watch").catch(continuousSync); |
| 148 | + RelaySocket = await Relay.connect("wss://history.nostr.watch").catch( |
| 149 | + continuousSync, |
| 150 | + ); |
134 | 151 | RelaySocket.subscribe(
|
135 | 152 | [
|
136 | 153 | {
|
|
159 | 176 | };
|
160 | 177 |
|
161 | 178 | async function get30166(relay) {
|
162 |
| - console.log(`getting 30166 for ${relay.url}`) |
| 179 | + console.log(`getting 30166 for ${relay.url}`); |
163 | 180 | return new Promise(async (resolve) => {
|
164 | 181 | RelaySocket = await Relay.connect("wss://history.nostr.watch").catch(
|
165 | 182 | console.error,
|
166 | 183 | );
|
167 | 184 | RelaySocket.subscribe(
|
168 | 185 | [
|
169 | 186 | {
|
170 |
| - "#d": [relay], |
| 187 | + "#d": [relay], |
171 | 188 | limit: 1,
|
172 | 189 | kinds: [30166],
|
173 | 190 | authors: [MONITOR],
|
|
219 | 236 | return ev;
|
220 | 237 | }
|
221 | 238 |
|
222 |
| - function getUrlFromEvent(event){ |
| 239 | + function getUrlFromEvent(event) { |
223 | 240 | return event.tags.filter((tag) => tag[0] == "d")?.[0]?.[1];
|
224 | 241 | }
|
225 | 242 |
|
226 | 243 | function parse30066(event) {
|
227 | 244 | const parsedTags = {};
|
228 |
| - const url = getUrlFromEvent(event) |
| 245 | + const url = getUrlFromEvent(event); |
229 | 246 | if (!url) return { error: "no d tag?" };
|
230 | 247 | const tags = event.tags.filter((tag) => tag.length >= 3);
|
231 | 248 | tags.forEach((tag) => {
|
|
288 | 305 |
|
289 | 306 | onMount(async () => {
|
290 | 307 | await initialSync();
|
| 308 | + eventRunner = setInterval( populateNextEvent, 500 ) |
291 | 309 | await continuousSync();
|
292 |
| -
|
293 | 310 | return () => {
|
| 311 | + clearInterval(eventRunner) |
294 | 312 | RelaySocket.close();
|
295 | 313 | };
|
296 | 314 | });
|
297 | 315 |
|
298 | 316 | function processBatch() {
|
299 | 317 | if (!initialSyncComplete) initialSyncComplete = true;
|
300 | 318 | k30066.update((currentk30066) => [...currentk30066]);
|
301 |
| - |
302 | 319 | }
|
303 | 320 |
|
304 | 321 | function calculateDimensions(event) {
|
|
377 | 394 | let isMuted = true; // Initial state of the sound, assuming it starts muted
|
378 | 395 |
|
379 | 396 | // Function to toggle the muted state
|
380 |
| - function toggleMute() { |
| 397 | + async function toggleMute() { |
381 | 398 | isMuted = !isMuted;
|
382 |
| - // Here, you can also add logic to actually control the audio |
| 399 | + if (isMuted) return; |
| 400 | + if (Tone.context.state !== "running") await Tone.start(); |
| 401 | + else Tone.Transport.cancel(); |
| 402 | + if (!toneGenerator) toneGenerator = new ToneGenerator(); |
383 | 403 | }
|
384 | 404 |
|
385 | 405 | window.addEventListener("resize", () => {
|
|
390 | 410 | </script>
|
391 | 411 |
|
392 | 412 | {#if initialSyncComplete}
|
393 |
| - <button on:click={toggleMute} class="mute"> |
394 |
| - {isMuted ? '🔇' : '🔈'} |
| 413 | + <button |
| 414 | + on:click={() => { |
| 415 | + toggleMute(); |
| 416 | + }} |
| 417 | + class="mute" |
| 418 | + > |
| 419 | + {isMuted ? "🔇" : "🔈"} |
395 | 420 | </button>
|
396 | 421 | <div id="header">
|
397 | 422 | <span class="sitetitle">nostrpul.se</span>
|
|
442 | 467 | on:click={() => showRelayModal(ev)}
|
443 | 468 | role="button"
|
444 | 469 | tabindex="0"
|
445 |
| - on:keydown={(event) => { event.key === "Enter" && showRelayModal(ev) } } |
| 470 | + on:keydown={(event) => { |
| 471 | + event.key === "Enter" && showRelayModal(ev); |
| 472 | + }} |
446 | 473 | >
|
447 | 474 | <span class="rtt">{rttDisplay("open", ev)}</span>
|
448 | 475 | <span class="rtt">{rttDisplay("read", ev)}</span>
|
|
685 | 712 | }
|
686 | 713 |
|
687 | 714 | .mute {
|
688 |
| - display:inline; |
689 |
| - background:none; |
690 |
| - border:none; |
691 |
| - position:absolute; |
692 |
| - top:10px; |
693 |
| - left:50%; |
| 715 | + display: inline; |
| 716 | + background: none; |
| 717 | + border: none; |
| 718 | + position: absolute; |
| 719 | + top: 10px; |
| 720 | + left: 50%; |
| 721 | + transform: translateX(-50%); |
| 722 | + z-index: 100; |
| 723 | + font-size: 3em; |
| 724 | + } |
| 725 | +
|
| 726 | + .test { |
| 727 | + display: inline; |
| 728 | + background: none; |
| 729 | + border: none; |
| 730 | + position: absolute; |
| 731 | + top: 50px; |
| 732 | + left: 50%; |
694 | 733 | transform: translateX(-50%);
|
695 | 734 | z-index: 100;
|
696 |
| - font-size:3em; |
| 735 | + font-size: 3em; |
697 | 736 | }
|
698 | 737 |
|
699 | 738 | @media (max-width: 600px) {
|
|
754 | 793 | }
|
755 | 794 |
|
756 | 795 | .mute {
|
757 |
| - display:none; |
| 796 | + display: none; |
758 | 797 | }
|
759 | 798 | }
|
760 | 799 | </style>
|
0 commit comments