Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions src/helpers/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ export default class Cache {
this.#workerManager.setOnCacheUpdates((updates) =>
this.#handleCacheUpdates(updates));

this.#workerManager.setOnCacheDeletions((deletions) =>
this.#handleCacheDeletions(deletions));

this.#workerManager.setOnCacheVersionRequest((domainId) =>
this.#handleCacheVersionRequest(domainId));

this.#workerManager.setOnCachedDomainIdsRequest(() =>
this.#handleCachedDomainIdsRequest());

this.#workerManager.setOnError((error) => {
Logger.error('Cache worker error:', error);
});
Expand All @@ -47,10 +53,8 @@ export default class Cache {
}

async stopScheduledUpdates() {
if (this.#workerManager) {
await this.#workerManager.stop();
this.#workerManager = null;
}
await this.#workerManager?.stop();
this.#workerManager = null;
}

async #updateCache(domain) {
Expand All @@ -62,7 +66,6 @@ export default class Cache {

this.#set(domain._id, {
data: reduceSnapshot(result.data.domain),
lastUpdate: domain.lastUpdate,
version: result.data.domain.version
});
}
Expand All @@ -71,19 +74,25 @@ export default class Cache {
for (const update of updates) {
this.#set(update.domainId, {
data: update.data,
lastUpdate: update.lastUpdate,
version: update.version
});
}
}

#handleCacheDeletions(deletions) {
for (const domainId of deletions) {
this.#instance.delete(String(domainId));
}
}

#handleCacheVersionRequest(domainId) {
const cached = this.#instance.get(String(domainId));
const cachedVersion = cached?.lastUpdate || null;

if (this.#workerManager) {
this.#workerManager.sendCacheVersionResponse(domainId, cachedVersion);
}
this.#workerManager.sendCacheVersionResponse(domainId, cached?.version);
}

#handleCachedDomainIdsRequest() {
const domainIds = Array.from(this.#instance.keys());
this.#workerManager.sendCachedDomainIdsResponse(domainIds);
}

#set(key, value) {
Expand Down
104 changes: 69 additions & 35 deletions src/helpers/cache/worker-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ export const EVENT_TYPE = {
STOPPED: 'stopped',
READY: 'ready',
CACHE_UPDATES: 'cache-updates',
CACHE_DELETIONS: 'cache-deletions',
REQUEST_CACHE_VERSION: 'request-cache-version',
CACHE_VERSION_RESPONSE: 'cache-version-response',
REQUEST_CACHED_DOMAIN_IDS: 'request-cached-domain-ids',
CACHED_DOMAIN_IDS_RESPONSE: 'cached-domain-ids-response',
ERROR: 'error'
};

Expand All @@ -30,53 +33,69 @@ export class CacheWorkerManager {
constructor(options = {}) {
this.worker = null;
this.status = STATUS_TYPE.STOPPED;
this.onCacheUpdates = null;
this.onCacheDeletions = null;
this.onCachedDomainIdsRequest = null;
this.onError = null;
this.options = {
interval: this.DEFAULT_INTERVAL,
...options
};
this.onCacheUpdates = null;
this.onError = null;
}

#buildEvents(resolve) {
return new Map([
[EVENT_TYPE.READY, () => {
this.worker.postMessage({ type: EVENT_TYPE.START });
}],
[EVENT_TYPE.STARTED, () => {
this.status = STATUS_TYPE.RUNNING;
resolve();
}],
[EVENT_TYPE.STOPPED, () => {
this.status = STATUS_TYPE.STOPPED;
}],
[EVENT_TYPE.CACHE_UPDATES, (message) => {
if (this.onCacheUpdates) {
this.onCacheUpdates(message.updates);
}
}],
[EVENT_TYPE.CACHE_DELETIONS, (message) => {
if (this.onCacheDeletions) {
this.onCacheDeletions(message.deletions);
}
}],
[EVENT_TYPE.REQUEST_CACHE_VERSION, (message) => {
if (this.onCacheVersionRequest) {
this.onCacheVersionRequest(message.domainId);
}
}],
[EVENT_TYPE.REQUEST_CACHED_DOMAIN_IDS, () => {
if (this.onCachedDomainIdsRequest) {
this.onCachedDomainIdsRequest();
}
}],
[EVENT_TYPE.ERROR, (message) => {
if (this.onError) {
this.onError(new Error(message.error));
}
}]
]);
}

start() {
return new Promise((resolve, reject) => {
const workerPath = join(__dirname, 'worker.js');
const eventHandlers = this.#buildEvents(resolve);

this.worker = new Worker(workerPath, {
workerData: this.options
});

this.worker.on('message', (message) => {
switch (message.type) {
case EVENT_TYPE.READY:
this.worker.postMessage({ type: EVENT_TYPE.START });
break;

case EVENT_TYPE.STARTED:
this.status = STATUS_TYPE.RUNNING;
resolve();
break;

case EVENT_TYPE.STOPPED:
this.status = STATUS_TYPE.STOPPED;
break;

case EVENT_TYPE.CACHE_UPDATES:
if (this.onCacheUpdates) {
this.onCacheUpdates(message.updates);
}
break;

case EVENT_TYPE.REQUEST_CACHE_VERSION:
if (this.onCacheVersionRequest) {
this.onCacheVersionRequest(message.domainId);
}
break;

case EVENT_TYPE.ERROR:
if (this.onError) {
this.onError(new Error(message.error));
}
break;
const handler = eventHandlers.get(message.type);
if (handler) {
handler(message);
}
});

Expand Down Expand Up @@ -129,8 +148,6 @@ export class CacheWorkerManager {
};

this.worker.on('message', onMessage);

// Send stop message
this.worker.postMessage({ type: EVENT_TYPE.STOP });
});
}
Expand All @@ -143,10 +160,18 @@ export class CacheWorkerManager {
this.onCacheUpdates = callback;
}

setOnCacheDeletions(callback) {
this.onCacheDeletions = callback;
}

setOnCacheVersionRequest(callback) {
this.onCacheVersionRequest = callback;
}

setOnCachedDomainIdsRequest(callback) {
this.onCachedDomainIdsRequest = callback;
}

sendCacheVersionResponse(domainId, cachedVersion) {
if (this.worker && this.status === STATUS_TYPE.RUNNING) {
this.worker.postMessage({
Expand All @@ -157,6 +182,15 @@ export class CacheWorkerManager {
}
}

sendCachedDomainIdsResponse(domainIds) {
if (this.worker && this.status === STATUS_TYPE.RUNNING) {
this.worker.postMessage({
type: EVENT_TYPE.CACHED_DOMAIN_IDS_RESPONSE,
domainIds
});
}
}

setOnError(callback) {
this.onError = callback;
}
Expand Down
Loading