Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle node.js environment correctly #544

Merged
merged 2 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions src/Environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import { Logger } from '@src/Logger';
import { LeftHandTapEffectInfo } from './rendering/effects/LeftHandTapEffectInfo';
import { CapellaImporter } from './importer/CapellaImporter';
import { ResizeObserverPolyfill } from './platform/javascript/ResizeObserverPolyfill';
import { WebPlatform } from './platform/javascript/WebPlatform';

export class LayoutEngineFactory {
public readonly vertical: boolean;
Expand Down Expand Up @@ -86,12 +87,12 @@ export class RenderEngineFactory {
*/
export class Environment {
/**
* The font size of the music font in pixel.
* The font size of the music font in pixel.
*/
public static readonly MusicFontSize = 34;

/**
* The scaling factor to use when rending raster graphics for sharper rendering on high-dpi displays.
* The scaling factor to use when rending raster graphics for sharper rendering on high-dpi displays.
*/
public static HighDpiFactor = 1;

Expand Down Expand Up @@ -158,12 +159,15 @@ export class Environment {
try {
Environment._globalThis = globalThis;
} catch (e) {
// global this not available
// globalThis not available
}

if (typeof Environment._globalThis === 'undefined') {
Environment._globalThis = self;
}
if (typeof Environment._globalThis === 'undefined') {
Environment._globalThis = global;
}
if (typeof Environment._globalThis === 'undefined') {
Environment._globalThis = window;
}
Expand All @@ -175,6 +179,11 @@ export class Environment {
return this._globalThis;
}

/**
* @target web
*/
public static webPlatform: WebPlatform = Environment.detectWebPlatform();

/**
* @target web
*/
Expand All @@ -183,9 +192,7 @@ export class Environment {
/**
* @target web
*/
public static bravuraFontChecker: FontLoadingChecker = new FontLoadingChecker(
'alphaTab'
);
public static bravuraFontChecker: FontLoadingChecker = new FontLoadingChecker('alphaTab');

/**
* @target web
Expand All @@ -200,16 +207,16 @@ export class Environment {
public static throttle(action: () => void, delay: number): () => void {
let timeoutId: number = 0;
return () => {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(action, delay);
Environment.globalThis.clearTimeout(timeoutId);
timeoutId = Environment.globalThis.setTimeout(action, delay);
};
}

/**
* @target web
*/
private static detectScriptFile(): string | null {
if (Environment.isRunningInWorker) {
if (Environment.isRunningInWorker || Environment.webPlatform !== WebPlatform.Browser) {
return null;
}
return (document.currentScript as HTMLScriptElement).src;
Expand Down Expand Up @@ -448,19 +455,39 @@ export class Environment {
* @target web
*/
public static platformInit(): void {
Environment.registerJQueryPlugin();
if (!Environment.isRunningInWorker) {
if (Environment.isRunningInWorker) {
AlphaTabWebWorker.init();
AlphaSynthWebWorker.init();
} else if (Environment.webPlatform === WebPlatform.Browser) {
Environment.registerJQueryPlugin();
Environment.HighDpiFactor = window.devicePixelRatio;
// ResizeObserver API does not yet exist so long on Safari (only start 2020 with iOS Safari 13.7 and Desktop 13.1)
// so we better add a polyfill for it
if(!('ResizeObserver' in globalThis)) {
// so we better add a polyfill for it
if (!('ResizeObserver' in globalThis)) {
(globalThis as any).ResizeObserver = ResizeObserverPolyfill;
}
} else {
AlphaTabWebWorker.init();
AlphaSynthWebWorker.init();
}
}

/**
* @target web
*/
private static detectWebPlatform(): WebPlatform {
try {
// Credit of the node.js detection goes to
// https://github.com/iliakan/detect-node
// MIT License
// Copyright (c) 2017 Ilya Kantor
// tslint:disable-next-line: strict-type-predicates
if (Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]') {
return WebPlatform.NodeJs;
}
} catch (e) {
// no node.js
}

return WebPlatform.Browser;
}
}

Environment.platformInit();
Environment.platformInit();
2 changes: 1 addition & 1 deletion src/platform/javascript/AlphaSynthWebAudioOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class AlphaSynthWebAudioOutput implements ISynthOutput {
if (ctx.state === 'suspended') {
let resume = () => {
ctx.resume();
window.setTimeout(() => {
Environment.globalThis.setTimeout(() => {
if (ctx.state === 'running') {
document.body.removeEventListener('touchend', resume, false);
document.body.removeEventListener('click', resume, false);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/javascript/AlphaTabWebWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class AlphaTabWebWorker {
}

public static init(): void {
Environment.globalThis.alphaTabWebWorker = new AlphaTabWebWorker(Environment.globalThis as IWorkerScope);
(Environment.globalThis as any).alphaTabWebWorker = new AlphaTabWebWorker(Environment.globalThis as IWorkerScope);
}

private handleMessage(e: MessageEvent): void {
Expand Down
8 changes: 8 additions & 0 deletions src/platform/javascript/BrowserUiFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { BrowserMouseEventArgs } from '@src/platform/javascript/BrowserMouseEven
import { Cursors } from '@src/platform/Cursors';
import { JsonConverter } from '@src/model/JsonConverter';
import { SettingsSerializer } from '@src/generated/SettingsSerializer';
import { WebPlatform } from './WebPlatform';
import { AlphaTabError } from '@src/AlphaTabError';
import { AlphaTabErrorType } from '@src/alphatab';

/**
* @target web
Expand Down Expand Up @@ -81,6 +84,11 @@ export class BrowserUiFacade implements IUiFacade<unknown> {
}

public constructor(rootElement: HTMLElement) {
if(Environment.webPlatform !== WebPlatform.Browser) {
throw new AlphaTabError(AlphaTabErrorType.General,
'Usage of AlphaTabApi is only possible in browser environments. For usage in node use the Low Level APIs'
);
}
rootElement.classList.add('alphaTab');
this.rootContainer = new HtmlElementContainer(rootElement);
this.areWorkersSupported = 'Worker' in window;
Expand Down
8 changes: 8 additions & 0 deletions src/platform/javascript/WebPlatform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Lists all web specific platforms alphaTab might run in
* like browser, nodejs.
*/
export enum WebPlatform {
Browser,
NodeJs
}