Giter Site home page Giter Site logo

cyclejs-cookie's Introduction

Cycle JS Cookie Driver

Cycle.js Cookie Driver, based on cookie_js library.

Install

$ npm install --save cyclejs-cookie xstream

Usage

import xs from 'xstream';
import {run} from '@cycle/xstream-run';
import {makeCookieDriver} from 'cyclejs-cookie';

function main({cookie}) {
    const cookieChangeSource$ = xs.periodic(1000);

    const cookieValue$ = cookie.get('MyCookie');

    // just for print debug
    const noop = () => undefined;
    cookieValue$.debug('cookie current value').addListener({
        next: noop,
        error: noop,
        complete: noop,
    });

    return {
        cookie: cookieChangeSource$.map((counter) => ({
            key: 'MyCookie',
            value: 'cookieValue-' + counter,
            settings: {
                expires: 30, // expiring in 30 days
            }
        }))
    };
};

run(main, {
    cookie: makeCookieDriver()
});

Api

Library export only one function: makeCookieDriver

makeCookieDriver(options = {}}

Instantiates an new cookie driver function (cookieDriver(sink$)).

options

  • decode - set cookie.decode attribute, check cookie_js documentation for details.

cookieDriver(sink$)

$sink - driver assumes that sink$ is stream of cookie setter objects. it interprete cookie setter object and set new cookie (or delete if cookie value is undefined).

Returning cookies observables functions object {get(), all()}.

  • get(cookieName) - returning stream of cookie cookieName changes, initiated by current cookie value
  • all() - returning stream of all cookies object changes, initiated by starting cookies object

cookie setter object

Driver assumes following objects on it $sink:

{
  key: 'cookieName',
  value: 'cookieValue', // if undefined, cookie will be deleted
  options: {..}
}

Options is cookie settings, like expires time. By cookie_js documentation:

The following fields can be added to the mentioned object:

key value default value
expires Either a number containing the days until the expiry, a date in the GMTString format or a date object. Expires when the browser is closed.
domain A string that specifies the domain that can access the cookie. The current domain.
path A string that limits the access of the cookie to that path. The current path.
secure A boolean indicating whether the cookie shall only be accessable over a secure connection or not. false

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.