Как реализовать синглтон Javascript для кеширования коллекции mon go? - PullRequest
0 голосов
/ 11 июля 2020

Я создал следующий синглтон, который будет использоваться в качестве кеша в памяти для коллекций mon go:

class CollectionStore {
    constructor(context) {
        console.log("Initializing collection store");

        this._data = {};
        this._context = context;
    }

    get(collection, id) {
        if (!this._data[collection])
            this.reset(collection).then(() => {
                return _getItem(collection, id);
            });
        else return _getItem(collection, id);
    }

    _getItem(collection, id) {
        console.log(
            "Getting item " + id + " from store collection " + collection
        );
        return this._data[collection].find((d) => d._id === id);
    }

    reset(collection) {
        console.log("Resetting store collection " + collection);
        mongoFind(this._context, collection).then((result) => {
            this._data[collection] = result;
        });
    }
}

const instance = new CollectionStore();
Object.freeze(instance);

export default instance;

mongoFind - внутренняя функция, которая извлекает полную коллекцию itens.

При создании экземпляра магазина я получаю следующую ошибку:

import { CollectionStore } from "../cache";

let context = {....} // Some access control stuff

let stores: {
    collectionStore: new CollectionStore(context) ** ERROR:  _cache.CollectionStore is not a constructor **
}

Есть идеи, что я делаю не так и как исправить?

...