Code Monkey home page Code Monkey logo

tonga-js's Introduction

Tonga-js

A javascript client for the Tonga flag management framework

Install

npm i git+https://github.com/Optibus/tonga-js.git#main-dist

Import in ES Modules

// import Prefetch Class
import { Prefetch } as CdkConfig from 'tonga-js'
// import Ondemand Class
import { Ondemand } as CdkConfig from 'tonga-js'

Import in CommonJS (Node js)

// import Prefetch Class
const { Prefetch } = require('tonga-js');
// assign to a common name
const CdkConfig = Prefetch;

Prefetch

should mostly be used in client side where you can expect many get's happening and want them to be quick.

  • in this mode the get function is synchronos
  • the constructor invokes an async function that will emit a ready event when done.

Example

import { Prefetch } as CdkConfig from 'tonga-js'

const getConfData = (context) => {
    return new Promise(resolve) => {
        // code that will get the flags from the server or database
    }
}
// any context that you want to be used in the getConfData above
const context = {
    user: 'me'
};

const cdkConfig = new CdkConfig({ getConfgData }, context);
cdkConfig.on('ready' () => {
    const flagValue = cdkConfig.get('flagKey');
})

On Demand

should mostly be used in servers where you are most likely not to call the get so often, so the cache size is smaller

  • in this mode the get function is asynchronos
  • the constructor

Example

import { Ondemand } as CdkConfig from 'tonga-js'

const getFlag = (context) => {
    return new Promise(resolve) => {
        // code that will get the flag value from the server or database
    }
}
// any context that you want to be used in the getConfData above
const context = {
    user: 'me'
};

const cdkConfig = new CdkConfig({ getFlag }, context);
const flagValue = await cdkConfig.get('flagKey');

Analyitics

The first argument passed in both modes is an object of functions. An optional function can be added to call analyitics - analytics. As an optimization it will be throttled. So in order to prevent calling it too many times during one second, it will aggregate all calls and will make one call after a full second that it hasnt been called.

Example

export interface ThrottleArgs {
  flagKey: string;
  flagValue: unknown;
}

const analyitics = (throttledArgs: ThrottledArgs[], context) => {
    return new Promise() {
        // ajax called that can have in its body the the throtteled args
    }
}

const cdkConfig = new CdkConfig({ getFlag, analytics }, context);
const flagValue = await cdkConfig.get('flagKey');

tonga-js's People

Contributors

eitany avatar orengriffin avatar

Watchers

 avatar

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.