Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/reduxjs/reselect into mig…
Browse files Browse the repository at this point in the history
…rate-typetests-to-vitest
  • Loading branch information
aryaemami59 committed Aug 13, 2024
2 parents d7a07ff + 88e7ffd commit 282544d
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 347 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:

# Note: We currently expect "FalseCJS" failures for Node16 + `moduleResolution: "node16",
- name: Run are-the-types-wrong
run: npx @arethetypeswrong/cli ./package.tgz --format table --ignore-rules false-cjs
run: npx @arethetypeswrong/cli@latest ./package.tgz --format table --ignore-rules false-cjs

test-published-artifact:
name: Test Published Artifact ${{ matrix.example }}
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "reselect",
"version": "5.1.1",
"description": "Selectors for Redux.",
"main": "./dist/cjs/reselect.cjs",
"main": "./dist/cjs/index.js",
"module": "./dist/reselect.legacy-esm.js",
"types": "./dist/reselect.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/reselect.d.ts",
"import": "./dist/reselect.mjs",
"default": "./dist/cjs/reselect.cjs"
"default": "./dist/cjs/index.js"
}
},
"files": [
Expand Down Expand Up @@ -85,5 +85,8 @@
"typescript": "^5.4.5",
"vitest": "^1.6.0"
},
"resolutions": {
"esbuild": "0.23.0"
},
"packageManager": "yarn@4.1.0"
}
4 changes: 2 additions & 2 deletions src/autotrackMemoize/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
dirtyTag
} from './tracking'

export const REDUX_PROXY_LABEL = Symbol()
export const REDUX_PROXY_LABEL = /* @__PURE__ */ Symbol()

let nextId = 0

const proto = Object.getPrototypeOf({})
const proto = /* @__PURE__ */ Object.getPrototypeOf({})

class ObjectTreeNode<T extends Record<string, unknown>> implements Node<T> {
proxy: T = new Proxy(this, objectProxyHandler) as unknown as T
Expand Down
4 changes: 2 additions & 2 deletions src/createSelectorCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ export function createSelectorCreator<
memoize,
memoizeOptions = [],
argsMemoize = weakMapMemoize,
argsMemoizeOptions = [],
devModeChecks = {}
argsMemoizeOptions = []
} = combinedOptions

// Simplifying assumption: it's unlikely that the first options arg of the provided memoizer
Expand Down Expand Up @@ -412,6 +411,7 @@ export function createSelectorCreator<
lastResult = memoizedResultFunc.apply(null, inputSelectorResults)

