mostjs-community / proxy Goto Github PK
View Code? Open in Web Editor NEWCircular dependencies for most.
License: MIT License
Circular dependencies for most.
License: MIT License
I have a small bit of code in which I'm using a proxy:
function proxy(initial) {
const {attach, stream} = makeProxy();
return [stream.startWith(initial), attach];
}
export default function flatScan(f, initial) {
return stream => {
const [proxyState$, fulfill] = proxy(initial);
const state$ = most.just(proxyState$).join(); // prevent proxy disposal bug
const initialize = (x, state) => f(state, x, state$);
const nextState$ = sample(initialize, stream, state$)
.join()
.thru(fn$ => sample((fn, state) => fn(state), fn$, state$))
.startWith(initial)
.multicast();
fulfill(nextState$);
return state$;
};
}
Notice the commented line where I'm wrapping the proxy stream in another stream and then flattening it again. If I don't do this, and a second observer attaches to the proxy before the previous observer has disposed itself, then the original observer will still try to call dispose as it should, but the proxy will continue emitting events to that observer's sink, even after disposal. Somehow wrapping the proxy in another dummy operator solves the problem.
If you need an active use case in order to figure this out, let me know and I'll help you set up a working copy of my project so you can see it happening.
I've tried several ways to close the cycle with Proxy and they all seem to result in a stack overflow. Perhaps this sets up an infinite loop in the stream control logic?
This example counts to 5 the hard way, but it's a good example of the process I'm trying to model (shutting down a harmonic oscillator when it comes to rest). y$ emits a single true when the accumulated sum reaches 15. Closed-loop this would shut x$ down.
When the attach line is commented out you can see this in the console log. When it not commented out Chrome just sits there for a few seconds until devtools disconnects. I presume this crash is from the stack filling up with an endless stream of handshaking.
import { proxy } from 'most-proxy'
import { periodic } from 'most'
const trigger$ = periodic(1000, 1)
.scan( (s, t) => s + t, 0)
.take(10)
const { stream: yProxy$, attach: attachY } = proxy()
const x$ = trigger$.until(yProxy$)
const y$ = x$
.scan( (s, x) => s + x, 0 )
.map( sum => sum >= 15)
.filter( b => b )
.skipRepeats()
attachY(y$)
x$.observe( x => console.log(x))
y$.observe( x => console.log(x))
[edit] - Perhaps some sort of air-gap or break in the handshaking protocols is needed in the proxy stream to make the stream graph appear acyclic to the scheduler?
Released code works fine, but when I downloaded the repo (to update the most version number in package.json) and ran npm install
I got a whole bunch of errors like this:
node_modules/most/type-definitions/most.d.ts(7,72): error TS2304: Cannot find name 'Promise'.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.