mobx-keystone-asyncstore
Advanced tools
Comparing version
import { ModelClass, AnyModel, ModelClassDeclaration } from "mobx-keystone"; | ||
import { AsyncStoreOptions } from "./AsyncStore"; | ||
export declare function createAsyncContainer<AModel extends ModelClass<AnyModel>, AProps extends AsyncStoreOptions<InstanceType<AModel>>>(ItemModel: AModel, asyncProps: AProps): ModelClassDeclaration<import("mobx-keystone")._Model<unknown, { | ||
export interface AsyncContainerOptions<T> extends AsyncStoreOptions<T> { | ||
name: string; | ||
} | ||
export declare function createAsyncContainer<AModel extends ModelClass<AnyModel>, AProps extends AsyncContainerOptions<InstanceType<AModel>>>(ItemModel: AModel, asyncProps: AProps): ModelClassDeclaration<import("mobx-keystone")._Model<unknown, { | ||
id: import("mobx-keystone").MaybeOptionalModelProp<string, string>; | ||
@@ -5,0 +8,0 @@ _value: import("mobx-keystone").MaybeOptionalModelProp<any, any>; |
@@ -8,4 +8,8 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createAsyncContainer = void 0; | ||
const debug_1 = __importDefault(require("debug")); | ||
const mobx_keystone_1 = require("mobx-keystone"); | ||
@@ -15,3 +19,3 @@ const mobx_1 = require("mobx"); | ||
function createAsyncContainer(ItemModel, asyncProps) { | ||
const { ttl = Infinity, failstateTtl = 5000 } = asyncProps; | ||
const { name, ttl = Infinity, failstateTtl = 5000 } = asyncProps; | ||
id++; | ||
@@ -22,2 +26,3 @@ const AsyncContainerModel = mobx_keystone_1.Model({ | ||
}); | ||
const debug = debug_1.default(`mobx-keystone:${name}Container`); | ||
let AsyncContainer = class AsyncContainer extends AsyncContainerModel { | ||
@@ -39,2 +44,3 @@ constructor() { | ||
const parent = mobx_keystone_1.getParent(this); | ||
debug("parent.addToFetchQueue()", parent); | ||
if (parent === null || parent === void 0 ? void 0 : parent.addToFetchQueue) { | ||
@@ -70,2 +76,3 @@ // Add itself to the fetch queue | ||
setValue(value) { | ||
debug("setValue()", value); | ||
if (ttl) { | ||
@@ -77,6 +84,8 @@ this.expiresAt = Date.now() + ttl; | ||
} | ||
setPending(pending = true) { | ||
this.isPending = pending; | ||
setPending(isPending = true) { | ||
debug("setPending()", isPending); | ||
this.isPending = isPending; | ||
} | ||
setFailstate(error) { | ||
debug("setFailstate()", error); | ||
this.error = error; | ||
@@ -113,3 +122,3 @@ } | ||
AsyncContainer = __decorate([ | ||
mobx_keystone_1.model(`stores/AsyncContainer(${id})`) | ||
mobx_keystone_1.model(`asyncStores/containers/${name})`) | ||
], AsyncContainer); | ||
@@ -116,0 +125,0 @@ return AsyncContainer; |
import { AnyModel, ModelClass, ModelProps, _Model } from "mobx-keystone"; | ||
import { IAsyncContainer } from "./AsyncContainer"; | ||
export interface AsyncStoreOptions<T> { | ||
name?: string; | ||
fetchOne?(id: string): Promise<T | undefined>; | ||
@@ -5,0 +6,0 @@ fetchMany?(ids: string[]): Promise<Array<T>>; |
@@ -17,4 +17,8 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AsyncStore = void 0; | ||
const debug_1 = __importDefault(require("debug")); | ||
const mobx_1 = require("mobx"); | ||
@@ -26,8 +30,9 @@ const mobx_keystone_1 = require("mobx-keystone"); | ||
function AsyncStore(ItemModel, asyncProps, modelProps = {}) { | ||
const { fetchOne, fetchMany, fetchAll, batchSize = 40, throttle = 200, } = asyncProps; | ||
id++; | ||
const AsyncContainer = AsyncContainer_1.createAsyncContainer(ItemModel, asyncProps); | ||
const { name = `AsyncStore(${id})`, fetchOne, fetchMany, fetchAll, batchSize = 40, throttle = 200, } = asyncProps; | ||
const AsyncContainer = AsyncContainer_1.createAsyncContainer(ItemModel, Object.assign(Object.assign({}, asyncProps), { name })); | ||
const BaseAsyncStoreModel = mobx_keystone_1.Model({ | ||
containers: mobx_keystone_1.prop_mapObject(() => new Map()), | ||
}); | ||
const debug = debug_1.default(`mobx-keystone:${name}`); | ||
let BaseAsyncStore = class BaseAsyncStore extends BaseAsyncStoreModel { | ||
@@ -41,2 +46,3 @@ constructor() { | ||
this.fetchQueueExecutor = (shouldFetch) => __awaiter(this, void 0, void 0, function* () { | ||
debug("fetchQueueExecutor()", shouldFetch); | ||
if (shouldFetch) { | ||
@@ -131,2 +137,3 @@ // Prioratize fetching all | ||
onInit() { | ||
debug("onInit()"); | ||
const dispose = mobx_1.reaction(() => !this.isPending && this.fetchQueue.length > 0, this.fetchQueueExecutor, { | ||
@@ -145,5 +152,7 @@ fireImmediately: true, | ||
spliceFetchQueue(start, end) { | ||
debug(`spliceFetchQueue()`, start, end); | ||
return this.fetchQueue.splice(start, end); | ||
} | ||
setReady() { | ||
debug(`setReady()`); | ||
this.isPending = false; | ||
@@ -153,5 +162,7 @@ this.isReady = true; | ||
setPending() { | ||
debug(`setPending()`); | ||
this.isPending = true; | ||
} | ||
setFailstate(error) { | ||
debug(`setFailstate()`); | ||
this.error = error; | ||
@@ -162,2 +173,3 @@ } | ||
addToFetchQueue(id) { | ||
debug(`addToFetchQueue()`, id); | ||
if (Array.isArray(id)) { | ||
@@ -171,2 +183,3 @@ this.fetchQueue.push(...id); | ||
getOne(id) { | ||
debug(`getOne()`, id); | ||
const ct = this.containers.get(id) || new AsyncContainer({ id }); | ||
@@ -180,2 +193,3 @@ this.containers.set(id, ct); | ||
getMany(ids) { | ||
debug(`getMany()`, ids); | ||
const idsToFetch = []; | ||
@@ -199,2 +213,3 @@ const cts = ids.map((id) => { | ||
getAll(force = false) { | ||
debug(`getAll()`, force); | ||
if (force || (!this.hasAll && !this.fetchQueue.includes("*"))) { | ||
@@ -206,2 +221,3 @@ this.addToFetchQueue("*"); | ||
createAsyncContainer(id, add = false) { | ||
debug(`createAsyncContainer()`, id, add); | ||
const ct = new AsyncContainer({ id }); | ||
@@ -263,3 +279,3 @@ if (add) { | ||
BaseAsyncStore = __decorate([ | ||
mobx_keystone_1.model(`stores/BaseAsyncStore(${id})`) | ||
mobx_keystone_1.model(`asyncStores/Base${name}`) | ||
], BaseAsyncStore); | ||
@@ -266,0 +282,0 @@ const ExportedBaseAsyncStore = BaseAsyncStore; |
{ | ||
"name": "mobx-keystone-asyncstore", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"author": "Marcus Ekwall <marcus.ekwall@gmail.com>", | ||
@@ -12,2 +12,3 @@ "license": "MIT", | ||
"test": "jest", | ||
"test-debug": "cross-env DEBUG='mobx-keystone:*' jest", | ||
"build": "tsc -b tsconfig.build.json", | ||
@@ -26,5 +27,7 @@ "publish": "yarn build && yarn npm publish" | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@types/debug": "^4.1.5", | ||
"@types/jest": "^26.0.7", | ||
"@types/node": "^14.0.27", | ||
"@yarnpkg/pnpify": "^2.1.0", | ||
"cross-env": "^7.0.2", | ||
"jest": "^26.1.0", | ||
@@ -35,3 +38,6 @@ "mobx": "^5.15.4", | ||
"typescript": "^3.9.7" | ||
}, | ||
"dependencies": { | ||
"debug": "^4.1.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39407
6.37%603
5.05%3
50%15
15.38%+ Added
+ Added
+ Added