0.0.3 • Published 3 years ago

create-ctx v0.0.3

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

create-ctx

creates a simple context object

usage

import create from 'create-ctx'
import { Event } from 'ts-typed-events'

type Data = Partial<{
  [index: string]: any
  test: string
  data2: string
  circular: Event<string>
}>

const initial: Data = {}

const ctx = create(
  initial,
  { create: () => '100' },
  {
    test: new Event<string>()
  }
)

ctx.ev.test.on(test => ctx.set({ test }))
ctx.ev.test.emit('100')

ctx.set({ data2: '500', circular: new Event })
console.log(ctx.data)

console.log(ctx.clean)

example of returned object

export interface ContextWrapper <Data, Methods> {
  data: Partial<Data>
  fn: Partial<Methods>
  ev: Events
  setFn(fn: Partial<Methods>): void
  get(): Context<Data, Methods>
  set (i: Partial<Data>): Partial<Data>
  clear(): void
  readonly clean: Partial<Data>
}