Skip to content

Conversation

@spelkey-ucd
Copy link
Contributor

@spelkey-ucd spelkey-ucd commented Oct 4, 2024

Summary

  • auto-registers additional event in store for each lru, with -request-loaded suffix.
  • adds method to service that allows user to emit this event.

Example

class FooService extends BaseService {

  constructor() {
    super();
    this.store = FooStore;
    this.basePath = `${appConfig.apiRoot}/foo`;
  }

  async list(){
    let ido = {action: 'list'};
    let id = payload.getKey(ido);

    await this.checkRequesting(
      id, this.store.data.list,
      () => this.request({
        url : this.basePath,
        checkCached : () => this.store.data.list.get(id),
        onUpdate : resp => this.store.set(
          payload.generate(ido, resp),
          this.store.data.list
        )
      })
    );

    // new method 
    this.emitIfLoaded(id, this.store.data.list);

    return this.store.data.list.get(id);
  }


}

Element then can listen via _onFooListRequestLoaded method

Utility

This is mostly useful when doing code-splitting. If data are retrieved by an element in chunk A, and then chunk B is loaded that has an element that needs that data, it's _onFooListUpdate event won't fire since the data has already been set in the cache.

Although, maybe a better pattern would be to call the callback in the element constructor. e.g.

this._onFooListUpdate(this.FooModel.store.data.list.get(payloadUtils.getKey({action: 'list'}))

@spelkey-ucd spelkey-ucd requested a review from jrmerz October 4, 2024 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants