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

Default shouldEmitPatchOnChange to false #108

Merged
merged 1 commit into from
Jul 11, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ const readOnlyExample = TransformExample.createReadOnly(snapshot);
readOnlyExample.withoutParams; // => URL { href: "https://example.com" }
```

Snapshotted views emit patches when their values change. If you don't want snapshotted views to emit a patch when they change, you can pass a `shouldEmitPatchOnChange` function that returns `false` to the `@snapshottedView`, or you can pass `false` to `setDefaultShouldEmitPatchOnChange` to disable patch emission for all snapshotted views.
If your snapshotted views need to emit patches when their values change, you can pass a `shouldEmitPatchOnChange` function that returns `true` to the `@snapshottedView` decorator, or you can pass `true` to `setDefaultShouldEmitPatchOnChange` to enable patch emission for all snapshotted views.

##### Snapshotted view semantics

Expand Down
25 changes: 0 additions & 25 deletions spec/__snapshots__/class-model-snapshotted-views.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`class model snapshotted views an observable instance emits a patch when the view value changes 1`] = `
[MockFunction] {
"calls": [
[
{
"op": "replace",
"path": "/__snapshottedViewsEpoch",
"value": 1,
},
{
"op": "replace",
"path": "/__snapshottedViewsEpoch",
"value": 0,
},
],
],
"results": [
{
"type": "return",
"value": undefined,
},
],
}
`;

exports[`class model snapshotted views references references to models with snapshotted views can be instantiated 1`] = `
{
"examples": {
Expand Down
42 changes: 17 additions & 25 deletions spec/class-model-snapshotted-views.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,6 @@ describe("class model snapshotted views", () => {
expect(instance.slug).toEqual("test");
});

test("an observable instance emits a patch when the view value changes", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is already covered inside the describe("shouldEmitPatchOnChange", ...) tests.

const observableArray = observable.array<string>([]);

@register
class MyViewExample extends ClassModel({ key: types.identifier, name: types.string }) {
@snapshottedView()
get arrayLength() {
return observableArray.length;
}
}

const fn = jest.fn();
const instance = MyViewExample.create({ key: "1", name: "Test" });
onPatch(instance, fn);

runInAction(() => {
observableArray.push("a");
});
expect(fn).toMatchSnapshot();
});

test("an observable instance's snapshot includes the snapshotted views epoch", () => {
const instance = ViewExample.create({ key: "1", name: "Test" });
expect(getSnapshot(instance)).toEqual({ __snapshottedViewsEpoch: 0, key: "1", name: "Test" });
Expand Down Expand Up @@ -236,7 +215,7 @@ describe("class model snapshotted views", () => {

@register
class Child extends ClassModel({}) {
@snapshottedView({ onError })
@snapshottedView({ onError, shouldEmitPatchOnChange: () => true })
get parentsChildLength() {
const parent: Parent = getParent(this, 2);
return parent.children.size;
Expand Down Expand Up @@ -290,7 +269,7 @@ describe("class model snapshotted views", () => {
describe("shouldEmitPatchOnChange", () => {
afterEach(() => {
// reset the default value
setDefaultShouldEmitPatchOnChange(true);
setDefaultShouldEmitPatchOnChange(false);
});

test("readonly instances don't use the shouldEmitPatchOnChange option", () => {
Expand Down Expand Up @@ -356,9 +335,15 @@ describe("class model snapshotted views", () => {
});

expect(onPatchFn).toHaveBeenCalled();
expect(onPatchFn.mock.calls).toEqual([
[
{ op: "replace", path: "/__snapshottedViewsEpoch", value: 1 },
{ op: "replace", path: "/__snapshottedViewsEpoch", value: 0 },
],
]);
});

test("observable instances do emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange hasn't been called", () => {
test("observable instances don't emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange hasn't been called", () => {
const observableArray = observable.array<string>([]);

@register
Expand All @@ -378,7 +363,7 @@ describe("class model snapshotted views", () => {
observableArray.push("a");
});

expect(onPatchFn).toHaveBeenCalled();
expect(onPatchFn).not.toHaveBeenCalled();
});

test("observable instances do emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange was passed true", () => {
Expand All @@ -404,6 +389,13 @@ describe("class model snapshotted views", () => {
});

expect(onPatchFn).toHaveBeenCalled();
expect(onPatchFn).toHaveBeenCalled();
expect(onPatchFn.mock.calls).toEqual([
[
{ op: "replace", path: "/__snapshottedViewsEpoch", value: 1 },
{ op: "replace", path: "/__snapshottedViewsEpoch", value: 0 },
],
]);
});

test("observable instances don't emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange was passed false", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/class-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ export const isClassModel = (type: IAnyType): type is IClassModelType<any, any,
return (type as any).isMQTClassModel;
};

let defaultShouldEmitPatchOnChange = true;
let defaultShouldEmitPatchOnChange = false;

/**
* Sets the default value for the `shouldEmitPatchOnChange` option for
Expand Down
Loading