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

Add initSettings() to NodeRay #269

Merged
merged 2 commits into from
Mar 29, 2024
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
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 { 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();
}

Check warning on line 21 in src/RayNode.ts

View check run for this annotation

Codecov / codecov/patch

src/RayNode.ts#L20-L21

Added lines #L20 - L21 were not covered by tests

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 const ray = (...args: any[]) => {
return Ray.create().then(r => r.send(...args));
return Ray.create().send(...args);

Check warning on line 105 in src/RayNode.ts

View check run for this annotation

Codecov / codecov/patch

src/RayNode.ts#L105

Added line #L105 was not covered by tests
};
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];
}