1.3.1 • Published 6 months ago

q1t v1.3.1

Weekly downloads
67
License
ISC
Repository
github
Last release
6 months ago

q1t (quant)

q1t - rxjs like state manager

Usage

import { State, mapObservable, toNextFrame } from 'q1t'

const counter = new State(0)

counter
	.pipe(mapObservable(n => n.toString()))
	.pipe(toNextFrame())
	.subscribe(valueStr => {
		console.log('valueStr', valueStr)
	})

counter.setValue(1)

Actions

const user = new State({ name: 'Max', age: 20 })

const { incAge } = user.createActions({ incAge: user => ({ ...user, age: user.age + 1 }) })

incAge()

user.getValue().age // 21

Worker

send fetch request to worker


types.ts

export type MyJsonRpc = JsonRpc<'sum', { values: number[] }, { sum: number }>

export type MyNotifs = Notification<'notify_name', { any_data: boolean[] }>

main.ts

import { connectWorker, JsonRpc, Notification } from 'q1t'
import type { MyJsonRpc, MyNotifs } from './types.ts' 

const worker = new Worker('my_worker.js')

const { fetch } = connectWorker<MyJsonRpc, MyNotifs>(worker)

const responseFromWorker = await fetch({ requestName: 'sum', data: { values: [1, 2]}})

console.log(resposeFromWorker) // 3

worker.ts

import { connectClient } from 'q1t'
import type { MyJsonRpc, MyNotifs } from './types.ts' 

connectClient<MyJsonRpc, MyNotifs>>({
	fetch(request) => {
		switch(request.requestName) {
			case 'sum': 
				return request.data.values.reduce((acc, v) => acc + v, 0)
			default: throw new Error('unexpected behavior')
		}
	}
})

Depends like RxJS

const items = createState([
	{ price: 1 },
	{ price: 2 },
	{ price: 3 },
])

const selectedIndex = createState(0)

const selectedItem = combineLatest([items, selectedIndex])
	.pipe(mapObservable(([items, index]) => items[index]))

const currency = createState({
	value: 1,
	symbol: '$'
})

const selectedItemPrice = combineLatest([
	selectedItem,
	currency,
])
	.pipe(mapObservable(([item, currency]) => {
		return currency.value * item.price
	}))

selectedItemPrice
	.subscribe(price => {
		console.log('selectedItemPrice has been updated', price)
	})

selectedIndex.next(1)
1.2.0

8 months ago

1.3.1

6 months ago

1.3.0

8 months ago

1.1.0

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.6

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