Skip to content

Commit

Permalink
feat(platform-browser): add isEmpty method to the TransferState c…
Browse files Browse the repository at this point in the history
…lass (#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
  • Loading branch information
AndrewKushnir authored and Pawel Kozlowski committed Jul 22, 2022
1 parent afa6050 commit 07606e3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions goldens/public-api/platform-browser/index.md
Expand Up @@ -228,6 +228,7 @@ export class Title {
export class TransferState {
get<T>(key: StateKey<T>, defaultValue: T): T;
hasKey<T>(key: StateKey<T>): boolean;
get isEmpty(): boolean;
onSerialize<T>(key: StateKey<T>, callback: () => T): void;
remove<T>(key: StateKey<T>): void;
set<T>(key: StateKey<T>, value: T): void;
Expand Down
1 change: 0 additions & 1 deletion integration/platform-server/src/transferstate/app.ts
Expand Up @@ -16,7 +16,6 @@ import {TransferStateComponent} from './transfer-state.component';
bootstrap: [TransferStateComponent],
imports: [
BrowserModule.withServerTransition({appId: 'ts'}),
BrowserTransferStateModule,
],
})
export class TransferStateModule {
Expand Down
7 changes: 7 additions & 0 deletions packages/platform-browser/src/browser/transfer_state.ts
Expand Up @@ -125,6 +125,13 @@ export class TransferState {
return this.store.hasOwnProperty(key);
}

/**
* Indicates whether the state is empty.
*/
get isEmpty(): boolean {
return Object.keys(this.store).length === 0;
}

/**
* Register a callback to provide the value for a key when `toJson` is called.
*/
Expand Down
14 changes: 13 additions & 1 deletion packages/platform-browser/test/browser/transfer_state_spec.ts
Expand Up @@ -44,7 +44,6 @@ describe('TransferState', () => {
TestBed.configureTestingModule({
imports: [
BrowserModule.withServerTransition({appId: APP_ID}),
BrowserTransferStateModule,
]
});
doc = TestBed.inject(DOCUMENT);
Expand Down Expand Up @@ -117,6 +116,19 @@ describe('TransferState', () => {

expect(transferState.toJson()).toBe('{"test":20,"delayed":"changed"}');
});

it('should provide an ability to detect whether the state is empty', () => {
const transferState = TestBed.inject(TransferState);

// The state is empty initially.
expect(transferState.isEmpty).toBeTrue();

transferState.set(TEST_KEY, 20);
expect(transferState.isEmpty).toBeFalse();

transferState.remove(TEST_KEY);
expect(transferState.isEmpty).toBeTrue();
});
});

describe('escape/unescape', () => {
Expand Down
4 changes: 1 addition & 3 deletions packages/platform-server/src/transfer_state.ts
Expand Up @@ -21,9 +21,7 @@ export const TRANSFER_STATE_SERIALIZATION_PROVIDERS: Provider[] = [{

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

0 comments on commit 07606e3

Please sign in to comment.