-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): added custom provider support
- Loading branch information
1 parent
f16a173
commit 1f52a24
Showing
11 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/client-api/src/providers/custom/create-custom-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'.", | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
packages/client-api/src/user-config/window/providers/custom-provider.config.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters