-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Labels
EnhancementResolution: RefineThis PR is marked for Jira refinement. We're not working on it - we're talking it through.This PR is marked for Jira refinement. We're not working on it - we're talking it through.
Description
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.
nwhittaker and crrobinson14
Metadata
Metadata
Assignees
Labels
EnhancementResolution: RefineThis PR is marked for Jira refinement. We're not working on it - we're talking it through.This PR is marked for Jira refinement. We're not working on it - we're talking it through.