Skip to content

Commit

Permalink
Handle node.js environment correctly (CoderLine#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 committed Feb 13, 2021
1 parent 418b396 commit a9f84fb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
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
}

0 comments on commit a9f84fb

Please sign in to comment.