Add local fork of meteor-reactive-cache package

- Clone @wekanteam/meteor-reactive-cache to npm-packages/
- Add .meteorignore to exclude npm-packages from Meteor
- Update package.json to use local file path

This allows direct modification of the package for Meteor 3.0
async migration while maintaining publishability to npm.
This commit is contained in:
Harry Adel 2026-01-31 20:46:05 +02:00
parent 88b35a6415
commit e241e27ca1
40 changed files with 10369 additions and 4 deletions

View file

@ -0,0 +1,61 @@
import bindEnvironment from './meteor/bindEnvironment';
import Tracker from './meteor/tracker';
import ReactiveCache from './ReactiveCache';
export default class DataCache {
constructor(getData, options) {
this.options = {
timeout: 60 * 1000, // 60 seconds
...(typeof options === 'function' ? { compare: options } : options),
};
this.getData = getData;
this.cache = new ReactiveCache(this.options.compare, () => false);
this.timeouts = {};
this.computations = {};
}
ensureComputation(key) {
if (this.timeouts[key]) {
clearTimeout(this.timeouts[key]);
delete this.timeouts[key];
}
if (this.computations[key] && !this.computations[key].stopped) return;
this.computations[key] = Tracker.nonreactive(() => Tracker.autorun(() => {
this.cache.set(key, this.getData(key));
}));
// stop the computation if the key doesn't have any dependants
this.computations[key].onInvalidate(() => this.checkStop(key));
}
checkStop(key) {
if (this.cache.ensureDependency(key).hasDependents()) return;
if (this.timeouts[key]) {
clearTimeout(this.timeouts[key]);
delete this.timeouts[key];
}
this.timeouts[key] = setTimeout(bindEnvironment(() => {
if (!this.computations[key]) return;
this.computations[key].stop();
delete this.computations[key];
this.cache.del(key);
}), this.options.timeout);
}
get(key) {
if (!Tracker.currentComputation) {
let data = this.cache.get(key);
if (data == null) {
data = this.getData(key);
this.cache.set(key, data);
this.checkStop(key);
}
return data;
}
this.ensureComputation(key);
const data = this.cache.get(key);
Tracker.currentComputation.onStop(() => this.checkStop(key));
return data;
}
}

View file

@ -0,0 +1,56 @@
import Tracker from './meteor/tracker';
export default class ReactiveCache {
constructor(compare, shouldStop) {
this.shouldStop = shouldStop || (() => true);
this.compare = compare || ((a, b) => a === b);
this.values = {};
this.deps = {};
}
ensureDependency(key) {
if (!this.deps[key]) this.deps[key] = new Tracker.Dependency();
return this.deps[key];
}
checkDeletion(key) {
const dep = this.ensureDependency(key);
if (dep.hasDependents()) return false;
delete this.values[key];
delete this.deps[key];
return true;
}
clear() {
Object.keys(this.values).forEach((key) => this.del(key));
}
del(key) {
const dep = this.ensureDependency(key);
delete this.values[key];
if (this.checkDeletion(key)) return;
dep.changed();
}
set(key, data, bypassCompare) {
const dep = this.ensureDependency(key);
const current = this.values[key];
this.values[key] = data;
if (!this.compare(current, data) || bypassCompare) {
dep.changed();
}
}
get(key) {
const data = this.values[key];
if (Tracker.currentComputation) {
const dep = this.ensureDependency(key);
dep.depend();
Tracker.currentComputation.onStop(() => {
if (!this.shouldStop(key)) return;
this.checkDeletion(key);
});
}
return data;
}
}

View file

@ -0,0 +1,3 @@
export { default as DataCache } from './DataCache';
export { default as ReactiveCache } from './ReactiveCache';
export { default as reactiveField } from './reactiveField';

View file

@ -0,0 +1,4 @@
import { getGlobal } from '@wekanteam/meteor-globals';
const Meteor = getGlobal('meteor', 'Meteor');
export default Meteor.bindEnvironment.bind(Meteor);

View file

@ -0,0 +1,3 @@
import { getGlobal } from '@wekanteam/meteor-globals';
export default getGlobal('ejson', 'EJSON');

View file

@ -0,0 +1,3 @@
import { getGlobal } from '@wekanteam/meteor-globals';
export default getGlobal('tracker', 'Tracker');

View file

@ -0,0 +1,9 @@
import DataCache from './DataCache';
import ejson from './meteor/ejson';
export default (fn, compare) => {
const cache = new DataCache((key) => fn(...ejson.parse(key)), compare);
const resolver = (...args) => cache.get(ejson.stringify(args));
resolver.cache = cache;
return resolver;
};