Skip to content

Commit 411c54f

Browse files
authored
Merge pull request #269 from permafrost-dev/nodejs-sync
Add initSettings() to NodeRay
2 parents f10a743 + cc8bbfb commit 411c54f

4 files changed

Lines changed: 26 additions & 9 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ Ray.useDefaultSettings({ port: 3000 });
149149
// ...and use ray() as normal
150150
```
151151

152+
**When using NodeJS,** you must call `await Ray.initSettings()` to initialize the settings before using `ray()`.
153+
This is not necessary when using the browser bundle.
154+
152155
```js
153156
ray('a string');
154157

src/RayNode.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@ import { ImagePayload } from '@/Payloads/ImagePayload';
77
import { NodeInfoPayload } from '@/Payloads/NodeInfoPayload';
88
import { NodeMeasurePayload } from '@/Payloads/NodeMeasurePayload';
99
import { Ray as BaseRay } from '@/Ray';
10+
import { Settings } from '@/Settings/Settings';
1011
import { SettingsFactory } from '@/Settings/SettingsFactory';
1112
import { NodeStopwatch } from '@/Stopwatch/NodeStopwatch';
1213
import { existsSync } from 'node:fs';
1314

1415
// @ts-ignore
1516
export class Ray extends BaseRay {
16-
public static async create(client: Client | null = null, uuid: string | null = null): Promise<Ray> {
17-
const settings = await SettingsFactory.createFromConfigFile();
17+
protected static settingsInstance: Settings | null = null;
18+
19+
public static async initSettings(): Promise<void> {
20+
Ray.settingsInstance = await SettingsFactory.createFromConfigFile();
21+
}
22+
23+
public static create(client: Client | null = null, uuid: string | null = null): Ray {
24+
const settings =
25+
Ray.settingsInstance ??
26+
new Settings({
27+
host: 'localhost',
28+
port: 23517,
29+
enable: true,
30+
always_send_raw_values: false,
31+
});
1832

1933
return new this(settings, client, uuid);
2034
}
@@ -88,5 +102,5 @@ export class Ray extends BaseRay {
88102
}
89103

90104
export const ray = (...args: any[]) => {
91-
return Ray.create().then(r => r.send(...args));
105+
return Ray.create().send(...args);
92106
};

src/Settings/SettingsFactory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class SettingsFactory {
2525
}
2626

2727
public async getSettingsFromConfigFile(configDirectory: string | null = null) {
28-
const configFilePath = await this.searchConfigFiles(configDirectory);
28+
const configFilePath = this.searchConfigFiles(configDirectory);
2929

3030
if (!(await exists(configFilePath))) {
3131
return {};
@@ -43,20 +43,20 @@ export class SettingsFactory {
4343
return options as RaySettings;
4444
}
4545

46-
protected async searchConfigFiles(configDirectory: string | null = null): Promise<string> {
46+
protected searchConfigFiles(configDirectory: string | null = null): string {
4747
if (configDirectory === null) {
4848
configDirectory = '';
4949
}
5050

5151
if (typeof this.cache[configDirectory] === 'undefined') {
52-
this.cache[configDirectory] = await this.searchConfigFilesOnDisk(configDirectory);
52+
this.cache[configDirectory] = this.searchConfigFilesOnDisk(configDirectory);
5353
}
5454

5555
return this.cache[configDirectory];
5656
}
5757

58-
protected async searchConfigFilesOnDisk(configDirectory: string | null = null): Promise<string> {
59-
const configFn = await findUp('ray.config.js', {
58+
protected searchConfigFilesOnDisk(configDirectory: string | null = null): string {
59+
const configFn = findUp('ray.config.js', {
6060
type: 'file',
6161
cwd: configDirectory ?? process.cwd(),
6262
});

src/lib/findUp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function findUpMultipleSync(name, options: any = {}) {
7575
return matches;
7676
}
7777

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

0 commit comments

Comments
 (0)