if (process.env.NODE_ENV !== 'production') {
const { devModeChecks = {} } = combinedOptions
const { identityFunctionCheck, inputStabilityCheck } =
getDevModeChecksExecutionInfo(firstRun, devModeChecks)
if (identityFunctionCheck.shouldRun) {
Expand Down
2 changes: 1 addition & 1 deletion src/createStructuredSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export interface StructuredSelectorCreator<StateType = any> {
* @public
*/
export const createStructuredSelector: StructuredSelectorCreator =
Object.assign(
/* @__PURE__ */ Object.assign(
<
InputSelectorsObject extends SelectorsObject,
MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize,
Expand Down
35 changes: 29 additions & 6 deletions src/weakMapMemoize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ class StrongRef<T> {
}
}

const Ref =
typeof WeakRef !== 'undefined'
? WeakRef
: (StrongRef as unknown as typeof WeakRef)
/**
* @returns The {@linkcode StrongRef} if {@linkcode WeakRef} is not available.
*
* @since 5.1.2
* @internal
*/
const getWeakRef = () =>
typeof WeakRef === 'undefined'
? (StrongRef as unknown as typeof WeakRef)
: WeakRef

const Ref = /* @__PURE__ */ getWeakRef()

const UNTERMINATED = 0
const TERMINATED = 1
Expand Down Expand Up @@ -96,6 +104,20 @@ export interface WeakMapMemoizeOptions<Result = any> {
resultEqualityCheck?: EqualityFn<Result>
}

/**
* Derefences the argument if it is a Ref. Else if it is a value already, return it.
*
* @param r - the object to maybe deref
* @returns The derefenced value if the argument is a Ref, else the argument value itself.
*/
function maybeDeref(r: any) {
if (r instanceof Ref) {
return r.deref()
}

return r
}

/**
* Creates a tree of `WeakMap`-based cache nodes based on the identity of the
* arguments it's been called with (in this case, the extracted values from your input selectors).
Expand Down Expand Up @@ -229,7 +251,8 @@ export function weakMapMemoize<Func extends AnyFunction>(
resultsCount++

if (resultEqualityCheck) {
const lastResultValue = lastResult?.deref?.() ?? lastResult
// Deref lastResult if it is a Ref
const lastResultValue = maybeDeref(lastResult)

if (
lastResultValue != null &&
Expand All @@ -244,7 +267,7 @@ export function weakMapMemoize<Func extends AnyFunction>(
(typeof result === 'object' && result !== null) ||
typeof result === 'function'

lastResult = needsWeakRef ? new Ref(result) : result
lastResult = needsWeakRef ? /* @__PURE__ */ new Ref(result) : result
}
}

Expand Down
65 changes: 57 additions & 8 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import type { Options } from 'tsup'
import { defineConfig } from 'tsup'

async function writeCommonJSEntry() {
await fs.writeFile(
path.join('dist/cjs/', 'index.js'),
`'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./reselect.production.min.cjs')
} else {
module.exports = require('./reselect.development.cjs')
}`
)
}

const tsconfig = 'tsconfig.build.json' satisfies Options['tsconfig']

export default defineConfig((options): Options[] => {
Expand All @@ -10,48 +24,83 @@ export default defineConfig((options): Options[] => {
},
sourcemap: true,
tsconfig,
target: ['esnext'],
clean: true,
...options
}

return [
// Modern ESM
{
...commonOptions,
name: 'Modern ESM',
target: ['esnext'],
format: ['esm'],
outExtension: () => ({ js: '.mjs' }),
dts: true,
clean: true
outExtension: () => ({ js: '.mjs' })
},

// Support Webpack 4 by pointing `"module"` to a file with a `.js` extension
// and optional chaining compiled away
{
...commonOptions,
name: 'Legacy ESM, Webpack 4',
entry: {
'reselect.legacy-esm': 'src/index.ts'
},
format: ['esm'],
outExtension: () => ({ js: '.js' }),
target: 'es2017'
target: ['es2017']
},
// Browser-ready ESM, production + minified

// Meant to be served up via CDNs like `unpkg`.
{
...commonOptions,
name: 'Browser-ready ESM',
entry: {
'reselect.browser': 'src/index.ts'
},
define: {
'process.env.NODE_ENV': JSON.stringify('production')
platform: 'browser',
env: {
NODE_ENV: 'production'
},
format: ['esm'],
outExtension: () => ({ js: '.mjs' }),
minify: true
},
{
...commonOptions,
name: 'CJS Development',
entry: {
'reselect.development': 'src/index.ts'
},
env: {
NODE_ENV: 'development'
},
format: ['cjs'],
outDir: './dist/cjs/',
outExtension: () => ({ js: '.cjs' })
},
{
...commonOptions,
name: 'CJS production',
entry: {
'reselect.production.min': 'src/index.ts'
},
env: {
NODE_ENV: 'production'
},
format: ['cjs'],
outDir: './dist/cjs/',
outExtension: () => ({ js: '.cjs' }),
minify: true,
onSuccess: async () => {
await writeCommonJSEntry()
}
},
{
...commonOptions,
name: 'CJS Type Definitions',
format: ['cjs'],
dts: { only: true }
}
]
})
Loading

0 comments on commit 282544d

Please sign in to comment.