mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

Add types.set

Open the-noob opened this issue 6 years ago • 11 comments

I have a few stores where I need a reference to be unique (i.e. tags for a product)

Since every mutation has to go through an action it can be catered for there.

  function addTag(entity) {
    if (!self.tags.has(entity.id)) {
      self.tags.put(entity);
    }

    return self.tag.get(entity.id);
  }

It would be nice to have types.set with only tags.add :)

the-noob avatar Sep 13 '17 16:09 the-noob

This is another reason to have ability to create custom types for MST. Here all we have is private Map and public api to interact with it. It would be nice to make it possible to create such Type as it is done inside MST with Maps, Arrays or in some ligther way.

Imagine this as a prototype code:

const setType = types.userType("Set")
    .basedOn(types.model)
    .privateProps(baseType => {
        map: types.map(baseType)
    })
    .actions(self => {
        return {
            function add(entity) {
                 if (!self.map.has(entity.id)) {
                      self.map.put(entity);
                  }
                  return self.map.get(entity.id);
            }
        };
    });

This is totally possible to do with simple types.model, but the concept is more generic and allows us to create extension types for MST and easy reuse them across community.

stgolem avatar Sep 14 '17 06:09 stgolem

Is this issue open for PR? I can try to implement types.set.

Idered avatar Mar 14 '18 12:03 Idered

@Idered - given that this was reopenened and the announcement requesting more maintainers (#700) , I'd think PRs are very appreciated.

philspitler avatar Mar 14 '18 14:03 philspitler

@Idered i guess it would be nice to have it in mobx first so we don't have to implement an enhanced map as the workaround. have a look at: https://github.com/mobxjs/mobx/issues/69

robinfehr avatar Mar 14 '18 14:03 robinfehr

Now that this has been added to mobx, can it be added to MST?

flybayer avatar Feb 08 '19 21:02 flybayer

👀

kresli avatar Jan 15 '20 11:01 kresli

Please

sbussard avatar Feb 21 '20 19:02 sbussard

"help/PR welcome"

mweststrate avatar Feb 21 '20 19:02 mweststrate

What should the patches' path reference to?

{
  op: "add",
  path: '', // what should be in path?
  value: { title: 'my-title' },
  oldValue: undefined
}

{
  op: "remove",
  path: '' // what should be in path?,
  oldValue: {title: 'my-title'}
}

EDIT: I think its obvious to use root path path: "/"

But then how we would process the remove patch? There is no key or index to track. Any suggestions @mweststrate ?

kresli avatar Apr 24 '20 21:04 kresli

I think the path should only be the path to the set itself, not a specific child, so for a top level set, i'd expect it to be empty indeed. I think immer might be a good reference point, iirc it always replaces the full contents on the parent position, rather than having a removal / add operators.

mweststrate avatar May 07 '20 10:05 mweststrate

Why is this missing in MST? Sets are a quite common data structure.

Yamo93 avatar Dec 15 '21 11:12 Yamo93