mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 22:03:36 +00:00
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:
@@ -8,9 +8,12 @@ describe("sessionSync decorator", () => {
|
|||||||
class TestClass {
|
class TestClass {
|
||||||
@sessionSync({ ctor: ctor, initializer: initializer })
|
@sessionSync({ ctor: ctor, initializer: initializer })
|
||||||
private testProperty = new BehaviorSubject("");
|
private testProperty = new BehaviorSubject("");
|
||||||
|
@sessionSync({ ctor: ctor, initializer: initializer, initializeAs: "array" })
|
||||||
|
private secondTestProperty = new BehaviorSubject("");
|
||||||
|
|
||||||
complete() {
|
complete() {
|
||||||
this.testProperty.complete();
|
this.testProperty.complete();
|
||||||
|
this.secondTestProperty.complete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,11 +22,40 @@ describe("sessionSync decorator", () => {
|
|||||||
expect((testClass as any).__syncedItemMetadata).toEqual([
|
expect((testClass as any).__syncedItemMetadata).toEqual([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
propertyKey: "testProperty",
|
propertyKey: "testProperty",
|
||||||
sessionKey: "TestClass_testProperty",
|
sessionKey: "testProperty_0",
|
||||||
ctor: ctor,
|
ctor: ctor,
|
||||||
initializer: initializer,
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ class BuildOptions<T, TJson = Jsonify<T>> {
|
|||||||
initializeAs?: InitializeOptions;
|
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.
|
* 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({
|
p.__syncedItemMetadata.push({
|
||||||
propertyKey,
|
propertyKey,
|
||||||
sessionKey: `${prototype.constructor.name}_${propertyKey}`,
|
sessionKey: `${propertyKey}_${index++}`,
|
||||||
ctor: buildOptions.ctor,
|
ctor: buildOptions.ctor,
|
||||||
initializer: buildOptions.initializer,
|
initializer: buildOptions.initializer,
|
||||||
initializeAs: buildOptions.initializeAs ?? "object",
|
initializeAs: buildOptions.initializeAs ?? "object",
|
||||||
|
|||||||
Reference in New Issue
Block a user