Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d73ddb
commit efcaae6
Showing
8 changed files
with
80 additions
and
12 deletions.
There are no files selected for viewing
File renamed without changes.
8 changes: 4 additions & 4 deletions
8
packages/shelter/src/devmode.tsx → packages/shelter/src/devmode/index.tsx
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
6 changes: 3 additions & 3 deletions
6
packages/shelter/src/dispatchLogger.ts → packages/shelter/src/flux/dispatchLogger.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
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
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,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; | ||
} |
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