signal-chain
Advanced tools
Comparing version 0.1.0 to 0.2.0
export * from './signal/types'; | ||
import { emit, select, stopIf, stop, passIf, count } from './signal/tools'; | ||
import { emit, select, stopIf, stop, passIf, passUnique, count } from './signal/tools'; | ||
import { key } from './signal/object'; | ||
import { effect } from './signal/effect'; | ||
import { collect } from './signal/collect'; | ||
import { combine } from './signal/combine'; | ||
@@ -145,3 +144,3 @@ import { merge } from './signal/merge'; | ||
effect: typeof effect; | ||
collect: typeof collect; | ||
collect: import("./signal/collect").CollectCall; | ||
count: typeof count; | ||
@@ -160,4 +159,5 @@ stop: typeof stop; | ||
sidechain: import("./signal/chain").SideChainCall; | ||
passUnique: typeof passUnique; | ||
}; | ||
export default _default; | ||
//# sourceMappingURL=signal-ts.d.ts.map |
export * from './signal/types'; | ||
import { emit, select, stopIf, stop, passIf, count } from './signal/tools'; | ||
import { emit, select, stopIf, stop, passIf, passUnique, count } from './signal/tools'; | ||
import { chain, sidechain } from './signal/chain'; | ||
@@ -103,2 +103,3 @@ import { connect as connectPrimitive, create as createPrimitive } from './signal/primitive'; | ||
sidechain, | ||
passUnique, | ||
// missing: | ||
@@ -108,3 +109,2 @@ // debounce | ||
// interval | ||
// passIfChanged | ||
}; |
import type { Chain, Function2 } from './types'; | ||
export declare function collect<V1, V2>(keep: Function2<V2, V1, V2>, initial: V2): Chain<V1, V2>; | ||
export interface CollectCall { | ||
<V1>(): Chain<V1, V1[]>; | ||
<V1, V2>(keep: Function2<V2, V1, V2>, initial: V2): Chain<V1, V2>; | ||
} | ||
export declare const collect: CollectCall; | ||
export declare const buffer: <V>(size: number) => Chain<V, V[]>; | ||
export declare const window: <V>(size: number) => Chain<V, V[]>; | ||
//# sourceMappingURL=collect.d.ts.map |
@@ -1,11 +0,16 @@ | ||
export function collect(keep, initial) { | ||
export const collect = (keep, initial) => { | ||
return (next, parameter, context) => { | ||
if (!context.isInitialized) { | ||
context.collection = initial; | ||
context.collection = keep ? initial : []; | ||
context.isInitialized = true; | ||
} | ||
context.collection = keep(context.collection, parameter); | ||
if (keep) { | ||
context.collection = keep(context.collection, parameter); | ||
} | ||
else { | ||
context.collection.push(parameter); | ||
} | ||
return next(context.collection); | ||
}; | ||
} | ||
}; | ||
export const buffer = (size) => collect((collection, value) => { | ||
@@ -12,0 +17,0 @@ collection.push(value); |
import { chain } from './chain'; | ||
import { execute } from './util'; | ||
const ASYNC_UPDATES = true; | ||
const UPDATE_METHOD = 'microtask'; | ||
const BATCHING = 'assignment'; | ||
if (UPDATE_METHOD === 'immediate' && BATCHING != 'none') { | ||
throw new Error('Cannot batch updates with immediate update method'); | ||
} | ||
export const connect = (listen1, ...additionalListeners) => { | ||
@@ -35,2 +39,3 @@ const chained = chain(listen1, ...additionalListeners); | ||
let disconnected = false; | ||
let queuedUpdate = 0; | ||
let currentValue = initialValue; | ||
@@ -53,21 +58,40 @@ let listeners = []; | ||
}; | ||
const update = (newValue) => { | ||
if (disconnected) { | ||
return; | ||
const setValue = (newValue) => { | ||
if (!disconnected) { | ||
currentValue = newValue; | ||
} | ||
currentValue = newValue; | ||
if (ASYNC_UPDATES) { | ||
queueMicrotask(() => { | ||
listeners.forEach(listener => { | ||
execute(listener.cleanup); | ||
listener.cleanup = listener.fn(currentValue); | ||
}; | ||
const applyListeners = (value) => { | ||
listeners.forEach(listener => { | ||
execute(listener.cleanup); | ||
// this could be not optimal, | ||
// maybe we should use currenetValue to update here instead | ||
listener.cleanup = listener.fn(value); | ||
}); | ||
}; | ||
const update = (value) => { | ||
switch (UPDATE_METHOD) { | ||
case 'immediate': | ||
applyListeners(value); | ||
break; | ||
case 'microtask': | ||
queuedUpdate++; | ||
queueMicrotask(() => { | ||
queuedUpdate--; | ||
applyListeners(value); | ||
}); | ||
}); | ||
break; | ||
case 'timeout': | ||
if (queuedUpdate === 0) { | ||
queuedUpdate++; | ||
setTimeout(() => { | ||
queuedUpdate--; | ||
applyListeners(value); | ||
}); | ||
} | ||
break; | ||
default: | ||
let x = UPDATE_METHOD; | ||
throw new Error(`Unknown update method: ${x}`); | ||
} | ||
else { | ||
listeners.forEach(listener => { | ||
execute(listener.cleanup); | ||
listener.cleanup = listener.fn(currentValue); | ||
}); | ||
} | ||
}; | ||
@@ -85,3 +109,8 @@ const disconnect = () => { | ||
listen, | ||
update, | ||
update: (value) => { | ||
setValue(value); | ||
if (queuedUpdate === 0 || BATCHING != 'all') { | ||
update(value); | ||
} | ||
}, | ||
disconnect, | ||
@@ -92,5 +121,8 @@ get value() { | ||
set value(value) { | ||
update(value); | ||
setValue(value); | ||
if (queuedUpdate === 0 || BATCHING === 'none') { | ||
update(value); | ||
} | ||
} | ||
}; | ||
}; |
@@ -6,2 +6,3 @@ import type { Chain, Function1 } from './types'; | ||
export declare function stopIf<V>(condition: Function1<V, boolean>): Chain<V>; | ||
export declare function passUnique<V>(): Chain<V>; | ||
export declare function stop<V>(): Chain<V, never>; | ||
@@ -8,0 +9,0 @@ export declare function through<V>(): Chain<V>; |
@@ -24,2 +24,10 @@ export function count() { | ||
} | ||
export function passUnique() { | ||
return (next, parameter, context) => { | ||
if (context.last !== parameter) { | ||
context.last = parameter; | ||
return next(parameter); | ||
} | ||
}; | ||
} | ||
export function stop() { | ||
@@ -26,0 +34,0 @@ return () => { }; |
{ | ||
"name": "signal-chain", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Christoph Franke", | ||
@@ -21,3 +21,4 @@ "description": "Declarative reactive programming library", | ||
"build": "tsc", | ||
"test": "vitest" | ||
"test:watch": "vitest", | ||
"test": "tsc -p tsconfig.test.json && vitest run" | ||
}, | ||
@@ -24,0 +25,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
270409
1620
59