Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

proposal: StateTransfer #791

Closed
Closed

Description

@Toxicable
  • I'm submitting a ...
- [x] feature request
  • What modules are related to this Issue?
- [x] core
  • What is the current behavior?
    There is no standard API for transferring state between Server and Client

  • What is the expected behavior?
    Should be a standard API for transferring state

After discussing with @alxhub I have 2 possible API's for this.
Both have draft Implementations:

1

  • more boilerplate in constructor
  • less boilerplate after constructor

https://github.com/Toxicable/universal/tree/state-transfer/modules/state-transfer-alt

const COUNTER_STATE = 'COUNTER_STATE'

@NgModule({
  providers: [
    BrowserStateTransferModule,
  ]
})
export class AppModule{}

class MyCounterComponent {
  counter: number;
  constructor(stateManager: StateManager) {
    this.counter = this.stateManager.get(COUNTER_STATE, {counter: 0}).counter;
    this.stateManager.onSerialize(COUNTER_STATE, () => {counter: this.counter});
  }
  click() {
    this.counter++;
  }
}

2

  • less boilerplate in constructor
  • more boilerplate after constructor

https://github.com/Toxicable/universal/tree/state-transfer/modules/ng-transfer-state

const MY_STORE_TOKEN = new InjectionToken('MY_STORE_TOKEN');

@NgModule({
  providers: [
    BrowserStateTransferModule,
    provideStore(MY_STORE_TOKEN, 'my-store')
  ]
})
export class AppModule{}

class MyCounterComponent {
  constructor(
    @Inject(MY_STORE_TOKEN) private store: StateTransferStore<{counter: number}>
  ) {}

  click() {
    const value = this.state.get('counter') || 0;
    this.state.set('counter', value++);
  }

3

This one is an extension of 1 except with the use of decorators to track state changes
POC implementation here https://stackblitz.com/edit/statemanager-using-decorators-a3fnpu?file=app/app.component.ts

@StateTransfer
class MyCounterComponent {
  constructor(
    private stateManager: StateManager
  ) {}

  @StateProperty()
  counter = 0;

  click() {
    counter++;
  }

With both your ServerModule will look like:

@NgModule({
  providers: [
    ServerStateTransferModule,
  ]
})
export class ServerModule{}

Activity

  1. patrickmichalina commented on Aug 31, 2017

    @patrickmichalina
    Contributor

    What is the purpose of the counter?

  2. Toxicable commented on Aug 31, 2017

    @Toxicable
    Author

    @patrickmichalina it's an example of what some state might look like.
    it can be whatever you want in your application, Http calls, user input, hide/show values.
    For Demo purpose it's just a simple value that's incremented on click

  3. mcferren commented on Sep 5, 2017

    @mcferren

    this would work the same on a service injectable as you demonstrate with MyCounterComponent?

  4. Toxicable commented on Sep 5, 2017

    @Toxicable
    Author

    @mcferren yup, it would work the same everywhere

  5. vikerman commented on Sep 6, 2017

    @vikerman
    Contributor

    We will get something similar into core angular. Will update here with a design doc soon

  6. Toxicable commented on Sep 22, 2017

    @Toxicable
    Author
  7. angular-automatic-lock-bot commented on Sep 4, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Sep 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions