cyclejs / cyclejs

A functional and reactive JavaScript framework for predictable code
http://cycle.js.org
MIT License
10.24k stars 420 forks source link

Create localStorage driver #141

Closed staltz closed 8 years ago

staltz commented 9 years ago

Draft API:

function storageDriver(request$) {
  // ...
  return responseCollection;
}

where request$ is an Observable of request objects, following the template:

let request = {
  target: 'local', // 'local' | 'session'
  action: 'setItem', // 'setItem' | 'removeItem' | 'clear'
  key: 'mykey',
  value: 'myvalue'
};

Defaults are set as 'local' for target, and 'setItem' for action.

responseCollection is a queryable collection of Observables, following the template:

let responseCollection = {
  local: { // for localStorage
    key(n) // function returning Observable of the nth key
    getItem(key) // function returning Observable of values
  },
  session: { // for sessionStorage
    key(n) // function returning Observable of the nth key
    getItem(key) // function returning Observable of values
  }
};
staltz commented 9 years ago

When ready, use in TodoMVC-Cycle

kahlil commented 9 years ago

Hi @staltz I am interested in this feature. Would be happy to help. Do you want to write this from scratch or maybe driverize this: https://github.com/nramadas/Storable? (to which @highondrive pointed me to in Gitter)

staltz commented 9 years ago

Might be a good first task to help. Check out how TodoMVC-Cycle has a small local-storage driver.

kahlil commented 9 years ago

Ok will have a look at that. Thanks.

kahlil commented 8 years ago

@staltz this is how I understood your Draft API: https://github.com/kahlil/cycle-storage-driver can you have a quick look and let me know if I am on the right track before I go ahead and add tests and stuff?

It's just a few lines of code.

staltz commented 8 years ago

Overall looks good, although I'd change this signature

doAction(target = 'local', action = 'setItem', key = undefined, value = undefined) 

to

doAction(key, value, target = 'local', action = 'setItem') 

Because if you call doAction('fooAge', 30), then we should assume the defaults target = 'local' and action = 'setItem', but the first signature wouldn't do that, it would take target= 'fooAge' and action = 30.

kahlil commented 8 years ago

OK will do, thanks.

Should I refactor the function to take an object instead of params since the cycle linter does not like 4 params?

staltz commented 8 years ago

Perhaps yes

kahlil commented 8 years ago

OK @staltz I think code-wise this is done: https://github.com/kahlil/cycle-storage-driver, please let me know if something needs to be changed. It is currently also published on npm as cycle-storage-driver.

I still need to write the Readme.

kahlil commented 8 years ago

@staltz to my understanding this is done. See: https://github.com/kahlil/cycle-storage-driver/

Let me know if anything needs to be changed.

kahlil commented 8 years ago

Can this issue be closed?

staltz commented 8 years ago

Yes

kahlil commented 8 years ago

:smile:

Frikki commented 8 years ago

I don’t think it’s working as it ought to. See this issue https://github.com/cyclejs/cycle-storage-driver/issues/7