Skip to content

Commit

Permalink
Merge pull request #266 from vitalygashkov/next
Browse files Browse the repository at this point in the history
Improved storage for extensions, added `common.getRandomElements` in extension API, improved `eval` error handling
  • Loading branch information
vitalygashkov authored Dec 7, 2024
2 parents cf89477 + aca4dc9 commit fc19a60
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apps/cli
Submodule cli updated from e7b92d to 411f7f
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamyx/api",
"version": "0.1.1",
"version": "0.1.2",
"description": "Type definitions for the latest Streamyx API",
"main": "streamyx.js",
"types": "streamyx.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions packages/api/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ export type Common = {
seasonsCount: number;
episodesCount: number;
};

getRandomElements: (array: unknown, count?: number) => unknown[];
};
46 changes: 27 additions & 19 deletions packages/core/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,40 @@ const createStorePath = (name: string) => {
return oldStorePath;
};

const exitQueue: (() => void)[] = [];

const onExit = () => {
for (const fn of exitQueue) fn();
exitQueue.length = 0;
};

process.on('SIGINT', () => {
onExit();
process.exit(0); // Ensure the app exits
});

process.on('SIGTERM', () => {
onExit();
process.exit(0);
});

// Catch uncaught exceptions
process.on('uncaughtException', () => {
onExit;
process.exit(1);
});

// Optional: Catch exit event (not ideal for async operations)
process.on('exit', () => onExit());

class LocalStorage implements Storage {
items: Map<string, any>;
filePath: string;

constructor(filePath: string) {
this.items = new Map();
this.filePath = filePath;

process.on('SIGINT', () => {
this.save();
process.exit(0); // Ensure the app exits
});

process.on('SIGTERM', () => {
this.save();
process.exit(0);
});

// Catch uncaught exceptions
process.on('uncaughtException', () => {
this.save();
process.exit(1);
});

// Optional: Catch exit event (not ideal for async operations)
process.on('exit', () => this.save());
exitQueue.push(() => this.save());
}

async load() {
Expand Down
9 changes: 7 additions & 2 deletions packages/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ const load = async (scriptPath, options = {}) => {
};

const create = (src, name = 'Eval') => {
const script = metavm.createScript(name, src);
return script;
try {
const script = metavm.createScript(name, src);
return script;
} catch (e) {
console.error(`Syntax Error: ${e.message}`);
return null;
}
};

module.exports = { load, create };

0 comments on commit fc19a60

Please sign in to comment.