Skip to content
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
9 changes: 5 additions & 4 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import * as emitter from "./emitter.js";
const entries = new WeakMap();
const stack = new Set();

function dispatch(entry) {
function dispatch(entry, resolved = false) {
const contexts = [];
let index = 0;

while (entry) {
entry.resolved = false;
entry.resolved = resolved;

while (entry) {
if (entry.contexts) {
for (const context of entry.contexts) {
if (!stack.has(context) && !contexts.includes(context)) {
context.resolved = false;
contexts.push(context);
}
}
Expand Down Expand Up @@ -137,7 +138,7 @@ export function sync(target, key, fn, value) {
entry.value = nextValue;
entry.assertValue = undefined;

dispatch(entry);
dispatch(entry, true);

// mark as resolved to avoid double fn call in get
entry.resolved = true;
Expand Down
14 changes: 11 additions & 3 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1120,8 +1120,10 @@ function get(Model, id) {
const entry = cache.getEntry(config, stringId);
const cachedModel = entry.value;

if (entry.resolved && cachedModel && !validate(cachedModel)) {
entry.resolved = false;
if (cachedModel) {
entry.resolved =
(entry.resolved && validate(cachedModel)) ||
getModelState(cachedModel).state === "pending";
}

return cache.get(config, stringId, () => {
Expand Down Expand Up @@ -1287,7 +1289,13 @@ function set(model, values = {}) {
}

if (!isDraft && values && hasOwnProperty.call(values, "id")) {
throw TypeError(`Values must not contain 'id' property: ${values.id}`);
if (!config.enumerable) {
throw TypeError(`Values must not contain 'id' property: ${values.id}`);
} else if (!isInstance || values.id !== model.id) {
throw TypeError(
`You cannot change the 'id' property of the model instance: ${values.id}`,
);
}
}

const localModel = config.create(values, isInstance ? model : undefined);
Expand Down
18 changes: 16 additions & 2 deletions test/spec/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,23 @@ describe("store:", () => {
expect(() => store.set(model, false)).toThrow();
});

it("throws an error when values contain 'id' property", async () => {
it("throws an error when values contain 'id' property for singleton model", async () => {
Model = {};
expect(() => store.set(Model, { id: "test" })).toThrow();
});

it("throws an error when values contain 'id' property for enumerable model", async () => {
expect(() => store.set(Model, { id: "test" })).toThrow();
});

it("throws an error when updating an instance and values contain different 'id' property", async () => {
const model = await promise;
expect(() => store.set(model, { ...model, id: "different" })).toThrow();
});

it("does not throw an error when updating an instance and values contain the same 'id' property", async () => {
const model = await promise;
expect(() => store.set(model, model)).toThrow();
expect(() => store.set(model, model)).not.toThrow();
});

it("throws an error when array with primitives is set with wrong type", async () => {
Expand Down