Nodding Previously Managed

    kiper

    1.0.0 • Public • Published

    kiper

    Keep objects available everywhere in nodejs application

    npm version Travis

    How to use

    First you need to install kiper using npm:

    npm install kiper --save

    Then, use kiper in your project:

    const kiper = require('kiper');
     
    // somewhere keep an asset
    kiper.keep('foo', 'bar');
     
    // somewhere you got it back
    let value = kiper.get('foo');
    console.log(value);
     

    API Usage

    Method: .keep(key: string, value: object)

    Keep an object in kiper
    Added in: v0.0.1
    Alias: set

    kiper.keep('foo', 'bar');

    Method: .keep(key: string, value: object, ttl: int, timeout: function)

    Keep an object for a period of time
    Added in: v0.1.0
    Alias: set

    kiper.keep('foo', 'bar', 1000, console.log);
    // output: bar foor

    Method: .get(key: string)

    Get an object from kiper
    Added in: v0.0.1

    kiper.get('foo');
    // returns: bar

    Method: .get(validator: function)

    Get an object from kiper by pass a validator function
    Added in: v0.0.2

    kiper.get((value, key) => value === 'bar');
    // returns: bar

    Method: .remove(key: string)

    Remove an object out of kiper
    Added in: v0.1.0

    kiper.remove('foo');
    // returns: bar

    Method: .retire()

    Stop interval checking, free memory and release objects
    Added in: v0.1.0

    kiper.retire();

    Method: .touch(key)

    Repair the last time usage
    Added in: v1.0.0

    kiper.touch('foo');

    Method: .watch(key, callback)

    Watch a key, if its value changes then a callback will be called
    Added in: v1.0.0

    // keep an asset and return an obserable object 
    let baz = kiper.keep('baz', {
        gold: 1000
    });
     
    // watch the key
    kiper.watch('baz', (obj, oldVal, propkey, type) => {
        console.log('change info:', obj, oldVal, propkey, type);
    });
     
    // baz lost one gold
    baz.gold = 999
    // baz has new silvers
    baz.silver = 1000
    // baz lost all golds
    delete baz.gold

    Method: .on(event, listener)

    Listen an event which emit from kiper
    Added in: v1.0.0

    kiper.keep('foo', 'bar', 1000);
    kiper.on('expired', (value, key) => {
        console.log('A timeout on key has expired', value, key);
    })

    Method: .once(event, listener)

    Listen an event once which emit from kiper
    Added in: v1.0.0

    kiper.keep('foo', 'bar', 1000);
    kiper.once('expired', (value, key) => {
        console.log('A timeout on key has expired', value, key);
    })

    License

    MIT

    Install

    npm i kiper

    DownloadsWeekly Downloads

    1

    Version

    1.0.0

    License

    MIT

    Last publish

    Collaborators

    • vunb