Skip to content

Commit 6a27c6d

Browse files
Cleanup some unnecessary utility (pyscript#1453)
1 parent 213ced0 commit 6a27c6d

File tree

5 files changed

+8
-41
lines changed

5 files changed

+8
-41
lines changed

pyscriptjs/src/main.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { InterpreterClient } from './interpreter_client';
88
import { PluginManager, Plugin, PythonPlugin } from './plugin';
99
import { make_PyScript, initHandlers, mountElements } from './components/pyscript';
1010
import { getLogger } from './logger';
11-
import { showWarning, globalExport, createLock } from './utils';
11+
import { showWarning, createLock } from './utils';
1212
import { calculateFetchPaths } from './plugins/calculateFetchPaths';
1313
import { createCustomElements } from './components/elements';
1414
import { UserError, ErrorCode, _createAlertBanner } from './exceptions';
@@ -450,10 +450,7 @@ modules must contain a "plugin" attribute. For more information check the plugin
450450
}
451451
}
452452

453-
function pyscript_get_config() {
454-
return globalApp.config;
455-
}
456-
globalExport('pyscript_get_config', pyscript_get_config);
453+
globalThis.pyscript_get_config = () => globalApp.config;
457454

458455
// main entry point of execution
459456
const globalApp = new PyScriptApp();

pyscriptjs/src/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class PluginManager {
143143
}
144144

145145
add(...plugins: Plugin[]) {
146-
for (const p of plugins) this._plugins.push(p);
146+
this._plugins.push(...plugins);
147147
}
148148

149149
addPythonPlugin(plugin: PythonPlugin) {

pyscriptjs/src/pyconfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import toml from '@hoodmane/toml-j0.4';
22
import { getLogger } from './logger';
33
import { version } from './version';
4-
import { getAttribute, readTextFromPath, htmlDecode, createDeprecationWarning } from './utils';
4+
import { readTextFromPath, htmlDecode, createDeprecationWarning } from './utils';
55
import { UserError, ErrorCode } from './exceptions';
66

77
const logger = getLogger('py-config');
@@ -74,7 +74,7 @@ export function loadConfigFromElement(el: Element): AppConfig {
7474
srcConfig = {};
7575
inlineConfig = {};
7676
} else {
77-
const configType = getAttribute(el, 'type') || 'toml';
77+
const configType = el.getAttribute('type') || 'toml';
7878
srcConfig = extractFromSrc(el, configType);
7979
inlineConfig = extractFromInline(el, configType);
8080
}
@@ -88,7 +88,7 @@ export function loadConfigFromElement(el: Element): AppConfig {
8888
}
8989

9090
function extractFromSrc(el: Element, configType: string) {
91-
const src = getAttribute(el, 'src');
91+
const src = el.getAttribute('src');
9292
if (src) {
9393
logger.info('loading ', src);
9494
return validateConfig(readTextFromPath(src), configType);

pyscriptjs/src/pyexec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ export async function pyDisplay(interpreter: InterpreterClient, obj: any, kwargs
5555
}
5656

5757
export function displayPyException(err: Error, errElem: HTMLElement) {
58-
//addClasses(errElem, ['py-error'])
5958
const pre = document.createElement('pre');
6059
pre.className = 'py-error';
6160

pyscriptjs/src/utils.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ import { $$ } from 'basic-devtools';
22

33
import { _createAlertBanner } from './exceptions';
44

5-
export function addClasses(element: HTMLElement, classes: string[]) {
6-
for (const entry of classes) {
7-
element.classList.add(entry);
8-
}
9-
}
10-
11-
export function removeClasses(element: HTMLElement, classes: string[]) {
12-
for (const entry of classes) {
13-
element.classList.remove(entry);
14-
}
15-
}
16-
175
export function escape(str: string): string {
186
return str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
197
}
@@ -60,23 +48,6 @@ export function inJest(): boolean {
6048
return typeof process === 'object' && process.env.JEST_WORKER_ID !== undefined;
6149
}
6250

63-
export function globalExport(name: string, obj: object) {
64-
// attach the given object to the global object, so that it is globally
65-
// visible everywhere. Should be used very sparingly!
66-
67-
globalThis[name] = obj;
68-
}
69-
70-
export function getAttribute(el: Element, attr: string): string | null {
71-
if (el.hasAttribute(attr)) {
72-
const value = el.getAttribute(attr);
73-
if (value) {
74-
return value;
75-
}
76-
}
77-
return null;
78-
}
79-
8051
export function joinPaths(parts: string[], separator = '/') {
8152
const res = parts
8253
.map(function (part) {
@@ -102,11 +73,11 @@ export function createDeprecationWarning(msg: string, elementName: string): void
10273
* @param sentinelText {string} [null] The text to match against existing warning banners.
10374
* If null, the full text of 'msg' is used instead.
10475
*/
105-
export function createSingularWarning(msg: string, sentinelText: string | null = null): void {
76+
export function createSingularWarning(msg: string, sentinelText?: string): void {
10677
const banners = $$('.alert-banner, .py-warning', document);
10778
let bannerCount = 0;
10879
for (const banner of banners) {
109-
if (banner.innerHTML.includes(sentinelText ? sentinelText : msg)) {
80+
if (banner.innerHTML.includes(sentinelText || msg)) {
11081
bannerCount++;
11182
}
11283
}

0 commit comments

Comments
 (0)