Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename toArray to arrayFromSet and simplify export syntax #550

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/dep.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { AnyEntry } from "./entry.js";
import { OptimisticWrapOptions } from "./index.js";
import { parentEntrySlot } from "./context.js";
import { hasOwnProperty, Unsubscribable, maybeUnsubscribe, toArray } from "./helpers.js";
import {
hasOwnProperty,
Unsubscribable,
maybeUnsubscribe,
arrayFromSet,
} from "./helpers.js";

type EntryMethodName = keyof typeof EntryMethods;
const EntryMethods = {
Expand Down Expand Up @@ -50,10 +55,10 @@ export function dep<TKey>(options?: {
entryMethodName &&
hasOwnProperty.call(EntryMethods, entryMethodName)
) ? entryMethodName : "setDirty";
// We have to use toArray(dep).forEach instead of dep.forEach, because
// modifying a Set while iterating over it can cause elements in the Set
// to be removed from the Set before they've been iterated over.
toArray(dep).forEach(entry => entry[m]());
// We have to use arrayFromSet(dep).forEach instead of dep.forEach,
// because modifying a Set while iterating over it can cause elements in
// the Set to be removed from the Set before they've been iterated over.
arrayFromSet(dep).forEach(entry => entry[m]());
depsByKey.delete(key);
maybeUnsubscribe(dep);
}
Expand Down
6 changes: 3 additions & 3 deletions src/entry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parentEntrySlot } from "./context.js";
import { OptimisticWrapOptions } from "./index.js";
import { Dep } from "./dep.js";
import { maybeUnsubscribe, toArray, Unsubscribable } from "./helpers.js";
import { maybeUnsubscribe, arrayFromSet, Unsubscribable } from "./helpers.js";

const emptySetPool: Set<any>[] = [];
const POOL_TARGET_SIZE = 100;
Expand Down Expand Up @@ -147,7 +147,7 @@ export class Entry<TArgs extends any[], TValue> {

public forgetDeps() {
if (this.deps) {
toArray(this.deps).forEach(dep => dep.delete(this));
arrayFromSet(this.deps).forEach(dep => dep.delete(this));
this.deps.clear();
emptySetPool.push(this.deps);
this.deps = null;
Expand Down Expand Up @@ -234,7 +234,7 @@ function eachParent(
) {
const parentCount = child.parents.size;
if (parentCount) {
const parents = toArray(child.parents);
const parents = arrayFromSet(child.parents);
for (let i = 0; i < parentCount; ++i) {
callback(parents[i], child);
}
Expand Down
15 changes: 5 additions & 10 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@ export const {
hasOwnProperty,
} = Object.prototype;

export const {
// This Array.from polyfill is restricted to working with Set<any> for now,
// but we can improve the polyfill and add other input types, as needed. Note
// that this fallback implementation will only be used if the host environment
// does not support a native Array.from function. In most modern JS runtimes,
// the toArray function exported here will be === Array.from.
from: toArray = (collection: Set<any>) => {
export const arrayFromSet: <T>(set: Set<T>) => T[] =
Array.from ||
function (set) {
const array: any[] = [];
collection.forEach(item => array.push(item));
set.forEach(item => array.push(item));
return array;
},
} = Array;
};

export type Unsubscribable = {
unsubscribe?: void | (() => any);
Expand Down