Skip to content

Commit

Permalink
new buffer structure, removed native dep
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Charette authored and Frederic Charette committed Dec 13, 2021
1 parent 46e2dfc commit 68570c8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 267 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ batch | false | <pre>{&#13;&#10;&nbsp;&nbsp;delay: 50,&#13;&#10;&nbsp;&nbsp;limi

HA-store emits events to track cache hits, miss and outbound requests.

Event | Description
Event | Format | Description
--- | ---
cacheHit | When the requested item is present in the microcache, or is already being fetched. Prevents another request from being created.
cacheMiss | When the requested item not cached or coalesced and must be fetched.
coalescedHit | When a record query successfully hooks to the promise of the same record in transit.
query | When a batch of requests is about to be sent.
queryFailed | Indicates that the batch has failed. Retry policy will dictate if it should be re-attempted.
querySuccess | Indicates that the batch request was successful.
cacheHit | `<number>` | When the requested item is present in the microcache, or is already being fetched. Prevents another request from being created.
cacheMiss | `<number>` | When the requested item not cached or coalesced and must be fetched.
coalescedHit | `<number>` | When a record query successfully hooks to the promise of the same record in transit.
query | `<object>` | When a batch of requests is about to be sent, gives the detail of the query and what triggered it.
queryFailed | `<object>` | Indicates that the batch has failed. Retry policy will dictate if it should be re-attempted.
querySuccess | `<object>` | Indicates that the batch request was successful.

You may also want to track the amount of `contexts` and `records` stored via the `size` method.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ha-store",
"version": "3.0.0",
"version": "3.1.0",
"description": "Efficient data fetching",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -49,6 +49,6 @@
],
"typings": "./src/index.d.ts",
"dependencies": {
"lru-native2": "^1.2.5"
"lru-cache": "^6.0.0"
}
}
31 changes: 16 additions & 15 deletions src/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function queryBuffer(config, emitter, targetStore) {
class RequestBuffer {
constructor(key, params) {
this.uid = Math.random().toString(36);
this.status = 0;
this.state = 0;
this.ids = [];
this.contextKey = key;
this.params = params;
Expand All @@ -23,32 +23,33 @@ function queryBuffer(config, emitter, targetStore) {
const sizeLimit = config.batch?.limit || 1;

if (this.ids.length >= sizeLimit) {
this.run();
this.run('limit');
return this;
}
if (this.timer === null) {
this.timer = setTimeout(this.run.bind(this), config.batch?.delay || 0);
this.timer = setTimeout(this.run.bind(this, 'timeout'), config.batch?.delay || 0);
}

return this;
}

run() {
run(cause) {
this.state = 1;
clearTimeout(this.timer);
emitter.emit('query', this);
emitter.emit('query', { cause, key: this.contextKey, uid: this.uid, size: this.ids.length, params: this.params, contexts: this.contexts, ids: this.ids });
config.resolver(this.ids, this.params, this.contexts)
.then(this.handleQuerySuccess.bind(this), this.handleQueryError.bind(this));

if (numCached > 0) {
emitter.emit('cacheHit', { found: numCached });
emitter.emit('cacheHit', numCached);
numCached = 0;
}
if (numMisses > 0) {
emitter.emit('cacheMiss', { found: numMisses });
emitter.emit('cacheMiss', numMisses);
numMisses = 0;
}
if (numCoalesced > 0) {
emitter.emit('coalescedHit', { found: numCoalesced });
emitter.emit('coalescedHit', numCoalesced);
numCoalesced = 0;
}
}
Expand All @@ -57,19 +58,19 @@ function queryBuffer(config, emitter, targetStore) {
this.state = 2;
emitter.emit('queryFailed', { key: this.contextKey, uid: this.uid, size: this.ids.length, params: this.params, error });
this.handle.reject(error);
buffers.splice(buffers.indexOf(this));
buffers.splice(buffers.indexOf(this), 1);
}

handleQuerySuccess(entries) {
this.state = 2;
emitter.emit('querySuccess', { key: this.contextKey, uid: this.uid, size: this.ids.length, params: this.params });
this.handle.resolve(entries);
if (config.cache !== null) targetStore.set(contextRecordKey(this.contextKey), this.ids, entries || {});
buffers.splice(buffers.indexOf(this));
buffers.splice(buffers.indexOf(this), 1);
}
}

async function getHandles(key, ids, params, context) {
const getHandles = (async function (key, ids, params, context) {
const handles = Array.from(new Array(ids.length));

if (config.cache !== null) {
Expand Down Expand Up @@ -97,10 +98,10 @@ function queryBuffer(config, emitter, targetStore) {
}

return handles;
}
});

function assignQuery(key, id, params, context) {
let liveBuffer = buffers.find(buffer => buffer.contextKey === key);
let liveBuffer = buffers.find(buffer => buffer.contextKey === key && buffer.state === 0);
if (!liveBuffer) {
liveBuffer = new RequestBuffer(key, params);
buffers.push(liveBuffer);
Expand All @@ -115,8 +116,8 @@ function queryBuffer(config, emitter, targetStore) {

function size() {
return {
pendingBuffers: buffers.filter(buffer => buffer.status === 0).length,
activeBuffers: buffers.filter(buffer => buffer.status === 1).length,
pendingBuffers: buffers.filter(buffer => buffer.state === 0).length,
activeBuffers: buffers.filter(buffer => buffer.state === 1).length,
}
}

Expand Down
86 changes: 0 additions & 86 deletions src/index-old.js

This file was deleted.

108 changes: 0 additions & 108 deletions src/queries.js

This file was deleted.

49 changes: 0 additions & 49 deletions src/store-old.js

This file was deleted.

0 comments on commit 68570c8

Please sign in to comment.