Skip to content

Commit

Permalink
feat(providers): added custom provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
thearturca committed Aug 14, 2024
1 parent f16a173 commit 1f52a24
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/client-api/src/providers/create-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Owner } from 'solid-js';

import { createBatteryProvider } from './battery/create-battery-provider';
import { createCpuProvider } from './cpu/create-cpu-provider';
import { createCustomProvider } from './custom/create-custom-provider';
import { createDateProvider } from './date/create-date-provider';
import { createGlazeWmProvider } from './glazewm/create-glazewm-provider';
import { createHostProvider } from './host/create-host-provider';
Expand Down Expand Up @@ -30,6 +31,8 @@ export async function createProvider(
return createBatteryProvider(config, owner);
case ProviderType.CPU:
return createCpuProvider(config, owner);
case ProviderType.CUSTOM:
return createCustomProvider(config, elementContext, owner);
case ProviderType.DATE:
return createDateProvider(config, owner);
case ProviderType.GLAZEWM:
Expand Down
78 changes: 78 additions & 0 deletions packages/client-api/src/providers/custom/create-custom-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
createSignal,
onCleanup,
runWithOwner,
type Owner,
} from 'solid-js';

import type { ElementContext } from '~/index';
import {
getScriptManager,
type CustomProviderConfig,
} from '~/user-config';
import type { PickPartial } from '~/utils';

export interface CustomState {
state: unknown;
}

export async function createCustomProvider(
config: CustomProviderConfig,
elementContext: PickPartial<
ElementContext,
'parsedConfig' | 'providers'
>,
owner: Owner,
): Promise<CustomState> {
const scriptManager = getScriptManager();

if (config.start_fn_path)
await scriptManager.callFn(
config.start_fn_path,
new Event('custom'),
elementContext,
);

const [state, setState] = createSignal<unknown>();

// run refresh fn first time to set initial state
setState(
await scriptManager.callFn(
config.refresh_fn_path,
new Event('custom'),
elementContext,
),
);

// and then every refresh interval
const interval = setInterval(
async () =>
setState(
await scriptManager.callFn(
config.refresh_fn_path,
new Event('custom'),
elementContext,
),
),
config.refresh_interval,
);

runWithOwner(owner, () => {
onCleanup(async () => {
clearInterval(interval);

if (config.stop_fn_path)
await scriptManager.callFn(
config.stop_fn_path,
new Event('custom'),
elementContext,
);
});
});

return {
get state() {
return state();
},
};
}
1 change: 1 addition & 0 deletions packages/client-api/src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './battery/create-battery-provider';
export * from './cpu/create-cpu-provider';
export * from './custom/create-custom-provider';
export * from './date/create-date-provider';
export * from './glazewm/create-glazewm-provider';
export * from './ip/create-ip-provider';
Expand Down
4 changes: 2 additions & 2 deletions packages/client-api/src/user-config/get-script-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { convertFileSrc } from '@tauri-apps/api/core';
import { join, homeDir } from '@tauri-apps/api/path';

import { createLogger } from '~/utils';
import { createLogger, type PickPartial } from '~/utils';
import type { ElementContext } from '../element-context.model';

const logger = createLogger('script-manager');
Expand Down Expand Up @@ -34,7 +34,7 @@ async function loadScriptForFn(fnPath: string): Promise<any> {
async function callFn(
fnPath: string,
event: Event,
context: ElementContext,
context: PickPartial<ElementContext, 'parsedConfig' | 'providers'>,
): Promise<any> {
const { modulePath, functionName } = parseFnPath(fnPath);
const foundModule = await resolveModule(modulePath);
Expand Down
8 changes: 8 additions & 0 deletions packages/client-api/src/user-config/shared/fn-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';

export const FnPathSchema = z
.string()
.regex(
/^(.+)#([a-zA-Z_$][a-zA-Z0-9_$]*)$/,
"Invalid function path. Needs to be in format 'path/to/my-script.js#functionName'.",
);
1 change: 1 addition & 0 deletions packages/client-api/src/user-config/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './boolean-like.model';
export * from './fn-path';
export * from './get-child-configs';
export * from './with-dynamic-key';
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';
import { FnPathSchema } from '../shared';
import type { Prettify } from '~/utils';

/**
Expand Down Expand Up @@ -108,12 +109,7 @@ export const ElementEventsConfigSchema = z
.array(
z.object({
type: z.enum(HTML_EVENTS),
fn_path: z
.string()
.regex(
/^(.+)#([a-zA-Z_$][a-zA-Z0-9_$]*)$/,
"Invalid function path. Needs to be in format 'path/to/my-script.js#functionName'.",
),
fn_path: FnPathSchema,
selector: z.string().optional(),
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Prettify } from '~/utils';
import {
BatteryProviderConfigSchema,
CpuProviderConfigSchema,
CustomProviderConfigSchema,
DateProviderConfigSchema,
GlazeWmProviderConfigSchema,
HostProviderConfigSchema,
Expand All @@ -20,6 +21,7 @@ import {
export const ProviderConfigSchema = z.union([
BatteryProviderConfigSchema,
CpuProviderConfigSchema,
CustomProviderConfigSchema,
DateProviderConfigSchema,
GlazeWmProviderConfigSchema,
HostProviderConfigSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { z } from 'zod';
export enum ProviderType {
BATTERY = 'battery',
CPU = 'cpu',
CUSTOM = 'custom',
DATE = 'date',
GLAZEWM = 'glazewm',
HOST = 'host',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { z } from 'zod';

import { ProviderType } from '../provider-type.model';
import { FnPathSchema } from '~/user-config/shared';

export const CustomProviderConfigSchema = z.object({
type: z.literal(ProviderType.CUSTOM),
refresh_interval: z.coerce.number().default(5 * 1000),
start_fn_path: FnPathSchema.optional(),
refresh_fn_path: FnPathSchema,
stop_fn_path: FnPathSchema.optional(),
});

export type CustomProviderConfig = z.infer<
typeof CustomProviderConfigSchema
>;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './battery-provider-config.model';
export * from './cpu-provider-config.model';
export * from './custom-provider.config.model';
export * from './date-provider-config.model';
export * from './glazewm-provider-config.model';
export * from './host-provider-config.model';
Expand Down

0 comments on commit 1f52a24

Please sign in to comment.