Skip to content

Commit

Permalink
refactor(array): refactor array remove
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Jan 4, 2025
1 parent ba4e5bc commit b034ced
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 188 deletions.
32 changes: 13 additions & 19 deletions src/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
markFinalization,
finalizePatches,
isDraft,
skipFinalization,
} from './utils';
import { checkReadable } from './unsafe';
import { generatePatches } from './patch';
Expand Down Expand Up @@ -92,29 +91,24 @@ const proxyHandler: ProxyHandler<ProxyDraft> = {

if (!has(source, key)) {
const desc = getDescriptor(source, key);
const value = desc?.value;
if (target.type === DraftType.Array) {
if (
[
'splice',
'push',
'pop',
'shift',
'unshift',
'sort',
'reverse',
].includes(key as string)
) {
if (['splice', 'shift', 'unshift', 'reverse'].includes(key as string)) {
return function (this: any, ...args: any[]) {
let returnValue: any;
arrayHandling = true;
const result = desc!.value.apply(this, args);
arrayHandling = false;
return result;
try {
returnValue = value.apply(this, args);
} finally {
arrayHandling = false;
}
return returnValue;
};
}
}
return desc
? `value` in desc
? desc.value
? value
: // !case: support for getter
desc.get?.call(target.proxy)
: undefined;
Expand Down Expand Up @@ -147,9 +141,9 @@ const proxyHandler: ProxyHandler<ProxyDraft> = {
return target.copy![key];
}
if (arrayHandling && !isDraft(value)) {
skipFinalization.add(value);
} else if (skipFinalization.has(value)) {
skipFinalization.delete(value);
target.options.skipFinalization!.add(value);
} else if (target.options.skipFinalization!.has(value)) {
target.options.skipFinalization!.delete(value);
}
return value;
},
Expand Down
2 changes: 1 addition & 1 deletion src/draftify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { dataTypes } from './constant';
export function draftify<
T extends object,
O extends PatchesOptions = false,
F extends boolean = false
F extends boolean = false,
>(
baseState: T,
options: Options<O, F>
Expand Down
65 changes: 34 additions & 31 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export interface ProxyDraft<T = any> {
copy: T | null;
proxy: T | null;
finalities: Finalities;
options: Options<any, any> & { updatedValues?: WeakMap<any, any> };
options: Options<any, any> & {
updatedValues?: WeakMap<any, any>;
skipFinalization?: WeakSet<any>;
};
parent?: ProxyDraft | null;
key?: string | number | symbol;
setMap?: Map<any, ProxyDraft>;
Expand All @@ -62,30 +65,30 @@ export type Patch<P extends PatchesOptions = any> = P extends {
path: string;
}
: P extends true | object
? IPatch & {
path: (string | number)[];
}
: IPatch & {
path: string | (string | number)[];
};
? IPatch & {
path: (string | number)[];
}
: IPatch & {
path: string | (string | number)[];
};

export type Patches<P extends PatchesOptions = any> = Patch<P>[];

export type Result<
T extends any,
O extends PatchesOptions,
F extends boolean
F extends boolean,
> = O extends true | object
? [F extends true ? Immutable<T> : T, Patches<O>, Patches<O>]
: F extends true
? Immutable<T>
: T;
? Immutable<T>
: T;

export type CreateResult<
T extends any,
O extends PatchesOptions,
F extends boolean,
R extends void | Promise<void> | T | Promise<T>
R extends void | Promise<void> | T | Promise<T>,
> = R extends Promise<void> | Promise<T>
? Promise<Result<T, O, F>>
: Result<T, O, F>;
Expand All @@ -99,8 +102,8 @@ export type Mark<O extends PatchesOptions, F extends boolean> = (
) => O extends true | object
? BaseMark
: F extends true
? BaseMark
: MarkWithCopy;
? BaseMark
: MarkWithCopy;

export interface Options<O extends PatchesOptions, F extends boolean> {
/**
Expand Down Expand Up @@ -154,8 +157,8 @@ export type IfAvailable<T, Fallback = void> = true | false extends (
)
? Fallback
: keyof T extends never
? Fallback
: T;
? Fallback
: T;
type WeakReferences =
| IfAvailable<WeakMap<any, any>>
| IfAvailable<WeakSet<any>>;
Expand All @@ -164,14 +167,14 @@ type AtomicObject = Function | Promise<any> | Date | RegExp;
export type Immutable<T> = T extends Primitive | AtomicObject
? T
: T extends IfAvailable<ReadonlyMap<infer K, infer V>>
? ImmutableMap<K, V>
: T extends IfAvailable<ReadonlySet<infer V>>
? ImmutableSet<V>
: T extends WeakReferences
? T
: T extends object
? ImmutableObject<T>
: T;
? ImmutableMap<K, V>
: T extends IfAvailable<ReadonlySet<infer V>>
? ImmutableSet<V>
: T extends WeakReferences
? T
: T extends object
? ImmutableObject<T>
: T;

type DraftedMap<K, V> = Map<K, Draft<V>>;
type DraftedSet<T> = Set<Draft<T>>;
Expand All @@ -182,11 +185,11 @@ type DraftedObject<T> = {
export type Draft<T> = T extends Primitive | AtomicObject
? T
: T extends IfAvailable<ReadonlyMap<infer K, infer V>>
? DraftedMap<K, V>
: T extends IfAvailable<ReadonlySet<infer V>>
? DraftedSet<V>
: T extends WeakReferences
? T
: T extends object
? DraftedObject<T>
: T;
? DraftedMap<K, V>
: T extends IfAvailable<ReadonlySet<infer V>>
? DraftedSet<V>
: T extends WeakReferences
? T
: T extends object
? DraftedObject<T>
: T;
13 changes: 8 additions & 5 deletions src/makeCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import { RAW_RETURN_SYMBOL, dataTypes } from './constant';

type MakeCreator = <
_F extends boolean = false,
_O extends PatchesOptions = false
_O extends PatchesOptions = false,
>(
options?: ExternalOptions<_O, _F>
) => {
<
T extends any,
F extends boolean = _F,
O extends PatchesOptions = _O,
R extends void | Promise<void> | T | Promise<T> = void
R extends void | Promise<void> | T | Promise<T> = void,
>(
base: T,
mutate: (draft: Draft<T>) => R,
Expand All @@ -38,7 +38,7 @@ type MakeCreator = <
T extends any,
F extends boolean = _F,
O extends PatchesOptions = _O,
R extends void | Promise<void> = void
R extends void | Promise<void> = void,
>(
base: T,
mutate: (draft: T) => R,
Expand All @@ -49,7 +49,7 @@ type MakeCreator = <
P extends any[] = [],
F extends boolean = _F,
O extends PatchesOptions = _O,
R extends void | Promise<void> = void
R extends void | Promise<void> = void,
>(
mutate: (draft: Draft<T>, ...args: P) => R,
options?: ExternalOptions<O, F>
Expand Down Expand Up @@ -143,11 +143,14 @@ export const makeCreator: MakeCreator = (arg) => {
const enablePatches = options.enablePatches ?? false;
const strict = options.strict ?? false;
const enableAutoFreeze = options.enableAutoFreeze ?? false;
const _options: Options<any, any> = {
const _options: Options<any, any> & {
skipFinalization: WeakSet<any>;
} = {
enableAutoFreeze,
mark,
strict,
enablePatches,
skipFinalization: new WeakSet(),
};
if (
!isDraftable(state, _options) &&
Expand Down
4 changes: 1 addition & 3 deletions src/utils/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
} from './draft';
import { forEach } from './forEach';

export const skipFinalization = new WeakSet();

export function handleValue(
target: any,
handledSet: WeakSet<any>,
Expand All @@ -24,7 +22,7 @@ export function handleValue(
!isDraftable(target, options) ||
handledSet.has(target) ||
Object.isFrozen(target) ||
skipFinalization.has(target)
options?.skipFinalization!.has(target)
)
return;
const isSet = target instanceof Set;
Expand Down
Loading

0 comments on commit b034ced

Please sign in to comment.