Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

caching.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.makeWeakCache = makeWeakCache;
  6. exports.makeWeakCacheSync = makeWeakCacheSync;
  7. exports.makeStrongCache = makeStrongCache;
  8. exports.makeStrongCacheSync = makeStrongCacheSync;
  9. exports.assertSimpleType = assertSimpleType;
  10. function _gensync() {
  11. const data = require("gensync");
  12. _gensync = function () {
  13. return data;
  14. };
  15. return data;
  16. }
  17. var _async = require("../gensync-utils/async");
  18. var _util = require("./util");
  19. const synchronize = gen => {
  20. return _gensync()(gen).sync;
  21. };
  22. function* genTrue() {
  23. return true;
  24. }
  25. function makeWeakCache(handler) {
  26. return makeCachedFunction(WeakMap, handler);
  27. }
  28. function makeWeakCacheSync(handler) {
  29. return synchronize(makeWeakCache(handler));
  30. }
  31. function makeStrongCache(handler) {
  32. return makeCachedFunction(Map, handler);
  33. }
  34. function makeStrongCacheSync(handler) {
  35. return synchronize(makeStrongCache(handler));
  36. }
  37. function makeCachedFunction(CallCache, handler) {
  38. const callCacheSync = new CallCache();
  39. const callCacheAsync = new CallCache();
  40. const futureCache = new CallCache();
  41. return function* cachedFunction(arg, data) {
  42. const asyncContext = yield* (0, _async.isAsync)();
  43. const callCache = asyncContext ? callCacheAsync : callCacheSync;
  44. const cached = yield* getCachedValueOrWait(asyncContext, callCache, futureCache, arg, data);
  45. if (cached.valid) return cached.value;
  46. const cache = new CacheConfigurator(data);
  47. const handlerResult = handler(arg, cache);
  48. let finishLock;
  49. let value;
  50. if ((0, _util.isIterableIterator)(handlerResult)) {
  51. const gen = handlerResult;
  52. value = yield* (0, _async.onFirstPause)(gen, () => {
  53. finishLock = setupAsyncLocks(cache, futureCache, arg);
  54. });
  55. } else {
  56. value = handlerResult;
  57. }
  58. updateFunctionCache(callCache, cache, arg, value);
  59. if (finishLock) {
  60. futureCache.delete(arg);
  61. finishLock.release(value);
  62. }
  63. return value;
  64. };
  65. }
  66. function* getCachedValue(cache, arg, data) {
  67. const cachedValue = cache.get(arg);
  68. if (cachedValue) {
  69. for (const {
  70. value,
  71. valid
  72. } of cachedValue) {
  73. if (yield* valid(data)) return {
  74. valid: true,
  75. value
  76. };
  77. }
  78. }
  79. return {
  80. valid: false,
  81. value: null
  82. };
  83. }
  84. function* getCachedValueOrWait(asyncContext, callCache, futureCache, arg, data) {
  85. const cached = yield* getCachedValue(callCache, arg, data);
  86. if (cached.valid) {
  87. return cached;
  88. }
  89. if (asyncContext) {
  90. const cached = yield* getCachedValue(futureCache, arg, data);
  91. if (cached.valid) {
  92. const value = yield* (0, _async.waitFor)(cached.value.promise);
  93. return {
  94. valid: true,
  95. value
  96. };
  97. }
  98. }
  99. return {
  100. valid: false,
  101. value: null
  102. };
  103. }
  104. function setupAsyncLocks(config, futureCache, arg) {
  105. const finishLock = new Lock();
  106. updateFunctionCache(futureCache, config, arg, finishLock);
  107. return finishLock;
  108. }
  109. function updateFunctionCache(cache, config, arg, value) {
  110. if (!config.configured()) config.forever();
  111. let cachedValue = cache.get(arg);
  112. config.deactivate();
  113. switch (config.mode()) {
  114. case "forever":
  115. cachedValue = [{
  116. value,
  117. valid: genTrue
  118. }];
  119. cache.set(arg, cachedValue);
  120. break;
  121. case "invalidate":
  122. cachedValue = [{
  123. value,
  124. valid: config.validator()
  125. }];
  126. cache.set(arg, cachedValue);
  127. break;
  128. case "valid":
  129. if (cachedValue) {
  130. cachedValue.push({
  131. value,
  132. valid: config.validator()
  133. });
  134. } else {
  135. cachedValue = [{
  136. value,
  137. valid: config.validator()
  138. }];
  139. cache.set(arg, cachedValue);
  140. }
  141. }
  142. }
  143. class CacheConfigurator {
  144. constructor(data) {
  145. this._active = true;
  146. this._never = false;
  147. this._forever = false;
  148. this._invalidate = false;
  149. this._configured = false;
  150. this._pairs = [];
  151. this._data = void 0;
  152. this._data = data;
  153. }
  154. simple() {
  155. return makeSimpleConfigurator(this);
  156. }
  157. mode() {
  158. if (this._never) return "never";
  159. if (this._forever) return "forever";
  160. if (this._invalidate) return "invalidate";
  161. return "valid";
  162. }
  163. forever() {
  164. if (!this._active) {
  165. throw new Error("Cannot change caching after evaluation has completed.");
  166. }
  167. if (this._never) {
  168. throw new Error("Caching has already been configured with .never()");
  169. }
  170. this._forever = true;
  171. this._configured = true;
  172. }
  173. never() {
  174. if (!this._active) {
  175. throw new Error("Cannot change caching after evaluation has completed.");
  176. }
  177. if (this._forever) {
  178. throw new Error("Caching has already been configured with .forever()");
  179. }
  180. this._never = true;
  181. this._configured = true;
  182. }
  183. using(handler) {
  184. if (!this._active) {
  185. throw new Error("Cannot change caching after evaluation has completed.");
  186. }
  187. if (this._never || this._forever) {
  188. throw new Error("Caching has already been configured with .never or .forever()");
  189. }
  190. this._configured = true;
  191. const key = handler(this._data);
  192. const fn = (0, _async.maybeAsync)(handler, `You appear to be using an async cache handler, but Babel has been called synchronously`);
  193. if ((0, _async.isThenable)(key)) {
  194. return key.then(key => {
  195. this._pairs.push([key, fn]);
  196. return key;
  197. });
  198. }
  199. this._pairs.push([key, fn]);
  200. return key;
  201. }
  202. invalidate(handler) {
  203. this._invalidate = true;
  204. return this.using(handler);
  205. }
  206. validator() {
  207. const pairs = this._pairs;
  208. return function* (data) {
  209. for (const [key, fn] of pairs) {
  210. if (key !== (yield* fn(data))) return false;
  211. }
  212. return true;
  213. };
  214. }
  215. deactivate() {
  216. this._active = false;
  217. }
  218. configured() {
  219. return this._configured;
  220. }
  221. }
  222. function makeSimpleConfigurator(cache) {
  223. function cacheFn(val) {
  224. if (typeof val === "boolean") {
  225. if (val) cache.forever();else cache.never();
  226. return;
  227. }
  228. return cache.using(() => assertSimpleType(val()));
  229. }
  230. cacheFn.forever = () => cache.forever();
  231. cacheFn.never = () => cache.never();
  232. cacheFn.using = cb => cache.using(() => assertSimpleType(cb()));
  233. cacheFn.invalidate = cb => cache.invalidate(() => assertSimpleType(cb()));
  234. return cacheFn;
  235. }
  236. function assertSimpleType(value) {
  237. if ((0, _async.isThenable)(value)) {
  238. throw new Error(`You appear to be using an async cache handler, ` + `which your current version of Babel does not support. ` + `We may add support for this in the future, ` + `but if you're on the most recent version of @babel/core and still ` + `seeing this error, then you'll need to synchronously handle your caching logic.`);
  239. }
  240. if (value != null && typeof value !== "string" && typeof value !== "boolean" && typeof value !== "number") {
  241. throw new Error("Cache keys must be either string, boolean, number, null, or undefined.");
  242. }
  243. return value;
  244. }
  245. class Lock {
  246. constructor() {
  247. this.released = false;
  248. this.promise = void 0;
  249. this._resolve = void 0;
  250. this.promise = new Promise(resolve => {
  251. this._resolve = resolve;
  252. });
  253. }
  254. release(value) {
  255. this.released = true;
  256. this._resolve(value);
  257. }
  258. }