Skip to content

Commit

Permalink
fixes loadState circular Exome instance loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcisbee committed Jun 17, 2021
1 parent 83cc52a commit 50aa3cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.12.4

### Bugfixes
* Fixes `loadState` inability to load circular Exome instances.

## 0.12.3

### Bugfixes
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
45 changes: 45 additions & 0 deletions src/utils/load-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions src/utils/load-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function loadState(
const cachedInstance = instances.get(localId)

if (cachedInstance) {
Object.assign(cachedInstance, state)
return cachedInstance
}

Expand Down

0 comments on commit 50aa3cb

Please sign in to comment.