These are chat archives for canjs/canjs

28th
Jul 2018
Justin Meyer
@justinbmeyer
Jul 28 2018 01:41
@ivospinheiro please create an issue, I think @phillipskevin might be able to take a look monday
Justin Meyer
@justinbmeyer
Jul 28 2018 01:47
@Lighttree yeah, batching will work best ... though the new queues will work better likely
in CanJS 4
Viktor Busko
@Lighttree
Jul 28 2018 12:38

@justinbmeyer not sure that I got how to accomplish what I was talking above.

I have singleton service that shared across app, and it has some defineList that can be mutated in different places.
My expectations that I'll have batches of mutations and would like to add some optimization for this case. This is some kind of.. registry for components ids in order to get translations for them. As soon as they register I need to call some web service to get translated text for them. I expect that most frequent scenario is when N components appears the same time all of them mutate this list at the same time, so if I... lets say wait for 1 sec (magic number :(, but dont know better way ) I'll be able to send 1 request instead of N.

This is example how it works right now. This will generate 2 calls of this.getTranslation(), I'm not sure that understand how to use batch here correctly.

// translation.service.js
class TranslationService {
    constructor() {
        this.translationKeys = new DefineList([]);

        this.translationKeys.on('length', (ev, newVal) => {
            this.getTranslation();
        });
    }

   /**
     * Updates translations keys list avoiding duplications.
     *
     * @param {*} translationKeys pageId keys to record in the store.
     */
    addTranslationKey(translationKeys) {
        translationKeys = Array.isArray(translationKeys) ? translationKeys : [translationKeys];

        translationKeys.forEach((item) => {
            if (this.translationKeys.indexOf(item) === -1) {
                this.translationKeys.push(item);
            }
        });

        return this.translationKeys;
    }
}

export default new TranslationService();

// example1.component.js
import TranslationServiceInstance from './translation.service'

TranslationServiceInstance .addTranslationKey(1111);

// example2.component.js
import TranslationServiceInstance from './translation.service'

TranslationServiceInstance .addTranslationKey(2222);
Viktor Busko
@Lighttree
Jul 28 2018 15:21
One more thing... is it possible to somehow observe cache store ? For example right now I have to have collection of promises to ensure that I get data only when it appears in cache. It is done to avoid cache error that no such data in store. So I rely on these promises... but I think that its better "observe" cache storage and get data from cache only when it available, in this case I won't need to know about promises or any other way how data appears in cache.
        translationService.translationPromises[propKey].then(() => {
            Translation.connection.cacheConnection.getData({
                  name: propKey
            }).then((data) => {
                textNode.nodeValue = data.value;
            });
        });