Skip to content

Commit 07606e3

Browse files
AndrewKushnirPawel Kozlowski
authored andcommitted
feat(platform-browser): add isEmpty method to the TransferState class (#46915)
This commit adds the `isEmpty` method to the `TransferState` class to make it possible to check whether the state is empty or not. This is helpful in situations when the `TransferState` should be serialized and the content is transferred to the client (if the state is empty - certain operations can be omitted). PR Close #46915
1 parent afa6050 commit 07606e3

File tree

5 files changed

+22
-5
lines changed

5 files changed

+22
-5
lines changed

goldens/public-api/platform-browser/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export class Title {
228228
export class TransferState {
229229
get<T>(key: StateKey<T>, defaultValue: T): T;
230230
hasKey<T>(key: StateKey<T>): boolean;
231+
get isEmpty(): boolean;
231232
onSerialize<T>(key: StateKey<T>, callback: () => T): void;
232233
remove<T>(key: StateKey<T>): void;
233234
set<T>(key: StateKey<T>, value: T): void;

integration/platform-server/src/transferstate/app.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {TransferStateComponent} from './transfer-state.component';
1616
bootstrap: [TransferStateComponent],
1717
imports: [
1818
BrowserModule.withServerTransition({appId: 'ts'}),
19-
BrowserTransferStateModule,
2019
],
2120
})
2221
export class TransferStateModule {

packages/platform-browser/src/browser/transfer_state.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ export class TransferState {
125125
return this.store.hasOwnProperty(key);
126126
}
127127

128+
/**
129+
* Indicates whether the state is empty.
130+
*/
131+
get isEmpty(): boolean {
132+
return Object.keys(this.store).length === 0;
133+
}
134+
128135
/**
129136
* Register a callback to provide the value for a key when `toJson` is called.
130137
*/

packages/platform-browser/test/browser/transfer_state_spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('TransferState', () => {
4444
TestBed.configureTestingModule({
4545
imports: [
4646
BrowserModule.withServerTransition({appId: APP_ID}),
47-
BrowserTransferStateModule,
4847
]
4948
});
5049
doc = TestBed.inject(DOCUMENT);
@@ -117,6 +116,19 @@ describe('TransferState', () => {
117116

118117
expect(transferState.toJson()).toBe('{"test":20,"delayed":"changed"}');
119118
});
119+
120+
it('should provide an ability to detect whether the state is empty', () => {
121+
const transferState = TestBed.inject(TransferState);
122+
123+
// The state is empty initially.
124+
expect(transferState.isEmpty).toBeTrue();
125+
126+
transferState.set(TEST_KEY, 20);
127+
expect(transferState.isEmpty).toBeFalse();
128+
129+
transferState.remove(TEST_KEY);
130+
expect(transferState.isEmpty).toBeTrue();
131+
});
120132
});
121133

122134
describe('escape/unescape', () => {

packages/platform-server/src/transfer_state.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ export const TRANSFER_STATE_SERIALIZATION_PROVIDERS: Provider[] = [{
2121

2222
function serializeTransferStateFactory(doc: Document, appId: string, transferStore: TransferState) {
2323
return () => {
24-
const store = (transferStore as unknown as {store: {}}).store;
25-
const isStateEmpty = Object.keys(store).length === 0;
26-
if (isStateEmpty) {
24+
if (transferStore.isEmpty) {
2725
// The state is empty, nothing to transfer,
2826
// avoid creating an extra `<script>` tag in this case.
2927
return;

0 commit comments

Comments
 (0)