mobx-keystone-asyncstore
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -167,4 +167,4 @@ import { AnyModel, ModelClass, ModelProps, _Model } from "mobx-keystone"; | ||
getMany(id: string[]): T[]; | ||
getAll(): T[]; | ||
getAll(force?: boolean): T[]; | ||
createAsyncContainer(id: string, add?: boolean): T; | ||
} |
@@ -38,2 +38,21 @@ "use strict"; | ||
this.hasAll = false; | ||
this.fetchQueueExecutor = (shouldFetch) => __awaiter(this, void 0, void 0, function* () { | ||
if (shouldFetch) { | ||
// Prioratize fetching all | ||
const fetchAllIndex = this.fetchQueue.indexOf("*"); | ||
if (fetchAllIndex !== -1) { | ||
this.spliceFetchQueue(fetchAllIndex, 1); | ||
yield this.fetchAll(); | ||
} | ||
else { | ||
const idsToFetch = this.spliceFetchQueue(0, batchSize); | ||
if (idsToFetch.length === 1) { | ||
yield this.fetchOne(idsToFetch[0]); | ||
} | ||
else { | ||
yield this.fetchMany(idsToFetch); | ||
} | ||
} | ||
} | ||
}); | ||
this.fetchOne = mobx_keystone_1._async(function* (id) { | ||
@@ -110,26 +129,7 @@ if (!fetchOne && !fetchMany) { | ||
onInit() { | ||
const dispose = mobx_1.reaction(() => !this.isPending && this.fetchQueue.length > 0, (shouldFetch) => __awaiter(this, void 0, void 0, function* () { | ||
if (shouldFetch) { | ||
// Prioratize fetching all | ||
const fetchAllIndex = this.fetchQueue.indexOf("*"); | ||
if (fetchAllIndex !== -1) { | ||
this.spliceFetchQueue(fetchAllIndex, 1); | ||
yield this.fetchAll(); | ||
} | ||
else { | ||
const idsToFetch = this.spliceFetchQueue(0, batchSize); | ||
if (idsToFetch.length === 1) { | ||
yield this.fetchOne(idsToFetch[0]); | ||
} | ||
else { | ||
yield this.fetchMany(idsToFetch); | ||
} | ||
} | ||
} | ||
}), { | ||
const dispose = mobx_1.reaction(() => !this.isPending && this.fetchQueue.length > 0, this.fetchQueueExecutor, { | ||
fireImmediately: true, | ||
scheduler: (run) => { | ||
return setTimeout(run, throttle); | ||
}, | ||
scheduler: this.fetchQueueScheduler, | ||
}); | ||
/* istanbul ignore next */ | ||
return () => { | ||
@@ -139,2 +139,5 @@ dispose(); | ||
} | ||
fetchQueueScheduler(run) { | ||
setTimeout(run, throttle); | ||
} | ||
spliceFetchQueue(start, end) { | ||
@@ -172,2 +175,3 @@ return this.fetchQueue.splice(start, end); | ||
getMany(ids) { | ||
const idsToFetch = []; | ||
const cts = ids.map((id) => { | ||
@@ -179,5 +183,10 @@ let ct = this.containers.get(id); | ||
} | ||
if (ct.shouldFetch) { | ||
idsToFetch.push(id); | ||
} | ||
return ct; | ||
}); | ||
this.addToFetchQueue(ids); | ||
if (idsToFetch.length > 0) { | ||
this.addToFetchQueue(idsToFetch); | ||
} | ||
return cts; | ||
@@ -184,0 +193,0 @@ } |
{ | ||
"name": "mobx-keystone-asyncstore", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"author": "Marcus Ekwall <marcus.ekwall@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37048
574