marcuswestin / store.js

Cross-browser storage for all use cases, used across the web.
MIT License
14.01k stars 1.33k forks source link

super_fn is always passed even if the function never existed #262

Open mdvorscak opened 6 years ago

mdvorscak commented 6 years ago

Not sure if this is expected behavior or not, but when creating a custom plugin I noticed that the first argument was always super_fn, regardless if the function existed or not.

For example:

function mapPlugin() {
  return {
    //I must add f here, even though 'has' wasn't a function to begin with
    has: function(f, key){
      return this.get(key) !== undefined;
    },
    delete: function(f, key){
      return this.remove(key);
    },
    clear: function(){
      this.clearAll();
    }
  };
};

If this is not the correct behavior it is a bug, otherwise the documentation should be updated since:

// Example plugin that stores a version history of every value
var versionHistoryPlugin = function() {
    var historyStore = this.namespace('history')
    return {
        set: function(super_fn, key, value) {
            var history = historyStore.get(key) || []
            history.push(value)
            historyStore.set(key, history)
            return super_fn()
        },
        getHistory: function(key) { //This line is not indicative of actual behavior
            return historyStore.get(key)
        }
    }
}
store.addPlugin(versionHistoryPlugin)
store.set('foo', 'bar 1')
store.set('foo', 'bar 2')
store.getHistory('foo') == ['bar 1', 'bar 2']