mirror of
https://github.com/jlfwong/speedscope.git
synced 2024-11-27 01:53:17 +03:00
404aacfa46
* Switch to `bracketSpacing: false`. * Add prettier-ignore in one case.
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import {Component} from 'preact'
|
|
|
|
export interface SerializedComponent<S> {
|
|
state: S
|
|
serializedSubcomponents: {[key: string]: any}
|
|
}
|
|
|
|
export abstract class ReloadableComponent<P, S> extends Component<P, S> {
|
|
serialize(): SerializedComponent<S> {
|
|
const serializedSubcomponents: {[key: string]: any} = Object.create(null)
|
|
|
|
const subcomponents = this.subcomponents()
|
|
for (const key in subcomponents) {
|
|
const val = subcomponents[key]
|
|
if (val && val instanceof ReloadableComponent) {
|
|
serializedSubcomponents[key] = val.serialize()
|
|
}
|
|
}
|
|
|
|
return {
|
|
state: this.state,
|
|
serializedSubcomponents,
|
|
}
|
|
}
|
|
rehydrate(serialized: SerializedComponent<S>) {
|
|
this.setState(serialized.state, () => {
|
|
const subcomponents = this.subcomponents()
|
|
for (const key in subcomponents) {
|
|
const val = subcomponents[key]
|
|
const data = serialized.serializedSubcomponents[key]
|
|
if (data && val && val instanceof ReloadableComponent) {
|
|
val.rehydrate(data)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
subcomponents(): {[key: string]: any} {
|
|
return Object.create(null)
|
|
}
|
|
}
|