How to use the @hpcc-js/util.AsyncCache function in @hpcc-js/util

To help you get started, we’ve selected a few @hpcc-js/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hpcc-systems / Visualization / tests / test-util / src / cache.spec.ts View on Github external
val: string = "";

            constructor(id: string, id2: string) {
                this.id = id;
            }

            async refresh(): Promise {
                return new Promise(resolve => {
                    setTimeout(() => {
                        this.val = "done";
                        resolve("done");
                    }, 1000);
                });
            }
        }
        const myCache = new AsyncCache<{ id: string }, MyClass>((obj: { id: string; }) => {
            return Cache.hash([obj.id]);
        });
        expect(myCache.has({ id: "007" })).is.false;
        let callbackCount = 0;
        const promises = [];
        for (let i = 0; i < 10; ++i) {
            promises.push(myCache.get({ id: "007" }, async () => {
                const retVal = new MyClass("007", "a");
                await retVal.refresh();
                ++callbackCount;
                return retVal;
            }));
        }
        expect(callbackCount).to.equal(0);
        expect(myCache.has({ id: "007" })).is.true;
        expect(myCache.has({ id: "008" })).is.false;