Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@karacas/kaysh

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@karacas/kaysh

In-memory caching with support for RxJs observables, native promises, functions & methods. Optional localStorage.

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Kaysh

In-memory caching with support for RxJs observables, native promises, functions & methods. Optional localStorage.



Config

const optional_config = {
  localStorage: true  /* Uses localStorage / Promises and RxJs automatically save a resolved value | dafault:false */,
  maxItems: 10        /* Maximum values cached per function/args | dafault:10 */,
  maxTime: 5 * 1000   /* Maximum time of the cached value in milliseconds | dafault:0(infinite) */,
};


RxJs cache operator

import { kayshOperator, kayshFlushOperator } from '@karacas/kaysh';

const getObservable = (payLoad?) => {
  return of(Math.random() + JSON.stringify(payLoad))
          .pipe(kayshOperator('getObservable', payLoad, optional_config));
};

const flushObservableValue = (payLoad?) => kayshFlushOperator('getObservable', payLoad);
const flushObservable = () => kayshFlushOperator('getObservable');

Expects

test('TestObserbable Operator', async function() {
  let value1 = await getObservable([1, 2]).toPromise();
  let value2 = await getObservable([1, 2]).toPromise();

  expect(value1).toBe(value2);

  flushObservableValue([1, 2]); // or flushObservable() for all values

  let value3 = await getObservable([1, 2]).toPromise();
  expect(value3).not.toBe(value1);
});


Memoize a function

function testFunction(val) {
  return String(Math.random()) + JSON.stringify(val);
}

const testFunctionMem = kayshMemFunction(testFunction, optional_config);
const flush = kayshFlushMemFunction(testFunction);

Expects

test('Test Simple function', function() {
  let value1 = testFunctionMem({ test: [1] });
  let value2 = testFunctionMem({ test: [1] });

  expect(value1).toBe(value2);

  flush({ test: [1] }); // or flush() for all values

  let value3 = testFunctionMem({ test: [1] });
  expect(value3).not.toBe(value1);
});


Memoize a Class method

import { kayshMemFunction, kayshFlushMemFunction, kayshDecorator } from '@karacas/kaysh';

class CacheTestClass {
  //Normal

  funcTest(v = 100) {
    return 1 + v;
  }

  funcTestMemo = kayshMemFunction(this.funcTest);
  flushFuncTestMemo = kayshFlushMemFunction(this.funcTest);
}

Or

class CacheClassExampleWithDecorator {
  //Decorator

  @kayshDecorator(kayshOptions)
  testDecorated(v = 100) {
    return 1 + v;
  }

  flushTestDecorated = kayshFlushMemFunction(this.testDecorated);
}

Keywords

FAQs

Package last updated on 04 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc