Last active
May 11, 2024 03:49
-
-
Save subtleGradient/b7dab8aafb5e575d171c9d9e82526f0a to your computer and use it in GitHub Desktop.
Hot reloading disposal events
This file contains hidden or 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
/* | |
MIT No Attribution | |
Copyright 2024 Thomas Aylott | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, | |
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | |
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
type RefObject<T> = { readonly current: T } | |
declare global { | |
var hotness: undefined | RefObject<Hotness> | |
} | |
type HotDisposable<T> = T & { | |
[Symbol.dispose](this: T, last: T, next: T): void | |
} | |
export class Hotness { | |
static version = "hotness 2.13" | |
version = Hotness.version | |
count = -1 | |
disposables = new Map<string, HotDisposable<any>>() | |
listeners: (null | (() => void))[] = []; | |
[Symbol.dispose]() { | |
this.listeners.forEach((it, i, them) => ((them[i] = null), it?.())) | |
this.listeners = [] | |
} | |
using<T extends Record<string, HotDisposable<any>>>(meta: ImportMeta, map: T): T { | |
for (const [id, current] of Object.entries(map)) this.usingOne(meta, id, current) | |
return map | |
} | |
usingOne<T extends HotDisposable<any>>({ filename }: ImportMeta, id: string, current: T): T { | |
const key = `${filename}#${id}` | |
const old = this.disposables.get(key) | |
this.disposables.set(key, current) | |
try { | |
old?.[Symbol.dispose]?.(old, current) | |
} catch (error) { | |
console.warn("Hotness.using", "error disposing", { key, error }) | |
} | |
return current | |
} | |
static get current(): Hotness { | |
const old = globalThis.hotness?.current | |
if (old?.version === Hotness.version) return old | |
globalThis.hotness = { current: new Hotness() } | |
return globalThis.hotness!.current | |
} | |
static onHotReload(listener: (old: Hotness, current: Hotness) => void): void { | |
const old = { ...Hotness.current } as Hotness | |
Hotness.current.listeners.push(() => listener(old, Hotness.current)) | |
} | |
} | |
Hotness.current.count++ | |
Hotness.current[Symbol.dispose]() | |
if (import.meta.main) { | |
console.log(`<TEST id=${Hotness.current.count}>`, import.meta.filename) | |
setImmediate(() => Hotness.onHotReload(({ count }) => console.log(`</TEST id=${count}>\n`))) | |
const testObject: HotDisposable<{ id: string }> = { | |
id: new Date().toLocaleTimeString() + " " + Hotness.current.count, | |
[Symbol.dispose]: function ({ id: OLD }, { id: NEW }): void { | |
console.log("Hot reloaded! disposed of old object.", { OLD, NEW }) | |
}, | |
} | |
Hotness.current.using(import.meta, { testObject }) | |
Hotness.onHotReload(({ count, version }) => { | |
console.log("onHotReload, Hot reloaded! ran once listener. ", { count, version }) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment