From 50aa3cbe989825ae24bfde53dda8e45bb562570f Mon Sep 17 00:00:00 2001 From: Marcis Bergmanis Date: Thu, 17 Jun 2021 16:30:27 +0300 Subject: [PATCH] fixes loadState circular Exome instance loading --- CHANGELOG.md | 5 ++++ package-lock.json | 4 ++-- package.json | 2 +- src/utils/load-state.test.ts | 45 ++++++++++++++++++++++++++++++++++++ src/utils/load-state.ts | 1 + 5 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78c5948..fef797c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 0.12.4 + +### Bugfixes +* Fixes `loadState` inability to load circular Exome instances. + ## 0.12.3 ### Bugfixes diff --git a/package-lock.json b/package-lock.json index 81f0268..3f9d51c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "exome", - "version": "0.12.3", + "version": "0.12.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "exome", - "version": "0.12.3", + "version": "0.12.4", "license": "MIT", "devDependencies": { "@types/node": "^14.14.41", diff --git a/package.json b/package.json index 61713ac..77c132b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "exome", - "version": "0.12.3", + "version": "0.12.4", "description": "Proxy based store manager for deeply nested states", "main": "exome.js", "module": "exome.esm.js", diff --git a/src/utils/load-state.test.ts b/src/utils/load-state.test.ts index eab109a..6cc225c 100644 --- a/src/utils/load-state.test.ts +++ b/src/utils/load-state.test.ts @@ -264,4 +264,49 @@ test('creates proper instances with minified class names', () => { assert.equal(target.friends[0].name, 'Phil') }) +test('creates proper instances with circular state', () => { + class Person extends Exome { + constructor( + public name: string, + public friends: Person[] + ) { + super() + } + } + + class Store extends Exome { + constructor( + public persons: Person[] + ) { + super() + } + } + + const target = new Store([]) + + const state = JSON.stringify({ + $$exome_id: 'Store-123', + persons: [ + { + $$exome_id: 'Person-123', + name: 'John', + friends: [ + { + $$exome_id: 'Person-123' + } + ] + } + ] + }) + + registerLoadable({ Person, Store }) + + loadState(target, state) + + assert.equal(target.persons.length, 1) + assert.equal(target.persons[0].name, 'John') + assert.equal(target.persons[0].friends.length, 1) + assert.is(target.persons[0].friends[0], target.persons[0]) +}) + test.run() diff --git a/src/utils/load-state.ts b/src/utils/load-state.ts index bf6f07b..d3918c0 100644 --- a/src/utils/load-state.ts +++ b/src/utils/load-state.ts @@ -30,6 +30,7 @@ export function loadState( const cachedInstance = instances.get(localId) if (cachedInstance) { + Object.assign(cachedInstance, state) return cachedInstance }