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

chore: handle declare statements in module rewriting #9249

Merged
merged 4 commits into from
Mar 10, 2024
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
42 changes: 39 additions & 3 deletions packages/graph/src/-private.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
export { graphFor, peekGraph } from './-private/index';
export { isBelongsTo } from './-private/-utils';

/**
* <p align="center">
<img
Expand Down Expand Up @@ -29,3 +26,42 @@ pnpm add @ember-data/graph
@module @ember-data/graph
@main @ember-data/graph
*/
import { DEBUG } from '@ember-data/env';
import type Store from '@ember-data/store';
import type { CacheCapabilitiesManager } from '@ember-data/store/-types/q/cache-store-wrapper';

import { getStore } from './-private/-utils';
import { Graph, Graphs } from './-private/graph';

export { isBelongsTo } from './-private/-utils';

function isStore(maybeStore: unknown): maybeStore is Store {
return (maybeStore as Store)._instanceCache !== undefined;
}

function getWrapper(store: CacheCapabilitiesManager | Store): CacheCapabilitiesManager {
return isStore(store) ? store._instanceCache._storeWrapper : store;
}

export function peekGraph(store: CacheCapabilitiesManager | Store): Graph | undefined {
return Graphs.get(getWrapper(store));
}
export type peekGraph = typeof peekGraph;

export function graphFor(store: CacheCapabilitiesManager | Store): Graph {
const wrapper = getWrapper(store);
let graph = Graphs.get(wrapper);

if (!graph) {
graph = new Graph(wrapper);
Graphs.set(wrapper, graph);
getStore(wrapper)._graph = graph;

if (DEBUG) {
if (getStore(wrapper).isDestroying) {
throw new Error(`Memory Leak Detected During Teardown`);
}
}
}
return graph;
}
37 changes: 0 additions & 37 deletions packages/graph/src/-private/index.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/model/src/-private/many-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { assert, deprecate } from '@ember/debug';

