Skip to content

Commit

Permalink
Default shouldEmitPatchOnChange to false
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-rc committed Jul 11, 2024
1 parent 91de9d1 commit 6b22061
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 52 deletions.
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
29 changes: 4 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", () => {
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 @@ -358,7 +337,7 @@ describe("class model snapshotted views", () => {
expect(onPatchFn).toHaveBeenCalled();
});

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 +357,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 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

0 comments on commit 6b22061

Please sign in to comment.