0.0.8 • Published 4 years ago

@botol/dipo v0.0.8

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

DIPO

Simple Composite controller

Example:

class ContextTG extends Context {
    id: number = 0;
    text?: string;
    photo?: string;
}

type ContextText = MakeRequired<ContextTG, 'text'>;
type ContextPhoto = MakeRequired<ContextTG, 'text'>;
type ContextAll = MakeRequired<ContextTG, 'text' | 'photo'>;
let y: ContextText;

let DefaultEvents = {
    all: CreateEvent<ContextAll, ContextTG>((handler) => (ctx, next) => {
        if ('text' in ctx && 'photo' in ctx) {
            return handler(ctx as ContextAll, next);
        }
        return next();
    }),
    text: CreateEvent<ContextText, ContextTG>((handler) => (ctx, next) => {
        if ('text' in ctx) {
            return handler(ctx as ContextText, next);
        }
        return next();
    }),
    photo: CreateEvent<ContextPhoto, ContextTG>((handler) => (ctx, next) => {
        if ('photo' in ctx) {
            return handler(ctx as ContextPhoto, next);
        }
        return next();
    })
};

let dipo = new Dipo<ExtractContexts<typeof DefaultEvents>, ContextTG>(DefaultEvents);
dipo.on('all', (ctx, next) => {
    console.log(3, ctx);
    return next();
});
dipo.on('text', (ctx, next) => {
    console.log(1, ctx);
    return next();
});
dipo.on('photo', (ctx, next) => {
    console.log(2, ctx);
});
dipo.use((ctx) => {
    console.log(4, ctx);
})

let ctx = new ContextTG();
ctx.photo = '1';
ctx.text = '1';
dipo.handle(ctx)

Middleware

type session = {
    id: number;
};
type ContextSession<session> =  {
    session: session;
} & Context;

dipo.middleware<ContextSession>(() => {})
    .on('text', (ctx) => {
        let id = ctx.session.id;
    })
    .on('text', (ctx) => {});

or

type session = {
    id: number;
};
type ContextSession<session> =  {
    session: session;
} & Context;

let dipoSession = dipo.middleware<ContextSession>(() => {})
dipoSession.on('text', (ctx) => {
        let id = ctx.session.id;
    })
dipoSession.on('text', (ctx) => {});
0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago