Skip to content

Commit

Permalink
fix(store): sync method should not trigger storage get of the model
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed May 24, 2024
1 parent 7c888ec commit 36d6e39
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
11 changes: 2 additions & 9 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,12 @@ function resolve(config, model, lastModel) {
return model;
}

function shallowEqual(target, compare) {
return Object.keys(target).every((key) => target[key] === compare[key]);
}

function resolveWithInvalidate(config, model, lastModel) {
resolve(config, model, lastModel);

if (
config.invalidate &&
(!lastModel ||
error(model) ||
!config.isInstance(lastModel) ||
!shallowEqual(model, lastModel))
(config.storage.loose || !lastModel || !config.isInstance(lastModel))
) {
config.invalidate();
}
Expand Down Expand Up @@ -1191,7 +1184,7 @@ function set(model, values = {}) {

let hasErrors = false;

if (localModel) {
if (localModel && config.checks.size) {
for (const [key, fn] of config.checks.entries()) {
if (keys.indexOf(key) === -1) {
if (lastError && lastError.errors && lastError.errors[key]) {
Expand Down
43 changes: 42 additions & 1 deletion test/spec/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,47 @@ describe("store:", () => {
expect(model.id).toBe("1");
});
});

it("does not trigger get method of the nested model", () => {
const spyProject = jasmine.createSpy();
const spyFeature = jasmine.createSpy();

const Project = {
id: true,
feature: store.ref(() => Feature),
[store.connect]: () => {
spyProject();
return null;
},
};

const Feature = {
id: true,
project: store.ref(() => Project),
[store.connect]: () => {
spyFeature();
return null;
},
};

store.sync(Feature, {
id: "0",
project: {
id: "321",
feature: "2",
},
});
store.sync(Feature, {
id: "1",
project: {
id: "321",
feature: "3",
},
});

expect(spyProject).toHaveBeenCalledTimes(0);
expect(spyFeature).toHaveBeenCalledTimes(0);
});
});

describe("clear()", () => {
Expand Down Expand Up @@ -1816,7 +1857,7 @@ describe("store:", () => {
expect(list.length).toBe(1);
}));

it("returns new list when modifies already existing item", () => {
it("returns a new list when modifies already existing item", () => {
const list = store.get([Model]);
return store.set(list[0], { value: "new value" }).then(() => {
const newList = store.get([Model]);
Expand Down

0 comments on commit 36d6e39

Please sign in to comment.