Skip to content

Commit

Permalink
[scoped apis] Add scoped APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowsink committed Feb 2, 2024
1 parent 3d73ddb commit efcaae6
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 12 deletions.
File renamed without changes.
@@ -1,9 +1,9 @@
import { devmodePrivateApis, installedPlugins, loadedPlugins, removePlugin, startPlugin, stopPlugin } from "./plugins";
import { observe } from "./observer";
import { devmodePrivateApis, installedPlugins, loadedPlugins, removePlugin, startPlugin, stopPlugin } from "../plugins";
import { observe } from "../observer";
import { injectCss, ModalBody, ModalHeader, ModalRoot, ModalSizes, openModal } from "shelter-ui";
import { css, classes } from "./devmode.css";
import { log } from "./util";
import DevUI from "./components/DevUI";
import { log } from "../util";
import DevUI from "../components/DevUI";

// any string would work here but this is funnier
export const devModeReservedId = "__DEVMODE_PLUGIN_DO_NOT_USE_OR_YOU_WILL_BE_FIRED";
Expand Down
@@ -1,6 +1,6 @@
import { blockedSym, getDispatcher, modifiedSym } from "./flux";
import { log } from "./util";
import { dbStore, defaults } from "./storage";
import { blockedSym, getDispatcher, modifiedSym } from ".";
import { log } from "../util";
import { dbStore, defaults } from "../storage";

defaults(dbStore, { logDispatch: false });

Expand Down
@@ -1,5 +1,5 @@
import { Dispatcher, FluxStore } from "./types";
import exfiltrate from "./exfiltrate";
import { Dispatcher, FluxStore } from "../types";
import exfiltrate from "../exfiltrate";

declare global {
interface Object {
Expand Down Expand Up @@ -168,3 +168,5 @@ export async function awaitStore(name: string, awaitInit = true): Promise<FluxSt
if (awaitInit) await awaitStoreInit(store);
return store;
}

export * from "./dispatchLogger";
2 changes: 1 addition & 1 deletion packages/shelter/src/index.ts
Expand Up @@ -3,7 +3,7 @@ import * as ui from "shelter-ui";
import * as util from "./util";
import * as plugins from "./plugins";
import { initSettings, removeAllSections } from "./settings";
import { initDispatchLogger } from "./dispatchLogger";
import { initDispatchLogger } from "./flux";
import { unobserve } from "./observer";
import windowApi from "./windowApi";
import { sleep } from "./util";
Expand Down
@@ -1,6 +1,6 @@
import { batch, createSignal, onCleanup } from "solid-js";
import { getDispatcher, intercept } from "./flux";
import { Fiber, FluxStore, FiberOwner } from "./types";
import { getDispatcher, intercept } from "../flux";
import { Fiber, FluxStore, FiberOwner } from "../types";

declare global {
interface Element {
Expand Down Expand Up @@ -101,3 +101,5 @@ export function createSubscription<TState, TStoreData = Record<string, any>>(
export const storeAssign = <T>(store: T, toApply: T) => batch(() => Object.assign(store, toApply));

export const sleep = (ms = 0) => new Promise((r) => setTimeout(r, ms));

export * from "./scopedApi";
63 changes: 63 additions & 0 deletions packages/shelter/src/util/scopedApi.ts
@@ -0,0 +1,63 @@
import { log } from ".";
import { intercept as fluxIntercept } from "../flux";
import { intercept as httpIntercept } from "../http";
import { observe as observeDom } from "../observer";
import * as patcher from "spitroast";
import { registerSection } from "../settings";
import { injectCss } from "shelter-ui";
import { Dispatcher } from "../types";

export type DisposableFn = (...props: unknown[]) => () => void;
function shimDisposableFn<F extends DisposableFn>(unpatches: (() => void)[], f: F) {
return (...props: Parameters<F>) => {
const up = f(...props);
unpatches.push(up);
return up;
};
}

export function createScopedApi(dispatcher: Dispatcher) {
const disposes = [];

return {
disposes,

disposeAllNow() {
for (const p of disposes) {
try {
p();
} catch (e) {
log([`Caught error while cleaning up scoped APIs, continuing safely.`, e], "error");
}
}
disposes.splice(0, disposes.length);
},

onDispose(callback: DisposableFn) {
disposes.push(callback);
},

flux: {
subscribe(type: string, cb: (payload: any) => void) {
dispatcher.subscribe(type, cb);
disposes.push(() => dispatcher.unsubscribe(type, cb));
},
intercept: shimDisposableFn(disposes, fluxIntercept),
},
http: {
intercept: shimDisposableFn(disposes, httpIntercept),
},
observeDom: shimDisposableFn(disposes, observeDom),
patcher: {
after: shimDisposableFn(disposes, patcher.after),
before: shimDisposableFn(disposes, patcher.before),
instead: shimDisposableFn(disposes, patcher.instead),
},
settings: {
registerSection: shimDisposableFn(disposes, registerSection),
},
ui: {
injectCss: shimDisposableFn(disposes, injectCss),
},
} as const;
}
1 change: 1 addition & 0 deletions packages/shelter/src/windowApi.ts
Expand Up @@ -31,6 +31,7 @@ const windowApi = async (unloads) => ({
"getDispatcher",
"blockedSym",
"modifiedSym",
"initDispatchLogger",
),
get http(): HTTPApi {
if (discordHttp === undefined) {
Expand Down

0 comments on commit efcaae6

Please sign in to comment.