Skip to content

Commit

Permalink
typing: remove window refs
Browse files Browse the repository at this point in the history
  • Loading branch information
pulsejet committed Mar 2, 2025
1 parent 069cfe8 commit a42f0a7
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
Binary file modified public/main.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions src/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const showProjectModal = ref(false);
const activeProjectName = ref(null as string | null);
const projectFiles = ref([] as IProjectFile[]);
const connState = ref(window._ndnd_conn_state);
const connState = ref(globalThis._ndnd_conn_state);
const busListeners = {
'project-list': (projs: IProject[]) => (projects.value = projs),
Expand All @@ -136,7 +136,7 @@ const busListeners = {
},
'chat-channels': (chans: IChatChannel[]) => (channels.value = chans),
'conn-change': () => {
connState.value = window._ndnd_conn_state;
connState.value = globalThis._ndnd_conn_state;
if (!connState.value.connected) {
Toast.info('Disconnected - you are offline');
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const GlobalBus = new EventEmitter() as TypedEmitter<{

/**
* Event when connectivity changes.
* The state is stored in window._ndnd_conn_state
* The state is stored in _ndnd_conn_state
*/
'conn-change': () => void;
}>;
42 changes: 20 additions & 22 deletions src/services/ndn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ import { StoreDexie, type StoreJS } from './store_js';
import { KeyChainDexie, type KeyChainJS } from './keychain_js';
import { GlobalBus } from './event-bus';

/* eslint-disable no-var */
declare global {
interface Window {
_ndnd_store_js: StoreJS;
_ndnd_keychain_js: KeyChainJS;
_yjs_merge_updates: (updates: Uint8Array[]) => Uint8Array;

_ndnd_conn_change_js: (connected: boolean, router: string) => void;
_ndnd_conn_state: { connected: boolean; router: string };

Go: typeof Go;
set_ndn?: (ndn: NDNAPI) => void;
ndn_api: NDNAPI;
}
var _ndnd_store_js: StoreJS;
var _ndnd_keychain_js: KeyChainJS;
var _yjs_merge_updates: (updates: Uint8Array[]) => Uint8Array;
var _ndnd_conn_change_js: (connected: boolean, router: string) => void;
var _ndnd_conn_state: { connected: boolean; router: string };

var set_ndn: undefined | ((ndn: NDNAPI) => void);
var ndn_api: NDNAPI;
}
/* eslint-enable no-var */

interface NDNAPI {
/** Check if there is a valid testbed key in the keychain */
Expand Down Expand Up @@ -121,22 +119,22 @@ class NDNService {
if (this.api) return;

// Provide JS APIs
window._ndnd_store_js = new StoreDexie('store');
window._ndnd_keychain_js = new KeyChainDexie();
window._yjs_merge_updates = Y.mergeUpdatesV2;
window._ndnd_conn_change_js = _ndnd_conn_change_js;
window._ndnd_conn_state = { connected: false, router: String() };
globalThis._ndnd_store_js = new StoreDexie('store');
globalThis._ndnd_keychain_js = new KeyChainDexie();
globalThis._yjs_merge_updates = Y.mergeUpdatesV2;
globalThis._ndnd_conn_change_js = _ndnd_conn_change_js;
globalThis._ndnd_conn_state = { connected: false, router: String() };

// Load the Go WASM module
const go = new window.Go();
const go = new Go();
const result = await WebAssembly.instantiateStreaming(fetch('/main.wasm'), go.importObject);

// Callback given by WebAssembly to set the NDN API
const ndnPromise = new Promise<NDNAPI>((resolve, reject) => {
const cancel = setTimeout(() => reject(new Error('NDN API not set')), 5000);
window.set_ndn = (ndn: NDNAPI) => {
window.set_ndn = undefined;
window.ndn_api = ndn;
globalThis.set_ndn = (ndn: NDNAPI) => {
globalThis.set_ndn = undefined;
globalThis.ndn_api = ndn;
resolve(ndn);
clearTimeout(cancel);
};
Expand All @@ -157,7 +155,7 @@ function _ndnd_conn_change_js(connected: boolean, router: string) {
router = new URL(router).host;
} catch {}
try {
window._ndnd_conn_state = { connected, router };
globalThis._ndnd_conn_state = { connected, router };
GlobalBus.emit('conn-change');
} catch {}
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/svs-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class SvsProvider {
// Compact the database every few updates
if (this.isCompacting || id - this.lastCompaction < 500) return;
this.isCompacting = true;
window.setTimeout(() => this.compact(id), 10); // background
globalThis.setTimeout(() => this.compact(id), 10); // background
}

/**
Expand Down Expand Up @@ -371,7 +371,7 @@ class NdnAwareness extends awareProto.Awareness {
// Unhook on document destroy
doc.once('destroy', async () => {
me.destroy();
window.clearTimeout(me.throttle);
globalThis.clearTimeout(me.throttle);
await me.flush(ndnAwareness); // last update
await ndnAwareness.stop();
});
Expand Down Expand Up @@ -406,7 +406,7 @@ class NdnAwareness extends awareProto.Awareness {
me.throttleSet.add(client);
}
if (!me.throttle) {
me.throttle = window.setTimeout(() => me.flush(ndnAwareness), 250);
me.throttle = globalThis.setTimeout(() => me.flush(ndnAwareness), 250);
}
});

Expand Down
13 changes: 6 additions & 7 deletions src/services/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import type { IWkspStats } from '@/services/types';
* This always runs in the background collecting data.
*/
declare global {
interface Window {
ActiveWorkspace: Workspace | null;
}
// eslint-disable-next-line no-var
var ActiveWorkspace: Workspace | null;
}

/**
Expand Down Expand Up @@ -101,17 +100,17 @@ export class Workspace {
}); // background

// Start workspace if not already active
if (window.ActiveWorkspace?.metadata.name !== metadata.name) {
if (globalThis.ActiveWorkspace?.metadata.name !== metadata.name) {
try {
await window.ActiveWorkspace?.destroy();
await globalThis.ActiveWorkspace?.destroy();
} catch (e) {
console.error(e);
GlobalBus.emit('wksp-error', new Error(`Failed to stop workspace: ${e}`));
}
window.ActiveWorkspace = await Workspace.create(metadata);
globalThis.ActiveWorkspace = await Workspace.create(metadata);
}

return window.ActiveWorkspace;
return globalThis.ActiveWorkspace;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/views/SpaceDiscussView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ async function setup() {
// Scroll to the end of the chat
nextTick(() => scroller.value?.scrollToBottom());
window.setTimeout(() => scroller.value?.scrollToBottom(), 100); // why
window.setTimeout(() => scroller.value?.scrollToBottom(), 500); // uhh
globalThis.setTimeout(() => scroller.value?.scrollToBottom(), 100); // why
globalThis.setTimeout(() => scroller.value?.scrollToBottom(), 500); // uhh
}
/** Skip the header if the user is the same and the message is within a minute */
Expand Down

0 comments on commit a42f0a7

Please sign in to comment.