mirror of
https://github.com/wekan/wekan.git
synced 2026-02-22 16:04:08 +01:00
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:
parent
88b35a6415
commit
e241e27ca1
40 changed files with 10369 additions and 4 deletions
61
npm-packages/meteor-reactive-cache/src/DataCache.js
Normal file
61
npm-packages/meteor-reactive-cache/src/DataCache.js
Normal 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;
|
||||
}
|
||||
}
|
||||
56
npm-packages/meteor-reactive-cache/src/ReactiveCache.js
Normal file
56
npm-packages/meteor-reactive-cache/src/ReactiveCache.js
Normal 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;
|
||||
}
|
||||
}
|
||||
3
npm-packages/meteor-reactive-cache/src/index.js
Normal file
3
npm-packages/meteor-reactive-cache/src/index.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export { default as DataCache } from './DataCache';
|
||||
export { default as ReactiveCache } from './ReactiveCache';
|
||||
export { default as reactiveField } from './reactiveField';
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import { getGlobal } from '@wekanteam/meteor-globals';
|
||||
|
||||
const Meteor = getGlobal('meteor', 'Meteor');
|
||||
export default Meteor.bindEnvironment.bind(Meteor);
|
||||
3
npm-packages/meteor-reactive-cache/src/meteor/ejson.js
Normal file
3
npm-packages/meteor-reactive-cache/src/meteor/ejson.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { getGlobal } from '@wekanteam/meteor-globals';
|
||||
|
||||
export default getGlobal('ejson', 'EJSON');
|
||||
3
npm-packages/meteor-reactive-cache/src/meteor/tracker.js
Normal file
3
npm-packages/meteor-reactive-cache/src/meteor/tracker.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import { getGlobal } from '@wekanteam/meteor-globals';
|
||||
|
||||
export default getGlobal('tracker', 'Tracker');
|
||||
9
npm-packages/meteor-reactive-cache/src/reactiveField.js
Normal file
9
npm-packages/meteor-reactive-cache/src/reactiveField.js
Normal 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;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue