Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
reactive-blast
Advanced tools
Small collection of buzzling librsries like events publisher & subscribe pattern, and observable & reactive objects library.
Small events publisher & subscribe pattern, and observable & reactive objects library.
First, we need to install reactive-blast
:
npm install --save reactive-blast
You cas use the blobal API like this:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const obj = observe({
a: 1,
b: 2
});
let result = 0;
const sum = computed(() => {
result = obj.a + obj.b;
}, { autoRun: false });
sum();
expect(result).to.equal(3);
obj.a = 2;
expect(result).to.equal(4);
obj.b = 3;
expect(result).to.equal(5);
Another example:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const obj = observe({
a: 1,
b: 2,
sum: 0
}, { props: [ 'a', 'b' ]})
computed(() => {
obj.sum += obj.a
obj.sum += obj.b
obj.sum += obj.a + obj.b
}, { autoRun: true })
// 1 + 2 + 3
expect(obj.sum).to.equal(6)
obj.a = 2
// 6 + 2 + 2 + 4
expect(obj.sum).to.equal(14)
Multi-observed objects:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const obj1 = observe({ a: 1 })
const obj2 = observe({ a: 2 })
const obj3 = observe({ a: 3 })
let result = 0
computed(() => {
result = obj1.a + obj2.a + obj3.a
})
expect(result).to.equal(6)
obj1.a = 0
expect(result).to.equal(5)
obj2.a = 0
expect(result).to.equal(3)
obj3.a = 0
expect(result).to.equal(0)
Array methods:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const arr = observe([{ val: 1 }, { val: 2 }, { val: 3 }])
let sum = 0
computed(() => { sum = arr.reduce((acc, { val }) => acc + val, 0) })
expect(sum).to.equal(6)
arr.push({ val: 4 })
expect(sum).to.equal(10)
arr.pop()
expect(sum).to.equal(6)
arr.unshift({ val: 5 }, { val: 4 })
expect(sum).to.equal(15)
arr.shift()
expect(sum).to.equal(10)
arr.splice(1, 3)
expect(sum).to.equal(4)
Dispose computed functions:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const obj = observe({ a: 0 })
let result = 0
let result2 = 0
const minusOne = computed(() => {
result2 = obj.a - 1
})
computed(() => {
result = obj.a + 1
})
obj.a = 1
expect(result).to.equal(2)
expect(result2).to.equal(0)
dispose(minusOne)
obj.a = 10
expect(result).to.equal(11)
expect(result2).to.equal(0)
Asynchronous computation:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const obj = observe({ a: 0, b: 0 })
const addOne = () => {
obj.b = obj.a + 1
}
const delayedAddOne = computed(
({ computeAsync }) => delay(200).then(() => computeAsync(addOne)),
{ autoRun: false }
)
await delayedAddOne()
obj.a = 2
expect(obj.b).to.equal(1)
await delay(250).then(() => {
expect(obj.b).to.equal(3)
})
Currect asynchronous computation:
import { Global } from 'reactive-blast';
import { expect } from 'chai'
const { observe, computed, dispose } = Global
const obj = observe({ a: 0, b: 0, c: 0 })
let result = 0
const plus = prop => computed(async ({ computeAsync }) => {
await delay(200)
computeAsync(() => result += obj[prop])
}, { autoRun: false })
const plusA = plus('a')
const plusB = plus('b')
const plusC = plus('c')
await Promise.all([ plusA(), plusB(), plusC() ])
expect(result).to.equal(0)
obj.a = 1
obj.b = 2
obj.c = 3
await delay(250).then(() => {
expect(result).to.equal(6)
})
Instead of using Global function, you can use Observable class to create a reactive object. It's nearly identical.
import { Observable } from 'reactive-blast';
import { expect } from 'chai'
const obj = new Observable({
a: 1,
b: 2
});
let result = 0;
const sum = obj.computed(() => {
result = obj.a + obj.b;
}, { autoRun: false });
sum();
expect(result).to.equal(3);
obj.a = 2;
expect(result).to.equal(4);
obj.b = 3;
expect(result).to.equal(5);
Another example:
import { Observable } from 'reactive-blast';
import { expect } from 'chai'
const obj = new Observable({
a: 1,
b: 2,
c: 3,
d: 4
})
let result = 0
const aPlusB = () => obj.a + obj.b
const cPlusD = () => obj.c + obj.d
obj.computed(() => {
result = aPlusB() + cPlusD()
})
expect(result).to.equal(10)
obj.a = 2
expect(result).to.equal(11)
obj.d = 5
expect(result).to.equal(12)
Multiple getters:
import { Observable } from 'reactive-blast';
import { expect } from 'chai'
const obj = new Observable({
a: 1,
b: 2,
sum: 0
}, { props: [ 'a', 'b' ]})
obj.computed(() => {
obj.sum += obj.a
obj.sum += obj.b
obj.sum += obj.a + obj.b
}, { autoRun: true })
// 1 + 2 + 3
expect(obj.sum).to.equal(6)
obj.a = 2
// 6 + 2 + 2 + 4
expect(obj.sum).to.equal(14)
Multiple observed objects:
import { Observable } from 'reactive-blast';
import { expect } from 'chai'
const obj1 = new Observable({ a: 1 })
const obj2 = new Observable({ a: 2 })
const obj3 = new Observable({ a: 3 })
let result = 0
obj1.computed(() => {
result = obj1.a + obj2.a + obj3.a
})
expect(result).to.equal(6)
obj1.a = 0
expect(result).to.equal(5)
obj2.a = 0
expect(result).to.equal(3)
obj3.a = 0
expect(result).to.equal(0)
FAQs
Small collection of buzzling librsries like events publisher & subscribe pattern, and observable & reactive objects library.
The npm package reactive-blast receives a total of 0 weekly downloads. As such, reactive-blast popularity was classified as not popular.
We found that reactive-blast demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.