-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support [un]subscribe API for lightweight deps.
- Loading branch information
Showing
5 changed files
with
144 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { AnyEntry } from "./entry"; | ||
import { OptimisticWrapOptions } from "."; | ||
import { parentEntrySlot } from "./context"; | ||
import { Unsubscribable, maybeUnsubscribe } from "./helpers"; | ||
|
||
export type OptimisticDependencyFunction<TKey> = | ||
((key: TKey) => void) & { | ||
dirty: (key: TKey) => void; | ||
}; | ||
|
||
export type Dep<TKey> = Set<AnyEntry> & { | ||
subscribe: OptimisticWrapOptions<[TKey]>["subscribe"]; | ||
} & Unsubscribable; | ||
|
||
export function dep<TKey>(options?: { | ||
subscribe: Dep<TKey>["subscribe"]; | ||
}) { | ||
const depsByKey = new Map<TKey, Dep<TKey>>(); | ||
const subscribe = options && options.subscribe; | ||
|
||
function depend(key: TKey) { | ||
const parent = parentEntrySlot.getValue(); | ||
if (parent) { | ||
let dep = depsByKey.get(key); | ||
if (!dep) { | ||
depsByKey.set(key, dep = new Set as Dep<TKey>); | ||
} | ||
parent.dependOn(dep); | ||
if (typeof subscribe === "function") { | ||
maybeUnsubscribe(dep); | ||
dep.unsubscribe = subscribe(key); | ||
} | ||
} | ||
} | ||
|
||
depend.dirty = function(key: TKey) { | ||
const dep = depsByKey.get(key); | ||
if (dep) { | ||
dep.forEach(entry => entry.setDirty()); | ||
depsByKey.delete(key); | ||
maybeUnsubscribe(dep); | ||
} | ||
}; | ||
|
||
return depend as OptimisticDependencyFunction<TKey>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export type Unsubscribable = { | ||
unsubscribe?: void | (() => any); | ||
} | ||
|
||
export function maybeUnsubscribe(entryOrDep: Unsubscribable) { | ||
const { unsubscribe } = entryOrDep; | ||
if (typeof unsubscribe === "function") { | ||
entryOrDep.unsubscribe = void 0; | ||
unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters