Add some logging for invalid zifferjs syntax
This commit is contained in:
28
src/API.ts
28
src/API.ts
@ -87,6 +87,7 @@ export class UserAPI {
|
||||
public currentSeed: string | undefined = undefined;
|
||||
public localSeeds = new Map<string, Function>();
|
||||
public patternCache = new LRUCache({ max: 1000, ttl: 1000 * 60 * 5 });
|
||||
public invalidPatterns: {[key: string]: boolean} = {};
|
||||
public cueTimes: { [key: string]: number } = {};
|
||||
private errorTimeoutID: number = 0;
|
||||
private printTimeoutID: number = 0;
|
||||
@ -717,23 +718,37 @@ export class UserAPI {
|
||||
input: string | Generator<number>,
|
||||
options: InputOptions = {},
|
||||
id: number | string = "",
|
||||
): Player => {
|
||||
): Player|undefined => {
|
||||
const zid = "z" + id.toString();
|
||||
const key = id === "" ? this.generateCacheKey(input, options) : zid;
|
||||
|
||||
const validSyntax = typeof input === "string" && !this.invalidPatterns[input]
|
||||
if(!validSyntax) this.app.api.log(`Invalid syntax: ${input}`);
|
||||
|
||||
let player;
|
||||
let replace = false;
|
||||
|
||||
if (this.app.api.patternCache.has(key)) {
|
||||
player = this.app.api.patternCache.get(key) as Player;
|
||||
if (typeof input === "string" && player.input !== input && player.atTheBeginning()) {
|
||||
player = undefined;
|
||||
|
||||
if (typeof input === "string" &&
|
||||
player.input !== input &&
|
||||
player.atTheBeginning()) {
|
||||
replace = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!player) {
|
||||
player = new Player(input, options, this.app, zid);
|
||||
if (validSyntax && (!player || replace)) {
|
||||
const newPlayer = new Player(input, options, this.app, zid);
|
||||
if(newPlayer.isValid()) {
|
||||
player = newPlayer
|
||||
this.app.api.patternCache.set(key, player);
|
||||
} else if(typeof input === "string") {
|
||||
this.invalidPatterns[input] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(player) {
|
||||
|
||||
if (player.ziffers.generator && player.ziffers.generatorDone) {
|
||||
this.removePatternFromCache(key);
|
||||
@ -749,6 +764,9 @@ export class UserAPI {
|
||||
}
|
||||
|
||||
return player;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
public z0 = (input: string, opts: InputOptions = {}) =>
|
||||
|
||||
@ -49,6 +49,10 @@ export class Player extends AbstractEvent {
|
||||
this.zid = zid;
|
||||
}
|
||||
|
||||
isValid() {
|
||||
return this.ziffers.values.length > 0;
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.initCallTime = 0;
|
||||
this.startCallTime = 0;
|
||||
|
||||
Reference in New Issue
Block a user