Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transitions of Microstate Arrays #127

Merged
merged 5 commits into from Jun 19, 2018
Merged

Fix transitions of Microstate Arrays #127

merged 5 commits into from Jun 19, 2018

Conversation

taras
Copy link
Member

@taras taras commented May 30, 2018

One of the features of Microstates is the ability to organize data into arrays. When you create an array microstate, you can use special transitions to manipulate this array in immutable way. For example, create([Number], [1, 2, 3]).push(4) will return a new array with item 4 added to the end.

Previously, we implemented map, filter, push, pop and others, but they were not implemented correctly. When you used one of these operations, the created microstate did not have a properly formed data structure. This PR fixes this problem allowing all of the supported transitions to be used.

Notable Changes

Removed splice

Previously, splice was used to implement most of the array transitions. This was removed and so was splice. We can re-add it if the need arises.

map microstates

Previously, map would call the mapping function with state for each element of the array. This was necessary because there was no easy way to get the state of each individual array item. Unfortunately, this also meant that there was no obvious way to perform microstate transitions inside of map operations. There are several issues effected by this #76 and #75.

Fortunately, #113 branch introduced the ability to get state for each composed microstate. This means that we're able to get state inside of map and allow transitions to happen. In this PR, I completed the change to allow performing transitions inside of map.

You can call transitions,

let dataset = create(Dataset, { records: [
  {content: 'Herro'},
  {content: 'Sweet'},
  {content: "Woooo"}
]});

let d2 = dataset.records.map(record => record.content.concat('!!!'));

d2.valueOf();
// { records: [
//  {content: 'Herro!!!'},
//  {content: 'Sweet!!!'},
//  {content: "Woooo!!!"}
// ]}

Or you can change the type by returning a new microstate.

class SweetSweetRecord extends Record {}

mapped = dataset.records.map(record => {
  if (record.content.state === 'Sweet') {
    return create(SweetSweetRecord, record);
  } else {
    return record;
 }
});

added clear transition

clear transition is equivalent to calling .set([])

Closes #125, #75 & #76

@taras taras requested a review from cowboyd May 30, 2018 21:27
@coveralls
Copy link

coveralls commented May 30, 2018

Coverage Status

Coverage decreased (-0.1%) to 90.37% when pulling f398cc2 on tm/array-graft into 56e27ad on master.

@taras taras merged commit d239b56 into master Jun 19, 2018
@taras taras deleted the tm/array-graft branch June 19, 2018 20:38
@taras taras changed the title Fix transitions of parameterized arrays Fix transitions of Microstate Arrays Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Transitioning dynamic Array member fails.
3 participants