Do not use object constructors for names (#4149)

* Do not use object constructors for names

Minification was selecting different class names for different instances
of services, which was causing them not to sync properly.

This was happening _only_ in production mode for some reason, perhaps
due to minifying post chunking?

* Add tests for additional synced properties
This commit is contained in:
Matt Gibson 2022-11-29 18:40:39 -05:00 committed by GitHub
parent 4d14508729
commit d4a8e5829a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 3 deletions

View File

@ -8,9 +8,12 @@ describe("sessionSync decorator", () => {
class TestClass {
@sessionSync({ ctor: ctor, initializer: initializer })
private testProperty = new BehaviorSubject("");
@sessionSync({ ctor: ctor, initializer: initializer, initializeAs: "array" })
private secondTestProperty = new BehaviorSubject("");
complete() {
this.testProperty.complete();
this.secondTestProperty.complete();
}
}
@ -19,11 +22,40 @@ describe("sessionSync decorator", () => {
expect((testClass as any).__syncedItemMetadata).toEqual([
expect.objectContaining({
propertyKey: "testProperty",
sessionKey: "TestClass_testProperty",
sessionKey: "testProperty_0",
ctor: ctor,
initializer: initializer,
}),
testClass.complete(),
expect.objectContaining({
propertyKey: "secondTestProperty",
sessionKey: "secondTestProperty_1",
ctor: ctor,
initializer: initializer,
initializeAs: "array",
}),
]);
testClass.complete();
});
class TestClass2 {
@sessionSync({ ctor: ctor, initializer: initializer })
private testProperty = new BehaviorSubject("");
complete() {
this.testProperty.complete();
}
}
it("should maintain sessionKey index count for other test classes", () => {
const testClass = new TestClass2();
expect((testClass as any).__syncedItemMetadata).toEqual([
expect.objectContaining({
propertyKey: "testProperty",
sessionKey: "testProperty_2",
ctor: ctor,
initializer: initializer,
}),
]);
testClass.complete();
});
});

View File

@ -9,6 +9,9 @@ class BuildOptions<T, TJson = Jsonify<T>> {
initializeAs?: InitializeOptions;
}
// Used to ensure uniqueness for each synced observable
let index = 0;
/**
* A decorator used to indicate the BehaviorSubject should be synced for this browser session across all contexts.
*
@ -44,7 +47,7 @@ export function sessionSync<T>(buildOptions: BuildOptions<T>) {
p.__syncedItemMetadata.push({
propertyKey,
sessionKey: `${prototype.constructor.name}_${propertyKey}`,
sessionKey: `${propertyKey}_${index++}`,
ctor: buildOptions.ctor,
initializer: buildOptions.initializer,
initializeAs: buildOptions.initializeAs ?? "object",