From c76076775ef5eae1932e3f2e050f059b31f36942 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 17 May 2023 14:48:50 -0400 Subject: [PATCH 1/2] Rename toArray to arrayFromSet and simplify export syntax. --- src/dep.ts | 11 ++++++++--- src/entry.ts | 6 +++--- src/helpers.ts | 15 +++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/dep.ts b/src/dep.ts index fb9b982..57b47a0 100644 --- a/src/dep.ts +++ b/src/dep.ts @@ -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 = { @@ -50,10 +55,10 @@ export function dep(options?: { entryMethodName && hasOwnProperty.call(EntryMethods, entryMethodName) ) ? entryMethodName : "setDirty"; - // We have to use toArray(dep).forEach instead of dep.forEach, because + // We have to use setToArray(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]()); + arrayFromSet(dep).forEach(entry => entry[m]()); depsByKey.delete(key); maybeUnsubscribe(dep); } diff --git a/src/entry.ts b/src/entry.ts index 31329ef..2646e2a 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -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[] = []; const POOL_TARGET_SIZE = 100; @@ -147,7 +147,7 @@ export class Entry { 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; @@ -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); } diff --git a/src/helpers.ts b/src/helpers.ts index 338818a..1a4dad5 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -2,18 +2,13 @@ export const { hasOwnProperty, } = Object.prototype; -export const { - // This Array.from polyfill is restricted to working with Set 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) => { +export const arrayFromSet: (set: Set) => 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); From 33862610ab5d730441088a4c633184d2d0b3e70f Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 17 May 2023 15:16:26 -0400 Subject: [PATCH 2/2] Fix comment mentioning setToArray. --- src/dep.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dep.ts b/src/dep.ts index 57b47a0..d5aa444 100644 --- a/src/dep.ts +++ b/src/dep.ts @@ -55,9 +55,9 @@ export function dep(options?: { entryMethodName && hasOwnProperty.call(EntryMethods, entryMethodName) ) ? entryMethodName : "setDirty"; - // We have to use setToArray(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. + // 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);