Skip to content

Commit

Permalink
Merge pull request #269 from permafrost-dev/nodejs-sync
Browse files Browse the repository at this point in the history
Add initSettings() to NodeRay
  • Loading branch information
patinthehat authored Mar 29, 2024
2 parents f10a743 + cc8bbfb commit 411c54f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ Ray.useDefaultSettings({ port: 3000 });
// ...and use ray() as normal
```

**When using NodeJS,** you must call `await Ray.initSettings()` to initialize the settings before using `ray()`.
This is not necessary when using the browser bundle.

```js
ray('a string');

Expand Down
20 changes: 17 additions & 3 deletions src/RayNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@ import { ImagePayload } from '@/Payloads/ImagePayload';
import { NodeInfoPayload } from '@/Payloads/NodeInfoPayload';
import { NodeMeasurePayload } from '@/Payloads/NodeMeasurePayload';
import { Ray as BaseRay } from '@/Ray';
import { Settings } from '@/Settings/Settings';
import { SettingsFactory } from '@/Settings/SettingsFactory';
import { NodeStopwatch } from '@/Stopwatch/NodeStopwatch';
import { existsSync } from 'node:fs';

// @ts-ignore
export class Ray extends BaseRay {
public static async create(client: Client | null = null, uuid: string | null = null): Promise<Ray> {
const settings = await SettingsFactory.createFromConfigFile();
protected static settingsInstance: Settings | null = null;

public static async initSettings(): Promise<void> {
Ray.settingsInstance = await SettingsFactory.createFromConfigFile();
}

public static create(client: Client | null = null, uuid: string | null = null): Ray {
const settings =
Ray.settingsInstance ??
new Settings({
host: 'localhost',
port: 23517,
enable: true,
always_send_raw_values: false,
});

return new this(settings, client, uuid);
}
Expand Down Expand Up @@ -88,5 +102,5 @@ export class Ray extends BaseRay {
}

export const ray = (...args: any[]) => {
return Ray.create().then(r => r.send(...args));
return Ray.create().send(...args);
};
10 changes: 5 additions & 5 deletions src/Settings/SettingsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class SettingsFactory {
}

public async getSettingsFromConfigFile(configDirectory: string | null = null) {
const configFilePath = await this.searchConfigFiles(configDirectory);
const configFilePath = this.searchConfigFiles(configDirectory);

if (!(await exists(configFilePath))) {
return {};
Expand All @@ -43,20 +43,20 @@ export class SettingsFactory {
return options as RaySettings;
}

protected async searchConfigFiles(configDirectory: string | null = null): Promise<string> {
protected searchConfigFiles(configDirectory: string | null = null): string {
if (configDirectory === null) {
configDirectory = '';
}

if (typeof this.cache[configDirectory] === 'undefined') {
this.cache[configDirectory] = await this.searchConfigFilesOnDisk(configDirectory);
this.cache[configDirectory] = this.searchConfigFilesOnDisk(configDirectory);
}

return this.cache[configDirectory];
}

protected async searchConfigFilesOnDisk(configDirectory: string | null = null): Promise<string> {
const configFn = await findUp('ray.config.js', {
protected searchConfigFilesOnDisk(configDirectory: string | null = null): string {
const configFn = findUp('ray.config.js', {
type: 'file',
cwd: configDirectory ?? process.cwd(),
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/findUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function findUpMultipleSync(name, options: any = {}) {
return matches;
}

export async function findUp(name, options: any = {}) {
export function findUp(name, options: any = {}) {
const matches = findUpMultipleSync(name, { ...options, limit: 1 });
return matches[0];
}

0 comments on commit 411c54f

Please sign in to comment.