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

feat: added custom provider support #83

Closed
wants to merge 2 commits into from
Closed
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 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
111 changes: 111 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,111 @@
import {
createSignal,
onCleanup,
onMount,
runWithOwner,
type Owner,
} from 'solid-js';

import type { ElementContext } from '~/index';
import {
getScriptManager,
type CustomProviderConfig,
} from '~/user-config';
import { isEventTarget, 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 [state, setState] = createSignal<unknown>();
const scriptManager = getScriptManager();

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

if ('emitter_fn_path' in config) {
const eventTarget = await scriptManager.callFn(
config.emitter_fn_path,
new Event('custom'),
elementContext,
);

if (!isEventTarget(eventTarget))
throw new TypeError(`Emitter function must return an event target.`);

const listener = (event: Event) => {
if (event.type !== 'value') return;

if (!('value' in event)) return;

setState(event.value);
};

runWithOwner(owner, () => {
onMount(() => eventTarget.addEventListener('value', listener));
onCleanup(async () => {
eventTarget.removeEventListener('value', listener);

if (config.stop_fn_path)
await scriptManager.callFn(
config.stop_fn_path,
new Event('custom'),
elementContext,
);
});
});
} else {
// 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,24 @@
import { z } from 'zod';

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

export const CustomProviderConfigSchema = z.union([
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(),
}),
z.object({
type: z.literal(ProviderType.CUSTOM),
start_fn_path: FnPathSchema.optional(),
emitter_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
1 change: 1 addition & 0 deletions packages/client-api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export * from './create-getter-proxy';
export * from './create-logger';
export * from './create-string-scanner';
export * from './get-coordinate-distance';
export * from './is-event-target';
export * from './simple-hash';
export * from './to-css-selector';
3 changes: 3 additions & 0 deletions packages/client-api/src/utils/is-event-target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isEventTarget(obj: any): obj is EventTarget {
return obj && typeof obj.addEventListener === 'function';
}