Skip to content

Add delete method #23

@George-Payne

Description

@George-Payne

Its not unusual to use a store as a map of items:

export interface Item {
    id: string;
    name: string;
    created: Date;
}

export type ItemStore = Record<string, Item>;

const itemStore = createStore<ItemStore>({});

This seems to work really well with @stencil/store:

const myItem = { id: 'a', name: 'My Item', created: new Date() };

itemStore.set(myItem.id, myItem);
// itemStore.state[myItem.id] = myItem;

itemStore.get('a') // => myItem

until you try to delete an item:

// nothing happens
delete itemStore['a'];

// sets the value to literal undefined
itemStore.set('a', undefined as any)
itemStore.state // { a: undefined } 

I'd like to propose adding a delete method to the store to allow you to do the following:

// deletes item
delete itemStore['a'];

// deletes the item from the store
itemStore.delete('a') // => boolean
itemStore.state // { } 

What are your opinions on this? I can see it being an issue with the current recommended usage:

export interface MyStore {
    clicks: number,
    seconds: number,
    squaredClicks: number
}

const { state, onChange } = createStore<MyStore>({
  clicks: 0,
  seconds: 0,
  squaredClicks: 0
}));

onChange('clicks', value => {
  state.squaredClicks = value ** 2;
});

// this is probably undesirable 
delete state.clicks;
console.log(state.squaredClicks) //=> NaN

But I think its utility outweighs its possible pitfalls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementResolution: RefineThis PR is marked for Jira refinement. We're not working on it - we're talking it through.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions