store.js icon indicating copy to clipboard operation
store.js copied to clipboard

super_fn is always passed even if the function never existed

Open mdvorscak opened this issue 8 years ago • 0 comments

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']

mdvorscak avatar Jan 07 '18 03:01 mdvorscak