Skip to content

Commit

Permalink
feat(serialization): allows to pass extra classes to bootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <toomas2d@gmail.com>
  • Loading branch information
Tomas2D committed Oct 10, 2024
1 parent 7632ed3 commit 60f8953
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/internals/serializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ interface SerializableStructure<T> {
snapshot: T;
}

interface DeserializeOptions {
extraClasses?: SerializableClass<unknown>[];
}

export abstract class Serializable<T = unknown> {
abstract createSnapshot(): T;
abstract loadSnapshot(snapshot: T): void;
Expand Down Expand Up @@ -58,8 +62,11 @@ export abstract class Serializable<T = unknown> {
});
}

protected deserialize(value: string): T {
const { __root } = Serializer.deserializeWithMeta<SerializableStructure<T>>(value);
protected deserialize(value: string, options?: DeserializeOptions): T {
const { __root } = Serializer.deserializeWithMeta<SerializableStructure<T>>(
value,
options?.extraClasses,
);
if (!__root.target) {
// eslint-disable-next-line
console.warn(
Expand Down Expand Up @@ -90,9 +97,10 @@ export abstract class Serializable<T = unknown> {
static fromSerialized<T extends Serializable>(
this: abstract new (...args: any[]) => T,
serialized: string,
options: DeserializeOptions = {},
): PromiseOrPlain<T, T["loadSnapshot"]> {
const target = Object.create(this.prototype) as T;
const state = target.deserialize(serialized);
const state = target.deserialize(serialized, options);
const load = target.loadSnapshot(state);
Cache.init(target);

Expand Down

0 comments on commit 60f8953

Please sign in to comment.