Skip to content

Commit

Permalink
namespace presence types again
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed Jul 4, 2024
1 parent e0f631a commit 92ca9eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
36 changes: 20 additions & 16 deletions src/core/presences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,23 @@ import type { IntoDependencies } from "./ioc";
import type { Emitter } from "./interfaces";

type Status = 'online' | 'idle' | 'invisible' | 'dnd'
type PresenceReduce = (previous: PresenceResult) => PresenceResult;
type PresenceReduce = (previous: Presence.Result) => Presence.Result;


export interface PresenceResult {
status?: Status;
afk?: boolean;
activities?: ActivitiesOptions[];
shardId?: number[];
repeat?: number | [Emitter, string];
onRepeat?: (previous: PresenceResult) => PresenceResult;
}

export const Presence = {
/**
* A small wrapper to provide type inference.
* Create a Presence module which **MUST** be put in a file called presence.(language-extension)
* adjacent to the file where **Sern.init** is CALLED.
*/
module : <T extends (keyof Dependencies)[]>(conf: PresenceConfig<T>) => conf,
module : <T extends (keyof Dependencies)[]>(conf: Presence.Config<T>) => conf,
/**
* Create a Presence body which can be either:
* - once, the presence is activated only once.
* - repeated, per cycle or event, the presence can be changed.
*/
of : (root: Omit<PresenceResult, 'repeat' | 'onRepeat'>) => {
of : (root: Omit<Presence.Result, 'repeat' | 'onRepeat'>) => {
return {
/**
* @example
Expand Down Expand Up @@ -56,9 +49,20 @@ export const Presence = {
};
}
}
export declare namespace Presence {
type Config<T extends (keyof Dependencies)[]> = {
inject?: [...T]
execute: (...v: IntoDependencies<T>) => Presence.Result;

}

export interface Result {
status?: Status;
afk?: boolean;
activities?: ActivitiesOptions[];
shardId?: number[];
repeat?: number | [Emitter, string];
onRepeat?: (previous: Result) => Result;
}
}

export type PresenceConfig <T extends (keyof Dependencies)[]> = {
inject?: [...T]
execute: (...v: IntoDependencies<T>) => PresenceResult;

};
8 changes: 4 additions & 4 deletions src/handlers/presence.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { concatMap, from, interval, of, map, scan, startWith, fromEvent, take } from "rxjs"
import { PresenceConfig, PresenceResult } from "../core/presences";
import { Presence } from "../core/presences";
import { Services } from "../core/ioc";
import assert from "node:assert";
import * as Files from "../core/module-loading";
type SetPresence = (conf: PresenceResult) => Promise<unknown>
type SetPresence = (conf: Presence.Result) => Promise<unknown>

const parseConfig = async (conf: Promise<PresenceResult>) => {
const parseConfig = async (conf: Promise<Presence.Result>) => {
return conf.then(s => {
if('repeat' in s) {
const { onRepeat, repeat } = s;
Expand All @@ -23,7 +23,7 @@ const parseConfig = async (conf: Promise<PresenceResult>) => {

export const presenceHandler = (path: string, setPresence: SetPresence) => {
const presence = Files
.importModule<PresenceConfig<(keyof Dependencies)[]>>(path)
.importModule<Presence.Config<(keyof Dependencies)[]>>(path)
.then(({ module }) => {
//fetch services with the order preserved, passing it to the execute fn
const fetchedServices = Services(...module.inject ?? []);
Expand Down
4 changes: 2 additions & 2 deletions src/sern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import interactionHandler from './handlers/interaction';
import { presenceHandler } from './handlers/presence';
import { handleCrash } from './handlers/event-utils';
import { UnpackedDependencies } from './types/utility';
import type { PresenceResult } from './core/presences';
import type { Presence} from './core/presences';

interface Wrapper {
commands: string;
Expand Down Expand Up @@ -52,7 +52,7 @@ export function init(maybeWrapper: Wrapper = { commands: "./dist/commands" }) {
const time = ((performance.now() - startTime) / 1000).toFixed(2);
deps['@sern/logger']?.info({ message: `sern: registered in ${time} s` });
if(presencePath.exists) {
const setPresence = async (p: PresenceResult) => {
const setPresence = async (p: Presence.Result) => {
return deps['@sern/client'].user?.setPresence(p);
}
presenceHandler(presencePath.path, setPresence).subscribe();
Expand Down

0 comments on commit 92ca9eb

Please sign in to comment.