mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 10:46:00 +00:00
structured reload trigger
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { apiOn } from './api';
|
||||
import getAsArray from './getAsArray';
|
||||
import stableStringify from 'json-stable-stringify';
|
||||
|
||||
const cachedByKey = {};
|
||||
const cachedPromisesByKey = {};
|
||||
@@ -15,10 +16,11 @@ function cacheGet(key) {
|
||||
|
||||
function addCacheKeyToReloadTrigger(cacheKey, reloadTrigger) {
|
||||
for (const item of getAsArray(reloadTrigger)) {
|
||||
if (!(item in cachedKeysByReloadTrigger)) {
|
||||
cachedKeysByReloadTrigger[item] = [];
|
||||
const itemString = stableStringify(item);
|
||||
if (!(itemString in cachedKeysByReloadTrigger)) {
|
||||
cachedKeysByReloadTrigger[itemString] = [];
|
||||
}
|
||||
cachedKeysByReloadTrigger[item].push(cacheKey);
|
||||
cachedKeysByReloadTrigger[itemString].push(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +34,8 @@ function cacheSet(cacheKey, value, reloadTrigger, generation) {
|
||||
function cacheClean(reloadTrigger) {
|
||||
cacheGeneration += 1;
|
||||
for (const item of getAsArray(reloadTrigger)) {
|
||||
const keys = cachedKeysByReloadTrigger[item];
|
||||
const itemString = stableStringify(item);
|
||||
const keys = cachedKeysByReloadTrigger[itemString];
|
||||
if (keys) {
|
||||
for (const key of keys) {
|
||||
delete cachedByKey[key];
|
||||
@@ -40,7 +43,7 @@ function cacheClean(reloadTrigger) {
|
||||
cacheGenerationByKey[key] = cacheGeneration;
|
||||
}
|
||||
}
|
||||
delete cachedKeysByReloadTrigger[item];
|
||||
delete cachedKeysByReloadTrigger[itemString];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,20 +90,24 @@ export async function loadCachedValue(reloadTrigger, cacheKey, func) {
|
||||
|
||||
export async function subscribeCacheChange(reloadTrigger, cacheKey, reloadHandler) {
|
||||
for (const item of getAsArray(reloadTrigger)) {
|
||||
if (!subscriptionsByReloadTrigger[item]) {
|
||||
subscriptionsByReloadTrigger[item] = [];
|
||||
const itemString = stableStringify(item);
|
||||
if (!subscriptionsByReloadTrigger[itemString]) {
|
||||
subscriptionsByReloadTrigger[itemString] = [];
|
||||
}
|
||||
subscriptionsByReloadTrigger[item].push(reloadHandler);
|
||||
subscriptionsByReloadTrigger[itemString].push(reloadHandler);
|
||||
}
|
||||
}
|
||||
|
||||
export async function unsubscribeCacheChange(reloadTrigger, cacheKey, reloadHandler) {
|
||||
for (const item of getAsArray(reloadTrigger)) {
|
||||
if (subscriptionsByReloadTrigger[item]) {
|
||||
subscriptionsByReloadTrigger[item] = subscriptionsByReloadTrigger[item].filter(x => x != reloadHandler);
|
||||
const itemString = stableStringify(item);
|
||||
if (subscriptionsByReloadTrigger[itemString]) {
|
||||
subscriptionsByReloadTrigger[itemString] = subscriptionsByReloadTrigger[itemString].filter(
|
||||
x => x != reloadHandler
|
||||
);
|
||||
}
|
||||
if (subscriptionsByReloadTrigger[item].length == 0) {
|
||||
delete subscriptionsByReloadTrigger[item];
|
||||
if (subscriptionsByReloadTrigger[itemString].length == 0) {
|
||||
delete subscriptionsByReloadTrigger[itemString];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,8 +117,9 @@ export function dispatchCacheChange(reloadTrigger) {
|
||||
cacheClean(reloadTrigger);
|
||||
|
||||
for (const item of getAsArray(reloadTrigger)) {
|
||||
if (subscriptionsByReloadTrigger[item]) {
|
||||
for (const handler of subscriptionsByReloadTrigger[item]) {
|
||||
const itemString = stableStringify(item);
|
||||
if (subscriptionsByReloadTrigger[itemString]) {
|
||||
for (const handler of subscriptionsByReloadTrigger[itemString]) {
|
||||
handler();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user