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

feat: support for mutable data in apply() function #55

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 20 additions & 12 deletions src/apply.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Draft, Options, Patches, DraftType, Operation } from './interface';
import { Operation, DraftType } from './interface';
import type {
Draft,
Patches,
ApplyMutableOptions,
ApplyOptions,
ApplyResult,
} from './interface';
import { deepClone, get, getType, isDraft, unescapePath } from './utils';
import { create } from './create';

Expand All @@ -23,14 +30,11 @@ import { create } from './create';
* expect(state).toEqual(apply(baseState, patches));
* ```
*/
export function apply<T extends object, F extends boolean = false>(
state: T,
patches: Patches,
applyOptions?: Pick<
Options<boolean, F>,
Exclude<keyof Options<boolean, F>, 'enablePatches'>
>
) {
export function apply<
T extends object,
F extends boolean = false,
A extends ApplyOptions<F> = ApplyOptions<F>
>(state: T, patches: Patches, applyOptions?: A): ApplyResult<T, F, A> {
let i: number;
for (i = patches.length - 1; i >= 0; i -= 1) {
const { value, op, path } = patches[i];
Expand All @@ -45,7 +49,7 @@ export function apply<T extends object, F extends boolean = false>(
if (i > -1) {
patches = patches.slice(i + 1);
}
const mutate = (draft: Draft<T>) => {
const mutate = (draft: Draft<T> | T) => {
patches.forEach((patch) => {
const { path: _path, op } = patch;
const path = unescapePath(_path);
Expand Down Expand Up @@ -122,15 +126,19 @@ export function apply<T extends object, F extends boolean = false>(
}
});
};
if ((applyOptions as ApplyMutableOptions)?.mutable) {
mutate(state);
return undefined as ApplyResult<T, F, A>;
}
if (isDraft(state)) {
if (applyOptions !== undefined) {
throw new Error(`Cannot apply patches with options to a draft.`);
}
mutate(state as Draft<T>);
return state;
return state as ApplyResult<T, F, A>;
}
return create<T, F>(state, mutate, {
...applyOptions,
enablePatches: false,
});
}) as any;
}
20 changes: 20 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export type Mark<O extends PatchesOptions, F extends boolean> = (
? BaseMark
: MarkWithCopy;

export interface ApplyMutableOptions {
/**
* If it's `true`, the state will be mutated directly.
*/
mutable?: boolean;
}

export interface Options<O extends PatchesOptions, F extends boolean> {
/**
* In strict mode, Forbid accessing non-draftable values and forbid returning a non-draft value.
Expand Down Expand Up @@ -190,3 +197,16 @@ export type Draft<T> = T extends Primitive | AtomicObject
: T extends object
? DraftedObject<T>
: T;

export type ApplyOptions<F extends boolean> =
| Pick<
Options<boolean, F>,
Exclude<keyof Options<boolean, F>, 'enablePatches'>
>
| ApplyMutableOptions;

export type ApplyResult<
T extends object,
F extends boolean = false,
A extends ApplyOptions<F> = ApplyOptions<F>
> = A extends { mutable: true } ? void : T;
53 changes: 43 additions & 10 deletions test/apply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1305,16 +1305,6 @@ test('merge multiple patches', () => {
enablePatches: true,
}
);
console.log(
JSON.stringify(
{
patches,
inversePatches,
},
null,
2
)
);
const [state1, patches1, inversePatches1] = create(
state,
(draft) => {
Expand Down Expand Up @@ -1357,3 +1347,46 @@ test('merge multiple patches', () => {
]);
expect(errorPrevState).not.toEqual(arr);
});

test('base - mutate', () => {
const baseState = {
a: {
c: 1,
},
};
const [state, patches, inversePatches] = create(
baseState,
(draft) => {
draft.a.c = 2;
},
{
enablePatches: true,
}
);
expect(state).toEqual({ a: { c: 2 } });
expect({ patches, inversePatches }).toEqual({
patches: [
{
op: 'replace',
path: ['a', 'c'],
value: 2,
},
],
inversePatches: [
{
op: 'replace',
path: ['a', 'c'],
value: 1,
},
],
});
const nextState = apply(baseState, patches);
expect(nextState).toEqual({ a: { c: 2 } });
expect(baseState).toEqual({ a: { c: 1 } });

const result = apply(baseState, patches, {
mutable: true,
});
expect(baseState).toEqual({ a: { c: 2 } });
expect(result).toBeUndefined();
});
Loading