Skip to content

Commit

Permalink
Do not load webtorrent in page load
Browse files Browse the repository at this point in the history
  • Loading branch information
ed-asriyan committed Apr 17, 2024
1 parent cdf170c commit 573fc6d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 0 additions & 2 deletions src/components/index.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import { _ } from 'svelte-i18n';
import { initWebtorrent } from '../stores/web-torrent';
import Loader from './loader.svelte';
import { Room } from '../stores/room';
import { syncTime } from '../stores/clock';
Expand Down Expand Up @@ -30,7 +29,6 @@
const init = async function (roomId: string): Promise<Room> {
await initTime();
await initWebtorrent();
await destroy();
previousRoom = new Room(roomId);
await previousRoom.init();
Expand Down
13 changes: 9 additions & 4 deletions src/stores/web-torrent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js';
import { iceServers, webTorrentTrackers } from '../settings';
import { readable, writable } from 'svelte/store';

Expand All @@ -7,8 +6,9 @@ window.WEBTORRENT_ANNOUNCE = null;

let __client: any;
let __torrent: any;
export const initWebtorrent = async function () {
if (!navigator.serviceWorker) {
let isInitiated = false;
const initWebtorrent = async function () {
if (isInitiated || !navigator.serviceWorker) {
return;
}

Expand All @@ -22,10 +22,12 @@ export const initWebtorrent = async function () {
if (!checkState(worker)) {
worker.addEventListener('statechange', ({ target }) => {
if (checkState(target)) {
isInitiated = true;
resolve();
}
});
} else {
isInitiated = true;
resolve();
}
});
Expand All @@ -49,6 +51,7 @@ export const createWebTorrentClient = async function () {
if (__client) {
return __client;
}
await import('https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js');
// @ts-ignore
const client = new WebTorrent({
tracker: {
Expand All @@ -60,6 +63,7 @@ export const createWebTorrentClient = async function () {
iceCandidatePoolsize: 1
}
});
await initWebtorrent();
await promise(client, client.loadWorker, navigator.serviceWorker.controller);
__client = client;
};
Expand All @@ -81,6 +85,7 @@ export const sendFile = async function (file: File): Promise<string> {
};

export const getStreamUrl = async function (url: string) {
await initWebtorrent();
await createWebTorrentClient();
if (__torrent?.magnetURI !== url) {
isSeeding.set(false);
Expand Down Expand Up @@ -117,4 +122,4 @@ export const timeRemaining = readable<number>(0, set => {
const id = setInterval(() => set(__torrent?.timeRemaining), 1000);
return () => clearInterval(id);
});
export const isSeeding = writable<boolean>(false);
export const isSeeding = writable<boolean>(false);

0 comments on commit 573fc6d

Please sign in to comment.