Comparing version
@@ -95,2 +95,10 @@ 'use strict'; | ||
function maybeUnsubscribe(entryOrDep) { | ||
var unsubscribe = entryOrDep.unsubscribe; | ||
if (typeof unsubscribe === "function") { | ||
entryOrDep.unsubscribe = void 0; | ||
unsubscribe(); | ||
} | ||
} | ||
var emptySetPool = []; | ||
@@ -138,3 +146,3 @@ var POOL_TARGET_SIZE = 100; | ||
this.value = []; | ||
this.sets = null; | ||
this.deps = null; | ||
++Entry.count; | ||
@@ -187,16 +195,16 @@ } | ||
}; | ||
Entry.prototype.addToSet = function (entrySet) { | ||
entrySet.add(this); | ||
if (!this.sets) { | ||
this.sets = emptySetPool.pop() || new Set(); | ||
Entry.prototype.dependOn = function (dep) { | ||
dep.add(this); | ||
if (!this.deps) { | ||
this.deps = emptySetPool.pop() || new Set(); | ||
} | ||
this.sets.add(entrySet); | ||
this.deps.add(dep); | ||
}; | ||
Entry.prototype.removeFromSets = function () { | ||
Entry.prototype.forgetDeps = function () { | ||
var _this = this; | ||
if (this.sets) { | ||
this.sets.forEach(function (set) { return set.delete(_this); }); | ||
this.sets.clear(); | ||
emptySetPool.push(this.sets); | ||
this.sets = null; | ||
if (this.deps) { | ||
this.deps.forEach(function (dep) { return dep.delete(_this); }); | ||
this.deps.clear(); | ||
emptySetPool.push(this.deps); | ||
this.deps = null; | ||
} | ||
@@ -326,3 +334,3 @@ }; | ||
// addToSet method. | ||
parent.removeFromSets(); | ||
parent.forgetDeps(); | ||
// After we forget all our children, this.dirtyChildren must be empty | ||
@@ -356,9 +364,2 @@ // and therefore must have been reset to null. | ||
} | ||
function maybeUnsubscribe(entry) { | ||
var unsubscribe = entry.unsubscribe; | ||
if (typeof unsubscribe === "function") { | ||
entry.unsubscribe = void 0; | ||
unsubscribe(); | ||
} | ||
} | ||
@@ -406,2 +407,30 @@ // A trie data structure that holds object keys weakly, yet can also hold | ||
function dep(options) { | ||
var depsByKey = new Map(); | ||
var subscribe = options && options.subscribe; | ||
function depend(key) { | ||
var parent = parentEntrySlot.getValue(); | ||
if (parent) { | ||
var dep_1 = depsByKey.get(key); | ||
if (!dep_1) { | ||
depsByKey.set(key, dep_1 = new Set); | ||
} | ||
parent.dependOn(dep_1); | ||
if (typeof subscribe === "function") { | ||
maybeUnsubscribe(dep_1); | ||
dep_1.unsubscribe = subscribe(key); | ||
} | ||
} | ||
} | ||
depend.dirty = function (key) { | ||
var dep = depsByKey.get(key); | ||
if (dep) { | ||
dep.forEach(function (entry) { return entry.setDirty(); }); | ||
depsByKey.delete(key); | ||
maybeUnsubscribe(dep); | ||
} | ||
}; | ||
return depend; | ||
} | ||
// The defaultMakeCacheKey function is remarkably powerful, because it gives | ||
@@ -465,23 +494,2 @@ // a unique object for any shallow-identical list of arguments. If you need | ||
} | ||
function dep() { | ||
var parentEntriesByKey = new Map(); | ||
function depend(key) { | ||
var parent = parentEntrySlot.getValue(); | ||
if (parent) { | ||
var parentEntrySet = parentEntriesByKey.get(key); | ||
if (!parentEntrySet) { | ||
parentEntriesByKey.set(key, parentEntrySet = new Set); | ||
} | ||
parent.addToSet(parentEntrySet); | ||
} | ||
} | ||
depend.dirty = function (key) { | ||
var parentEntrySet = parentEntriesByKey.get(key); | ||
if (parentEntrySet) { | ||
parentEntrySet.forEach(function (entry) { return entry.setDirty(); }); | ||
parentEntriesByKey.delete(key); | ||
} | ||
}; | ||
return depend; | ||
} | ||
@@ -488,0 +496,0 @@ Object.defineProperty(exports, 'asyncFromGen', { |
@@ -92,2 +92,10 @@ import { Slot } from '@wry/context'; | ||
function maybeUnsubscribe(entryOrDep) { | ||
var unsubscribe = entryOrDep.unsubscribe; | ||
if (typeof unsubscribe === "function") { | ||
entryOrDep.unsubscribe = void 0; | ||
unsubscribe(); | ||
} | ||
} | ||
var emptySetPool = []; | ||
@@ -135,3 +143,3 @@ var POOL_TARGET_SIZE = 100; | ||
this.value = []; | ||
this.sets = null; | ||
this.deps = null; | ||
++Entry.count; | ||
@@ -184,16 +192,16 @@ } | ||
}; | ||
Entry.prototype.addToSet = function (entrySet) { | ||
entrySet.add(this); | ||
if (!this.sets) { | ||
this.sets = emptySetPool.pop() || new Set(); | ||
Entry.prototype.dependOn = function (dep) { | ||
dep.add(this); | ||
if (!this.deps) { | ||
this.deps = emptySetPool.pop() || new Set(); | ||
} | ||
this.sets.add(entrySet); | ||
this.deps.add(dep); | ||
}; | ||
Entry.prototype.removeFromSets = function () { | ||
Entry.prototype.forgetDeps = function () { | ||
var _this = this; | ||
if (this.sets) { | ||
this.sets.forEach(function (set) { return set.delete(_this); }); | ||
this.sets.clear(); | ||
emptySetPool.push(this.sets); | ||
this.sets = null; | ||
if (this.deps) { | ||
this.deps.forEach(function (dep) { return dep.delete(_this); }); | ||
this.deps.clear(); | ||
emptySetPool.push(this.deps); | ||
this.deps = null; | ||
} | ||
@@ -323,3 +331,3 @@ }; | ||
// addToSet method. | ||
parent.removeFromSets(); | ||
parent.forgetDeps(); | ||
// After we forget all our children, this.dirtyChildren must be empty | ||
@@ -353,9 +361,2 @@ // and therefore must have been reset to null. | ||
} | ||
function maybeUnsubscribe(entry) { | ||
var unsubscribe = entry.unsubscribe; | ||
if (typeof unsubscribe === "function") { | ||
entry.unsubscribe = void 0; | ||
unsubscribe(); | ||
} | ||
} | ||
@@ -403,2 +404,30 @@ // A trie data structure that holds object keys weakly, yet can also hold | ||
function dep(options) { | ||
var depsByKey = new Map(); | ||
var subscribe = options && options.subscribe; | ||
function depend(key) { | ||
var parent = parentEntrySlot.getValue(); | ||
if (parent) { | ||
var dep_1 = depsByKey.get(key); | ||
if (!dep_1) { | ||
depsByKey.set(key, dep_1 = new Set); | ||
} | ||
parent.dependOn(dep_1); | ||
if (typeof subscribe === "function") { | ||
maybeUnsubscribe(dep_1); | ||
dep_1.unsubscribe = subscribe(key); | ||
} | ||
} | ||
} | ||
depend.dirty = function (key) { | ||
var dep = depsByKey.get(key); | ||
if (dep) { | ||
dep.forEach(function (entry) { return entry.setDirty(); }); | ||
depsByKey.delete(key); | ||
maybeUnsubscribe(dep); | ||
} | ||
}; | ||
return depend; | ||
} | ||
// The defaultMakeCacheKey function is remarkably powerful, because it gives | ||
@@ -462,25 +491,4 @@ // a unique object for any shallow-identical list of arguments. If you need | ||
} | ||
function dep() { | ||
var parentEntriesByKey = new Map(); | ||
function depend(key) { | ||
var parent = parentEntrySlot.getValue(); | ||
if (parent) { | ||
var parentEntrySet = parentEntriesByKey.get(key); | ||
if (!parentEntrySet) { | ||
parentEntriesByKey.set(key, parentEntrySet = new Set); | ||
} | ||
parent.addToSet(parentEntrySet); | ||
} | ||
} | ||
depend.dirty = function (key) { | ||
var parentEntrySet = parentEntriesByKey.get(key); | ||
if (parentEntrySet) { | ||
parentEntrySet.forEach(function (entry) { return entry.setDirty(); }); | ||
parentEntriesByKey.delete(key); | ||
} | ||
}; | ||
return depend; | ||
} | ||
export { KeyTrie, defaultMakeCacheKey, dep, wrap }; | ||
//# sourceMappingURL=bundle.esm.js.map |
import { OptimisticWrapOptions } from "./index"; | ||
import { Dep } from "./dep"; | ||
import { Unsubscribable } from "./helpers"; | ||
declare type Value<T> = [] | [T] | [void, any]; | ||
@@ -9,3 +11,3 @@ export declare type AnyEntry = Entry<any, any>; | ||
subscribe: OptimisticWrapOptions<TArgs>["subscribe"]; | ||
unsubscribe?: () => any; | ||
unsubscribe: Unsubscribable["unsubscribe"]; | ||
readonly parents: Set<Entry<any, any>>; | ||
@@ -21,6 +23,6 @@ readonly childValues: Map<Entry<any, any>, Value<any>>; | ||
dispose(): void; | ||
private sets; | ||
addToSet(entrySet: Set<AnyEntry>): void; | ||
removeFromSets(): void; | ||
private deps; | ||
dependOn(dep: Dep<any>): void; | ||
forgetDeps(): void; | ||
} | ||
export {}; |
import { KeyTrie } from "./key-trie"; | ||
export { bindContext, noContext, setTimeout, asyncFromGen, } from "./context"; | ||
export { dep } from "./dep"; | ||
export declare type TCacheKey = any; | ||
@@ -9,11 +10,7 @@ export declare function defaultMakeCacheKey(...args: any[]): any; | ||
}; | ||
export declare type OptimisticDependencyFunction<TKey> = ((key: TKey) => void) & { | ||
dirty: (key: TKey) => void; | ||
}; | ||
export declare type OptimisticWrapOptions<TArgs extends any[]> = { | ||
max?: number; | ||
makeCacheKey?: (...args: TArgs) => TCacheKey; | ||
subscribe?: (...args: TArgs) => (() => any) | undefined; | ||
subscribe?: (...args: TArgs) => void | (() => any); | ||
}; | ||
export declare function wrap<TArgs extends any[], TResult>(originalFunction: (...args: TArgs) => TResult, options?: OptimisticWrapOptions<TArgs>): OptimisticWrapperFunction<TArgs, TResult>; | ||
export declare function dep<TKey>(): OptimisticDependencyFunction<TKey>; |
{ | ||
"name": "optimism", | ||
"version": "0.11.1", | ||
"version": "0.11.2", | ||
"author": "Ben Newman <ben@benjamn.com>", | ||
@@ -5,0 +5,0 @@ "description": "Composable reactive caching with efficient invalidation.", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
107241
3.15%15
15.38%1081
2.56%