Skip to content

Commit

Permalink
Remove references to window from headless
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Mar 22, 2024
1 parent 03d3466 commit f8ae78b
Show file tree
Hide file tree
Showing 29 changed files with 127 additions and 154 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/headless/plugins/chat/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Message extends ModelWithContact {
const is_ephemeral = this.isEphemeral();
if (is_ephemeral) {
const timeout = typeof is_ephemeral === "number" ? is_ephemeral : 10000;
this.ephemeral_timer = window.setTimeout(() => this.safeDestroy(), timeout);
this.ephemeral_timer = setTimeout(() => this.safeDestroy(), timeout);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/headless/plugins/chat/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,17 +485,17 @@ class ChatBox extends ModelWithContact {
*/
setChatState (state, options) {
if (this.chat_state_timeout !== undefined) {
window.clearTimeout(this.chat_state_timeout);
clearTimeout(this.chat_state_timeout);
delete this.chat_state_timeout;
}
if (state === COMPOSING) {
this.chat_state_timeout = window.setTimeout(
this.chat_state_timeout = setTimeout(
this.setChatState.bind(this),
_converse.TIMEOUTS.PAUSED,
PAUSED
);
} else if (state === PAUSED) {
this.chat_state_timeout = window.setTimeout(
this.chat_state_timeout = setTimeout(
this.setChatState.bind(this),
_converse.TIMEOUTS.INACTIVE,
INACTIVE
Expand Down Expand Up @@ -1015,7 +1015,7 @@ class ChatBox extends ModelWithContact {
return;
}
const data = item.dataforms.where({'FORM_TYPE': {'value': Strophe.NS.HTTPUPLOAD, 'type': "hidden"}}).pop();
const max_file_size = window.parseInt((data?.attributes || {})['max-file-size']?.value);
const max_file_size = parseInt((data?.attributes || {})['max-file-size']?.value, 10);
const slot_request_url = item?.id;

if (!slot_request_url) {
Expand All @@ -1036,7 +1036,7 @@ class ChatBox extends ModelWithContact {
*/
file = await api.hook('beforeFileUpload', this, file);

if (!window.isNaN(max_file_size) && file.size > max_file_size) {
if (!isNaN(max_file_size) && file.size > max_file_size) {
return this.createMessage({
'message': __('The size of your file, %1$s, exceeds the maximum allowed by your server, which is %2$s.',
file.name, filesize(max_file_size).join(', ')),
Expand Down
2 changes: 1 addition & 1 deletion src/headless/plugins/muc/muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ class MUC extends ChatBox {
const actors_per_traffic_state = converse.MUC_TRAFFIC_STATES_LIST.reduce(reducer, {});
const actors_per_role_change = converse.MUC_ROLE_CHANGES_LIST.reduce(reducer, {});
this.notifications.set(Object.assign(actors_per_chat_state, actors_per_traffic_state, actors_per_role_change));
window.setTimeout(() => this.removeNotification(actor, state), 10000);
setTimeout(() => this.removeNotification(actor, state), 10000);
}

handleMetadataFastening (attrs) {
Expand Down
11 changes: 4 additions & 7 deletions src/headless/plugins/roster/contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ class RosterContacts extends Collection {
* Register a handler for roster IQ "set" stanzas, which update
* roster contacts.
*/
// eslint-disable-next-line class-methods-use-this
registerRosterHandler () {
// Register a handler for roster IQ "set" stanzas, which update
// roster contacts.
api.connection.get().addHandler(iq => {
api.connection.get().addHandler((iq) => {
_converse.state.roster.onRosterPush(iq);
return true;
}, Strophe.NS.ROSTER, 'iq', "set");
Expand All @@ -50,14 +49,12 @@ class RosterContacts extends Collection {
* Register a handler for RosterX message stanzas, which are
* used to suggest roster contacts to a user.
*/
// eslint-disable-next-line class-methods-use-this
registerRosterXHandler () {
let t = 0;
const connection = api.connection.get();
connection.addHandler(
function (msg) {
const { roster } = _converse.state;
window.setTimeout(function () {
connection.addHandler((msg) => {
setTimeout(() => {
const { roster } = _converse.state;
api.connection.get().flush();
roster.subscribeToSuggestedItems(msg);
}, t);
Expand Down
4 changes: 2 additions & 2 deletions src/headless/plugins/status/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export function registerIntervalHandler () {
window.addEventListener('keypress', onUserActivity);
window.addEventListener('mousemove', onUserActivity);
window.addEventListener(getUnloadEvent(), onUserActivity, {'once': true, 'passive': true});
everySecondTrigger = window.setInterval(onEverySecond, 1000);
everySecondTrigger = setInterval(onEverySecond, 1000);
}

export function tearDown () {
Expand All @@ -158,7 +158,7 @@ export function tearDown () {
window.removeEventListener('mousemove', onUserActivity);
window.removeEventListener(getUnloadEvent(), onUserActivity);
if (everySecondTrigger) {
window.clearInterval(everySecondTrigger);
clearInterval(everySecondTrigger);
everySecondTrigger = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/headless/shared/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Connection extends Strophe.Connection {
async onDomainDiscovered (response) {
const { api } = _converse;
const text = await response.text();
const xrd = (new window.DOMParser()).parseFromString(text, "text/xml").firstElementChild;
const xrd = (new DOMParser()).parseFromString(text, "text/xml").firstElementChild;
if (xrd.nodeName != "XRD" || xrd.namespaceURI != "http://docs.oasis-open.org/ns/xri/xrd-1.0") {
return log.warn("Could not discover XEP-0156 connection methods");
}
Expand Down
62 changes: 32 additions & 30 deletions src/headless/tests/eventemitter.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
/*global mock */

const container = {};

describe("The _converse Event Emitter", function() {

it("allows you to subscribe to emitted events", mock.initConverse((_converse) => {
window.callback = function () {};
spyOn(window, 'callback');
_converse.on('connected', window.callback);
container.callback = function () {};
spyOn(container, 'callback');
_converse.on('connected', container.callback);
_converse.api.trigger('connected');
expect(window.callback).toHaveBeenCalled();
expect(container.callback).toHaveBeenCalled();
_converse.api.trigger('connected');
expect(window.callback.calls.count(), 2);
expect(container.callback.calls.count(), 2);
_converse.api.trigger('connected');
expect(window.callback.calls.count(), 3);
expect(container.callback.calls.count(), 3);
}));

it("allows you to listen once for an emitted event", mock.initConverse((_converse) => {
window.callback = function () {};
spyOn(window, 'callback');
_converse.once('connected', window.callback);
container.callback = function () {};
spyOn(container, 'callback');
_converse.once('connected', container.callback);
_converse.api.trigger('connected');
expect(window.callback).toHaveBeenCalled();
expect(container.callback).toHaveBeenCalled();
_converse.api.trigger('connected');
expect(window.callback.calls.count(), 1);
expect(container.callback.calls.count(), 1);
_converse.api.trigger('connected');
expect(window.callback.calls.count(), 1);
expect(container.callback.calls.count(), 1);
}));

it("allows you to stop listening or subscribing to an event", mock.initConverse((_converse) => {
window.callback = function () {};
window.anotherCallback = function () {};
window.neverCalled = function () {};
container.callback = function () {};
container.anotherCallback = function () {};
container.neverCalled = function () {};

spyOn(window, 'callback');
spyOn(window, 'anotherCallback');
spyOn(window, 'neverCalled');
_converse.on('connected', window.callback);
_converse.on('connected', window.anotherCallback);
spyOn(container, 'callback');
spyOn(container, 'anotherCallback');
spyOn(container, 'neverCalled');
_converse.on('connected', container.callback);
_converse.on('connected', container.anotherCallback);

_converse.api.trigger('connected');
expect(window.callback).toHaveBeenCalled();
expect(window.anotherCallback).toHaveBeenCalled();
expect(container.callback).toHaveBeenCalled();
expect(container.anotherCallback).toHaveBeenCalled();

_converse.off('connected', window.callback);
_converse.off('connected', container.callback);

_converse.api.trigger('connected');
expect(window.callback.calls.count(), 1);
expect(window.anotherCallback.calls.count(), 2);
expect(container.callback.calls.count(), 1);
expect(container.anotherCallback.calls.count(), 2);

_converse.once('connected', window.neverCalled);
_converse.off('connected', window.neverCalled);
_converse.once('connected', container.neverCalled);
_converse.off('connected', container.neverCalled);

_converse.api.trigger('connected');
expect(window.callback.calls.count(), 1);
expect(window.anotherCallback.calls.count(), 3);
expect(window.neverCalled).not.toHaveBeenCalled();
expect(container.callback.calls.count(), 1);
expect(container.anotherCallback.calls.count(), 3);
expect(container.neverCalled).not.toHaveBeenCalled();
}));
});
2 changes: 1 addition & 1 deletion src/headless/types/plugins/chat/message.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare class Message extends ModelWithContact {
* @method _converse.Message#setTimerForEphemeralMessage
*/
setTimerForEphemeralMessage(): void;
ephemeral_timer: number;
ephemeral_timer: NodeJS.Timeout;
checkValidity(): boolean;
/**
* Determines whether this messsage may be retracted by the current user.
Expand Down
2 changes: 1 addition & 1 deletion src/headless/types/plugins/chat/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ declare class ChatBox extends ModelWithContact {
* @param { string } state - The chat state (consts ACTIVE, COMPOSING, PAUSED, INACTIVE, GONE)
*/
setChatState(state: string, options: any): ChatBox;
chat_state_timeout: number;
chat_state_timeout: NodeJS.Timeout;
/**
* Given an error `<message>` stanza's attributes, find the saved message model which is
* referenced by that error.
Expand Down
5 changes: 0 additions & 5 deletions src/headless/types/utils/html.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ export function isElement(el: any): boolean;
* @returns { boolean }
*/
export function isTagEqual(stanza: Element | typeof Strophe.Builder, name: string): boolean;
/**
* @param {HTMLElement} el
* @param {boolean} include_margin
*/
export function getOuterWidth(el: HTMLElement, include_margin?: boolean): number;
/**
* Converts an HTML string into a DOM element.
* Expects that the HTML string has only one top-level element,
Expand Down
6 changes: 0 additions & 6 deletions src/headless/types/utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ declare const _default: {
getDefaultStore: typeof getDefaultStore;
getLongestSubstring: typeof getLongestSubstring;
getOpenPromise: any;
getOuterWidth: typeof getOuterWidth;
getRandomInt: typeof getRandomInt;
getSelectValues: typeof getSelectValues;
getURI: typeof getURI;
getUniqueId: typeof getUniqueId;
isAllowedProtocolForMedia: typeof isAllowedProtocolForMedia;
isAudioURL: typeof isAudioURL;
isElement: typeof isElement;
isEmptyMessage: typeof isEmptyMessage;
Expand Down Expand Up @@ -60,7 +58,6 @@ declare const _default: {
safeSave: typeof safeSave;
shouldClearCache: typeof shouldClearCache;
shouldCreateMessage: typeof shouldCreateMessage;
shouldRenderMediaFromURL: typeof shouldRenderMediaFromURL;
stringToArrayBuffer: typeof stringToArrayBuffer;
stringToElement: typeof stringToElement;
toStanza: typeof toStanza;
Expand All @@ -78,10 +75,8 @@ import { createStore } from "./storage.js";
import { getCurrentWord } from "./form.js";
import { getDefaultStore } from "./storage.js";
declare function getLongestSubstring(string: any, candidates: any): any;
import { getOuterWidth } from "./html.js";
import { getSelectValues } from "./form.js";
import { getURI } from "./url.js";
import { isAllowedProtocolForMedia } from "./url.js";
import { isAudioURL } from "./url.js";
import { isElement } from "./html.js";
import { isError } from "./object.js";
Expand Down Expand Up @@ -112,7 +107,6 @@ import { queryChildren } from "./html.js";
import { replaceCurrentWord } from "./form.js";
import { shouldClearCache } from "./session.js";
declare function shouldCreateMessage(attrs: any): any;
import { shouldRenderMediaFromURL } from "./url.js";
import { stringToArrayBuffer } from "./arraybuffer.js";
import { stringToElement } from "./html.js";
import { toStanza } from "strophe.js";
Expand Down
8 changes: 0 additions & 8 deletions src/headless/types/utils/url.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
* @returns {boolean}
*/
export function isValidURL(text: string): boolean;
/**
* Given a url, check whether the protocol being used is allowed for rendering
* the media in the chat (as opposed to just rendering a URL hyperlink).
* @param {string} url
* @returns {boolean}
*/
export function isAllowedProtocolForMedia(url: string): boolean;
/**
* @param {string|URI} url
*/
Expand All @@ -26,7 +19,6 @@ export function getURI(url: string | URI): any;
*/
export function checkFileTypes(types: string[], url: string): boolean;
export function isDomainWhitelisted(whitelist: any, url: any): any;
export function shouldRenderMediaFromURL(url_text: any, type: any): any;
export function filterQueryParamsFromURL(url: any): any;
export function isDomainAllowed(url: any, setting: any): any;
/**
Expand Down
6 changes: 3 additions & 3 deletions src/headless/utils/arraybuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export function arrayBufferToBase64 (ab) {
}

export function base64ToArrayBuffer (b64) {
const binary_string = window.atob(b64),
len = binary_string.length,
bytes = new Uint8Array(len);
const binary_string = atob(b64);
const len = binary_string.length;
const bytes = new Uint8Array(len);

for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i)
Expand Down
15 changes: 0 additions & 15 deletions src/headless/utils/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,6 @@ export function isTagEqual (stanza, name) {
}
}

/**
* @param {HTMLElement} el
* @param {boolean} include_margin
*/
export function getOuterWidth (el, include_margin=false) {
let width = el.offsetWidth;
if (!include_margin) {
return width;
}
const style = window.getComputedStyle(el);
width += parseInt(style.marginLeft ? style.marginLeft : '0', 10) +
parseInt(style.marginRight ? style.marginRight : '0', 10);
return width;
}

/**
* Converts an HTML string into a DOM element.
* Expects that the HTML string has only one top-level element,
Expand Down
Loading

0 comments on commit f8ae78b

Please sign in to comment.