import { DEPRECATE_MANY_ARRAY_DUPLICATES } from '@ember-data/deprecations';
import type Store from '@ember-data/store';
import type { NativeProxy } from '@ember-data/store/-private';
import {
ARRAY_SIGNAL,
isStableIdentifier,
Expand Down Expand Up @@ -171,7 +172,7 @@ export default class RelatedCollection<T = unknown> extends RecordArray<T> {

[MUTATE](
target: StableRecordIdentifier[],
receiver: typeof Proxy<StableRecordIdentifier[], T[]>,
receiver: typeof NativeProxy<StableRecordIdentifier[], T[]>,
prop: string,
args: unknown[],
_SIGNAL: Signal
Expand Down
42 changes: 41 additions & 1 deletion packages/store/src/-private.ts
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
export * from './-private/index';
/**
@module @ember-data/store
*/

export { default as Store, storeFor } from './-private/store-service';

export { recordIdentifierFor } from './-private/caches/instance-cache';

export { CacheHandler, type LifetimesService } from './-private/cache-handler';

export {
setIdentifierGenerationMethod,
setIdentifierUpdateMethod,
setIdentifierForgetMethod,
setIdentifierResetMethod,
isStableIdentifier,
} from './-private/caches/identifier-cache';

// TODO this should be a deprecated helper but we have so much usage of it
// to also eliminate
export { default as coerceId } from './-private/utils/coerce-id';
export type { NativeProxy } from './-private/record-arrays/native-proxy-type-fix';
export {
default as RecordArray,
default as IdentifierArray,
Collection as AdapterPopulatedRecordArray,
notifyArray,
SOURCE,
MUTATE,
ARRAY_SIGNAL,
} from './-private/record-arrays/identifier-array';
export { default as RecordArrayManager, fastPush } from './-private/managers/record-array-manager';

// leaked for private use / test use, should investigate removing
export { _clearCaches } from './-private/caches/instance-cache';
export { default as peekCache, removeRecordDataFor } from './-private/caches/cache-utils';

// @ember-data/model needs these temporarily
export { setRecordIdentifier, StoreMap } from './-private/caches/instance-cache';
export { setCacheFor } from './-private/caches/cache-utils';
export { default as _deprecatingNormalize } from './-private/utils/normalize-model-name';
41 changes: 0 additions & 41 deletions packages/store/src/-private/index.ts

This file was deleted.

18 changes: 7 additions & 11 deletions packages/store/src/-private/record-arrays/identifier-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { isStableIdentifier } from '../caches/identifier-cache';
import { recordIdentifierFor } from '../caches/instance-cache';
import type RecordArrayManager from '../managers/record-array-manager';
import type Store from '../store-service';
import { NativeProxy } from './native-proxy-type-fix';

type KeyType = string | symbol | number;
const ARRAY_GETTER_METHODS = new Set<KeyType>([
Expand Down Expand Up @@ -81,11 +82,6 @@ function convertToInt(prop: KeyType): number | null {
}

type ProxiedMethod = (...args: unknown[]) => unknown;
declare global {
interface ProxyConstructor {
new <TSource extends object, TTarget extends object>(target: TSource, handler: ProxyHandler<TSource>): TTarget;
}
}

export type IdentifierArrayCreateOptions<T = unknown> = {
identifiers: StableRecordIdentifier[];
Expand All @@ -101,9 +97,9 @@ interface PrivateState {
links: Links | PaginationLinks | null;
meta: Record<string, unknown> | null;
}
type ForEachCB<T> = (record: T, index: number, context: typeof Proxy<StableRecordIdentifier[], T[]>) => void;
type ForEachCB<T> = (record: T, index: number, context: typeof NativeProxy<StableRecordIdentifier[], T[]>) => void;
function safeForEach<T>(
instance: typeof Proxy<StableRecordIdentifier[], T[]>,
instance: typeof NativeProxy<StableRecordIdentifier[], T[]>,
arr: StableRecordIdentifier[],
store: Store,
callback: ForEachCB<T>,
Expand Down Expand Up @@ -144,7 +140,7 @@ function safeForEach<T>(
interface IdentifierArray<T = unknown> extends Omit<Array<T>, '[]'> {
[MUTATE]?(
target: StableRecordIdentifier[],
receiver: typeof Proxy<StableRecordIdentifier[], T[]>,
receiver: typeof NativeProxy<StableRecordIdentifier[], T[]>,
prop: string,
args: unknown[],
_SIGNAL: Signal
Expand Down Expand Up @@ -231,8 +227,8 @@ class IdentifierArray<T = unknown> {
// we track all mutations within the call
// and forward them as one

const proxy = new Proxy<StableRecordIdentifier[], T[]>(this[SOURCE], {
get<R extends typeof Proxy<StableRecordIdentifier[], T[]>>(
const proxy = new NativeProxy<StableRecordIdentifier[], T[]>(this[SOURCE], {
get<R extends typeof NativeProxy<StableRecordIdentifier[], T[]>>(
target: StableRecordIdentifier[],
prop: keyof R,
receiver: R
Expand Down Expand Up @@ -344,7 +340,7 @@ class IdentifierArray<T = unknown> {
target: StableRecordIdentifier[],
prop: KeyType,
value: unknown,
receiver: typeof Proxy<StableRecordIdentifier[], T[]>
receiver: typeof NativeProxy<StableRecordIdentifier[], T[]>
): boolean {
if (prop === 'length') {
if (!transaction && value === 0) {
Expand Down
134 changes: 134 additions & 0 deletions packages/store/src/-private/record-arrays/native-proxy-type-fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
We redefine Proxy because the native Proxy type treats the `target` and
`receiver` as the same type incorrectly.

We ported this from Typescript's own Proxy types on 3/10/2024.
*/
interface ProxyHandler<T extends object> {
/**
* A trap method for a function call.
* @param target The original callable object which is being proxied.
* @internal
*/
apply?(target: T, thisArg: any, argArray: any[]): any;

/**
* A trap for the `new` operator.
* @param target The original object which is being proxied.
* @param newTarget The constructor that was originally called.
* @internal
*/
// eslint-disable-next-line @typescript-eslint/ban-types
construct?(target: T, argArray: any[], newTarget: Function): object;

/**
* A trap for `Object.defineProperty()`.
* @param target The original object which is being proxied.
* @returns A `Boolean` indicating whether or not the property has been defined.
* @internal
*/
defineProperty?(target: T, property: string | symbol, attributes: PropertyDescriptor): boolean;

/**
* A trap for the `delete` operator.
* @param target The original object which is being proxied.
* @param p The name or `Symbol` of the property to delete.
* @returns A `Boolean` indicating whether or not the property was deleted.
* @internal
*/
deleteProperty?(target: T, p: string | symbol): boolean;

/**
* A trap for getting a property value.
* @param target The original object which is being proxied.
* @param p The name or `Symbol` of the property to get.
* @param receiver The proxy or an object that inherits from the proxy.
* @internal
*/
get?(target: T, p: string | symbol, receiver: any): any;

/**
* A trap for `Object.getOwnPropertyDescriptor()`.
* @param target The original object which is being proxied.
* @param p The name of the property whose description should be retrieved.
* @internal
*/
getOwnPropertyDescriptor?(target: T, p: string | symbol): PropertyDescriptor | undefined;

/**
* A trap for the `[[GetPrototypeOf]]` internal method.
* @param target The original object which is being proxied.
* @internal
*/
getPrototypeOf?(target: T): object | null;

/**
* A trap for the `in` operator.
* @param target The original object which is being proxied.
* @param p The name or `Symbol` of the property to check for existence.
* @internal
*/
has?(target: T, p: string | symbol): boolean;

/**
* A trap for `Object.isExtensible()`.
* @param target The original object which is being proxied.
* @internal
*/
isExtensible?(target: T): boolean;

/**
* A trap for `Reflect.ownKeys()`.
* @param target The original object which is being proxied.
* @internal
*/
ownKeys?(target: T): ArrayLike<string | symbol>;

/**
* A trap for `Object.preventExtensions()`.
* @param target The original object which is being proxied.
* @internal
*/
preventExtensions?(target: T): boolean;

/**
* A trap for setting a property value.
* @param target The original object which is being proxied.
* @param p The name or `Symbol` of the property to set.
* @param receiver The object to which the assignment was originally directed.
* @returns A `Boolean` indicating whether or not the property was set.
* @internal
*/
set?(target: T, p: string | symbol, newValue: any, receiver: any): boolean;

/**
* A trap for `Object.setPrototypeOf()`.
* @param target The original object which is being proxied.
* @param newPrototype The object's new prototype or `null`.
* @internal
*/
setPrototypeOf?(target: T, v: object | null): boolean;
}

interface ProxyConstructor {
/**
* Creates a revocable Proxy object.
* @param target A target object to wrap with Proxy.
* @param handler An object whose properties define the behavior of Proxy when an operation is attempted on it.
* @internal
*/
revocable<T extends object>(target: T, handler: ProxyHandler<T>): { proxy: T; revoke: () => void };

/**
* Creates a Proxy object. The Proxy object allows you to create an object that can be used in place of the
* original object, but which may redefine fundamental Object operations like getting, setting, and defining
* properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs.
* @param target A target object to wrap with Proxy.
* @param handler An object whose properties define the behavior of Proxy when an operation is attempted on it.
* @internal
*/
new <TSource extends object, TTarget extends object>(target: TSource, handler: ProxyHandler<TSource>): TTarget;
}

export const NativeProxy: ProxyConstructor = Proxy as unknown as ProxyConstructor;
Loading
Loading