Comparing version
@@ -96,2 +96,3 @@ 'use strict'; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function maybeUnsubscribe(entryOrDep) { | ||
@@ -151,2 +152,3 @@ var unsubscribe = entryOrDep.unsubscribe; | ||
if (this.value.length === 1 && !mightBeDirty(this)) { | ||
rememberParent(this); | ||
return this.value[0]; | ||
@@ -182,4 +184,3 @@ } | ||
var _this = this; | ||
forgetChildren(this); | ||
maybeUnsubscribe(this); | ||
this.setDirty(); | ||
// Because this entry has been kicked out of the cache (in index.js), | ||
@@ -201,2 +202,8 @@ // we've lost the ability to find out if/when this entry becomes dirty, | ||
}; | ||
Entry.prototype.forget = function () { | ||
// The code that creates Entry objects in index.ts will replace this method | ||
// with one that actually removes the Entry from the cache, which will also | ||
// trigger the entry.dispose method. | ||
this.dispose(); | ||
}; | ||
Entry.prototype.dependOn = function (dep) { | ||
@@ -287,2 +294,3 @@ dep.add(this); | ||
assert(mightBeDirty(child)); | ||
var parentWasClean = !mightBeDirty(parent); | ||
if (!parent.dirtyChildren) { | ||
@@ -298,3 +306,7 @@ parent.dirtyChildren = emptySetPool.pop() || new Set; | ||
parent.dirtyChildren.add(child); | ||
reportDirty(parent); | ||
// If parent was clean before, it just became (possibly) dirty (according to | ||
// mightBeDirty), since we just added child to parent.dirtyChildren. | ||
if (parentWasClean) { | ||
reportDirty(parent); | ||
} | ||
} | ||
@@ -372,2 +384,7 @@ // Let a parent Entry know that one of its children is no longer dirty. | ||
var EntryMethods = { | ||
setDirty: true, | ||
dispose: true, | ||
forget: true, | ||
}; | ||
function dep(options) { | ||
@@ -390,6 +407,8 @@ var depsByKey = new Map(); | ||
} | ||
depend.dirty = function dirty(key) { | ||
depend.dirty = function dirty(key, entryMethodName) { | ||
var dep = depsByKey.get(key); | ||
if (dep) { | ||
dep.forEach(function (entry) { return entry.setDirty(); }); | ||
var m_1 = (entryMethodName && | ||
hasOwnProperty.call(EntryMethods, entryMethodName)) ? entryMethodName : "setDirty"; | ||
dep.forEach(function (entry) { return entry[m_1](); }); | ||
depsByKey.delete(key); | ||
@@ -402,2 +421,8 @@ maybeUnsubscribe(dep); | ||
function makeDefaultMakeCacheKeyFunction() { | ||
var keyTrie = new trie.Trie(typeof WeakMap === "function"); | ||
return function () { | ||
return keyTrie.lookupArray(arguments); | ||
}; | ||
} | ||
// The defaultMakeCacheKey function is remarkably powerful, because it gives | ||
@@ -410,10 +435,3 @@ // a unique object for any shallow-identical list of arguments. If you need | ||
// In those cases, just write your own custom makeCacheKey functions. | ||
var keyTrie = new trie.Trie(typeof WeakMap === "function"); | ||
function defaultMakeCacheKey() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return keyTrie.lookupArray(args); | ||
} | ||
var defaultMakeCacheKey = makeDefaultMakeCacheKeyFunction(); | ||
var caches = new Set(); | ||
@@ -424,4 +442,5 @@ function wrap(originalFunction, options) { | ||
var keyArgs = options.keyArgs; | ||
var makeCacheKey = options.makeCacheKey || defaultMakeCacheKey; | ||
function optimistic() { | ||
var makeCacheKey = options.makeCacheKey || | ||
makeDefaultMakeCacheKeyFunction(); | ||
var optimistic = function () { | ||
var key = makeCacheKey.apply(null, keyArgs ? keyArgs.apply(null, arguments) : arguments); | ||
@@ -435,2 +454,5 @@ if (key === void 0) { | ||
entry.subscribe = options.subscribe; | ||
// Give the Entry the ability to trigger cache.delete(key), even though | ||
// the Entry itself does not know about key or cache. | ||
entry.forget = function () { return cache.delete(key); }; | ||
} | ||
@@ -450,26 +472,35 @@ var value = entry.recompute(Array.prototype.slice.call(arguments)); | ||
return value; | ||
} | ||
function lookup() { | ||
var key = makeCacheKey.apply(null, arguments); | ||
if (key !== void 0) { | ||
return cache.get(key); | ||
} | ||
} | ||
optimistic.dirty = function () { | ||
var entry = lookup.apply(null, arguments); | ||
}; | ||
function dirtyKey(key) { | ||
var entry = cache.get(key); | ||
if (entry) { | ||
entry.setDirty(); | ||
} | ||
} | ||
optimistic.dirtyKey = dirtyKey; | ||
optimistic.dirty = function dirty() { | ||
dirtyKey(makeCacheKey.apply(null, arguments)); | ||
}; | ||
optimistic.peek = function () { | ||
var entry = lookup.apply(null, arguments); | ||
function peekKey(key) { | ||
var entry = cache.get(key); | ||
if (entry) { | ||
return entry.peek(); | ||
} | ||
} | ||
optimistic.peekKey = peekKey; | ||
optimistic.peek = function peek() { | ||
return peekKey(makeCacheKey.apply(null, arguments)); | ||
}; | ||
optimistic.forget = function () { | ||
var key = makeCacheKey.apply(null, arguments); | ||
return key !== void 0 && cache.delete(key); | ||
function forgetKey(key) { | ||
return cache.delete(key); | ||
} | ||
optimistic.forgetKey = forgetKey; | ||
optimistic.forget = function forget() { | ||
return forgetKey(makeCacheKey.apply(null, arguments)); | ||
}; | ||
return optimistic; | ||
optimistic.makeCacheKey = makeCacheKey; | ||
optimistic.getKey = keyArgs ? function getKey() { | ||
return makeCacheKey.apply(null, keyArgs.apply(null, arguments)); | ||
} : makeCacheKey; | ||
return Object.freeze(optimistic); | ||
} | ||
@@ -476,0 +507,0 @@ |
@@ -94,2 +94,3 @@ import { Trie } from '@wry/trie'; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
function maybeUnsubscribe(entryOrDep) { | ||
@@ -149,2 +150,3 @@ var unsubscribe = entryOrDep.unsubscribe; | ||
if (this.value.length === 1 && !mightBeDirty(this)) { | ||
rememberParent(this); | ||
return this.value[0]; | ||
@@ -180,4 +182,3 @@ } | ||
var _this = this; | ||
forgetChildren(this); | ||
maybeUnsubscribe(this); | ||
this.setDirty(); | ||
// Because this entry has been kicked out of the cache (in index.js), | ||
@@ -199,2 +200,8 @@ // we've lost the ability to find out if/when this entry becomes dirty, | ||
}; | ||
Entry.prototype.forget = function () { | ||
// The code that creates Entry objects in index.ts will replace this method | ||
// with one that actually removes the Entry from the cache, which will also | ||
// trigger the entry.dispose method. | ||
this.dispose(); | ||
}; | ||
Entry.prototype.dependOn = function (dep) { | ||
@@ -285,2 +292,3 @@ dep.add(this); | ||
assert(mightBeDirty(child)); | ||
var parentWasClean = !mightBeDirty(parent); | ||
if (!parent.dirtyChildren) { | ||
@@ -296,3 +304,7 @@ parent.dirtyChildren = emptySetPool.pop() || new Set; | ||
parent.dirtyChildren.add(child); | ||
reportDirty(parent); | ||
// If parent was clean before, it just became (possibly) dirty (according to | ||
// mightBeDirty), since we just added child to parent.dirtyChildren. | ||
if (parentWasClean) { | ||
reportDirty(parent); | ||
} | ||
} | ||
@@ -370,2 +382,7 @@ // Let a parent Entry know that one of its children is no longer dirty. | ||
var EntryMethods = { | ||
setDirty: true, | ||
dispose: true, | ||
forget: true, | ||
}; | ||
function dep(options) { | ||
@@ -388,6 +405,8 @@ var depsByKey = new Map(); | ||
} | ||
depend.dirty = function dirty(key) { | ||
depend.dirty = function dirty(key, entryMethodName) { | ||
var dep = depsByKey.get(key); | ||
if (dep) { | ||
dep.forEach(function (entry) { return entry.setDirty(); }); | ||
var m_1 = (entryMethodName && | ||
hasOwnProperty.call(EntryMethods, entryMethodName)) ? entryMethodName : "setDirty"; | ||
dep.forEach(function (entry) { return entry[m_1](); }); | ||
depsByKey.delete(key); | ||
@@ -400,2 +419,8 @@ maybeUnsubscribe(dep); | ||
function makeDefaultMakeCacheKeyFunction() { | ||
var keyTrie = new Trie(typeof WeakMap === "function"); | ||
return function () { | ||
return keyTrie.lookupArray(arguments); | ||
}; | ||
} | ||
// The defaultMakeCacheKey function is remarkably powerful, because it gives | ||
@@ -408,10 +433,3 @@ // a unique object for any shallow-identical list of arguments. If you need | ||
// In those cases, just write your own custom makeCacheKey functions. | ||
var keyTrie = new Trie(typeof WeakMap === "function"); | ||
function defaultMakeCacheKey() { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return keyTrie.lookupArray(args); | ||
} | ||
var defaultMakeCacheKey = makeDefaultMakeCacheKeyFunction(); | ||
var caches = new Set(); | ||
@@ -422,4 +440,5 @@ function wrap(originalFunction, options) { | ||
var keyArgs = options.keyArgs; | ||
var makeCacheKey = options.makeCacheKey || defaultMakeCacheKey; | ||
function optimistic() { | ||
var makeCacheKey = options.makeCacheKey || | ||
makeDefaultMakeCacheKeyFunction(); | ||
var optimistic = function () { | ||
var key = makeCacheKey.apply(null, keyArgs ? keyArgs.apply(null, arguments) : arguments); | ||
@@ -433,2 +452,5 @@ if (key === void 0) { | ||
entry.subscribe = options.subscribe; | ||
// Give the Entry the ability to trigger cache.delete(key), even though | ||
// the Entry itself does not know about key or cache. | ||
entry.forget = function () { return cache.delete(key); }; | ||
} | ||
@@ -448,26 +470,35 @@ var value = entry.recompute(Array.prototype.slice.call(arguments)); | ||
return value; | ||
} | ||
function lookup() { | ||
var key = makeCacheKey.apply(null, arguments); | ||
if (key !== void 0) { | ||
return cache.get(key); | ||
} | ||
} | ||
optimistic.dirty = function () { | ||
var entry = lookup.apply(null, arguments); | ||
}; | ||
function dirtyKey(key) { | ||
var entry = cache.get(key); | ||
if (entry) { | ||
entry.setDirty(); | ||
} | ||
} | ||
optimistic.dirtyKey = dirtyKey; | ||
optimistic.dirty = function dirty() { | ||
dirtyKey(makeCacheKey.apply(null, arguments)); | ||
}; | ||
optimistic.peek = function () { | ||
var entry = lookup.apply(null, arguments); | ||
function peekKey(key) { | ||
var entry = cache.get(key); | ||
if (entry) { | ||
return entry.peek(); | ||
} | ||
} | ||
optimistic.peekKey = peekKey; | ||
optimistic.peek = function peek() { | ||
return peekKey(makeCacheKey.apply(null, arguments)); | ||
}; | ||
optimistic.forget = function () { | ||
var key = makeCacheKey.apply(null, arguments); | ||
return key !== void 0 && cache.delete(key); | ||
function forgetKey(key) { | ||
return cache.delete(key); | ||
} | ||
optimistic.forgetKey = forgetKey; | ||
optimistic.forget = function forget() { | ||
return forgetKey(makeCacheKey.apply(null, arguments)); | ||
}; | ||
return optimistic; | ||
optimistic.makeCacheKey = makeCacheKey; | ||
optimistic.getKey = keyArgs ? function getKey() { | ||
return makeCacheKey.apply(null, keyArgs.apply(null, arguments)); | ||
} : makeCacheKey; | ||
return Object.freeze(optimistic); | ||
} | ||
@@ -474,0 +505,0 @@ |
import { AnyEntry } from "./entry"; | ||
import { OptimisticWrapOptions } from "./index"; | ||
import { Unsubscribable } from "./helpers"; | ||
declare type EntryMethodName = keyof typeof EntryMethods; | ||
declare const EntryMethods: { | ||
setDirty: boolean; | ||
dispose: boolean; | ||
forget: boolean; | ||
}; | ||
export declare type OptimisticDependencyFunction<TKey> = ((key: TKey) => void) & { | ||
dirty: (key: TKey) => void; | ||
dirty: (key: TKey, entryMethodName?: EntryMethodName) => void; | ||
}; | ||
@@ -13,1 +19,2 @@ export declare type Dep<TKey> = Set<AnyEntry> & { | ||
}): OptimisticDependencyFunction<TKey>; | ||
export {}; |
@@ -22,2 +22,3 @@ import { OptimisticWrapOptions } from "./index"; | ||
dispose(): void; | ||
forget(): void; | ||
private deps; | ||
@@ -24,0 +25,0 @@ dependOn(dep: Dep<any>): void; |
@@ -0,1 +1,2 @@ | ||
export declare const hasOwnProperty: (v: string | number | symbol) => boolean; | ||
export declare type Unsubscribable = { | ||
@@ -2,0 +3,0 @@ unsubscribe?: void | (() => any); |
import { Trie } from "@wry/trie"; | ||
export { bindContext, noContext, setTimeout, asyncFromGen, } from "./context"; | ||
export { dep, OptimisticDependencyFunction } from "./dep"; | ||
export declare type TCacheKey = any; | ||
export declare function defaultMakeCacheKey(...args: any[]): any; | ||
export declare const defaultMakeCacheKey: (...args: any[]) => any; | ||
export { Trie as KeyTrie }; | ||
export declare type OptimisticWrapperFunction<TArgs extends any[], TResult, TKeyArgs extends any[] = TArgs> = ((...args: TArgs) => TResult) & { | ||
export declare type OptimisticWrapperFunction<TArgs extends any[], TResult, TKeyArgs extends any[] = TArgs, TCacheKey = any> = ((...args: TArgs) => TResult) & { | ||
dirty: (...args: TKeyArgs) => void; | ||
dirtyKey: (key: TCacheKey) => void; | ||
peek: (...args: TKeyArgs) => TResult | undefined; | ||
peekKey: (key: TCacheKey) => TResult | undefined; | ||
forget: (...args: TKeyArgs) => boolean; | ||
forgetKey: (key: TCacheKey) => boolean; | ||
getKey: (...args: TArgs) => TCacheKey; | ||
makeCacheKey: (...args: TKeyArgs) => TCacheKey; | ||
}; | ||
export declare type OptimisticWrapOptions<TArgs extends any[], TKeyArgs extends any[] = TArgs> = { | ||
export declare type OptimisticWrapOptions<TArgs extends any[], TKeyArgs extends any[] = TArgs, TCacheKey = any> = { | ||
max?: number; | ||
@@ -18,2 +22,2 @@ keyArgs?: (...args: TArgs) => TKeyArgs; | ||
}; | ||
export declare function wrap<TArgs extends any[], TResult, TKeyArgs extends any[] = TArgs>(originalFunction: (...args: TArgs) => TResult, options?: OptimisticWrapOptions<TArgs, TKeyArgs>): OptimisticWrapperFunction<TArgs, TResult, TKeyArgs>; | ||
export declare function wrap<TArgs extends any[], TResult, TKeyArgs extends any[] = TArgs, TCacheKey = any>(originalFunction: (...args: TArgs) => TResult, options?: OptimisticWrapOptions<TArgs, TKeyArgs>): OptimisticWrapperFunction<TArgs, TResult, TKeyArgs, TCacheKey>; |
{ | ||
"name": "optimism", | ||
"version": "0.15.0", | ||
"version": "0.16.0", | ||
"author": "Ben Newman <ben@benjamn.com>", | ||
@@ -38,3 +38,3 @@ "description": "Composable reactive caching with efficient invalidation.", | ||
"@types/mocha": "^8.0.3", | ||
"@types/node": "^14.11.2", | ||
"@types/node": "^15.0.2", | ||
"fibers": "^5.0.0", | ||
@@ -41,0 +41,0 @@ "mocha": "^8.1.3", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
116986
10.8%1108
7.26%