Skip to content

Commit

Permalink
effects run after full propagation.
Browse files Browse the repository at this point in the history
  • Loading branch information
StreetStrider committed Nov 27, 2019
1 parent 51f903f commit 81d7b7c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
13 changes: 12 additions & 1 deletion lib/Queue/Queue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import Counter from '../Counter'

import propagate from './propagate'
import { propagate } from './propagate'
import { effects } from './propagate'


export default function Queue (dep_fns)
Expand Down Expand Up @@ -63,6 +64,16 @@ export default function Queue (dep_fns)

propagate(queue, dep, compute(dep))
}


effects(queue, bud)

for (let n = 0; (n < L); n++)
{
const dep = order[n]

effects(queue, dep)
}
}

function compute (bud)
Expand Down
13 changes: 11 additions & 2 deletions lib/Queue/propagate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Many from '../Many'
import End from '../End'


export default function propagate (queue, bud, value)
export function propagate (queue, bud, value)
{
if (bud.value === End)
{
Expand Down Expand Up @@ -44,9 +44,18 @@ function update_first (queue, bud, value)

bud.value = value

bud.on.emit()
/* bud.on.emit() */

queue.touch(bud)

return true
}


export function effects (queue, bud)
{
if (queue.touched_now(bud))
{
bud.on.emit()
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fluh",
"version": "0.1.0",
"version": "0.2.0-pre",

"engines":
{
Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Stream is a push, Behavior is a pull, there's a distinction.
so you are free to emit `null` or `undefined` if you really want to.
* When Bud acquire value it propagates it to *effects* and *dependents*. If Bud already has value, newly created effects
and dependents will be notified immediately with that value.
* Effects run after all dependents had been updated, so, from effects' perspective the graph's all current values change atomically and are always in a consistent state.
* All schema is sync by default, but you are free to create async operators (defer, delay…).
* Data graph is optimized for static usage, however, you can create new streams dynamically. Streams' disposal is still a task to solve (`TODO`). In static graph disposal is not an issue at all.
* Can be pure and impure, depending on usage.
Expand Down Expand Up @@ -73,6 +74,7 @@ not be touched.
* You can pass more than one value using `Many(...values)`. Additional values will be handled one after another atomically (for the whole dependent subtree) and synchronously.
* It is better to not performing side-effects inside `join`'s transformer. It is possible but
it's better to do it as an effect (`on`).
* All effects run after all propagations. From effects' perspective graph is always in a consistent state, corresponding to the most recently propagated value. Graph always changes atomically.
* `bud.map(fn)` is a shortcut for `join` deriving from a single Bud.

```js
Expand Down Expand Up @@ -110,8 +112,8 @@ var b = a.thru(delay(50))

* Attach effects to Bud by using `bud.on(fn)`.
* Effects are fired in straight-by-attach order.
* Effects run before propagating event to dependents and before effects of dependents.
* In effect you can re-`emit` values on another Bud (or even that one), but watch out for order of events and infinite loops.
* Effects on certain Bud run after propagating event to all dependents and before effects on that dependents.
* In effect you can re-`emit` values on another Bud (or even that one), but taking care of order of emits and infinite loops are on your own.
* Re-emitting will happen after current emit is done for the whole dependent subtree.

```js
Expand Down
10 changes: 5 additions & 5 deletions test/effects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ describe('effects', () =>

expect(rs).deep.eq(
[
[ 'join-b', 17 ],
[ 'join-c', 17 ],
[ 'a-1', 17 ],
[ 'a-2', 17 ],
[ 'join-b', 17 ],
[ 'b-1', 17 ],
[ 'b-2', 17 ],
[ 'join-c', 17 ],
[ 'c-1', 17 ],
[ 'c-2', 17 ],
])
Expand Down Expand Up @@ -177,17 +177,17 @@ describe('effects', () =>

expect(rs).deep.eq(
[
[ 'join-b', 17 ],
[ 'join-c', 17 ],
[ 'a-1', 17 ],
[ 'a-2', 17 ],
[ 'join-b', 17 ],
[ 'b-1', 17 ],
[ 'b-2', 17 ],
[ 'join-c', 17 ],
[ 'c-1', 17 ],
[ 'c-2', 17 ],
[ 'join-c', 1917 ],
[ 'b-1', 1917 ],
[ 'b-2', 1917 ],
[ 'join-c', 1917 ],
[ 'c-1', 1917 ],
[ 'c-2', 1917 ],
])
Expand Down

0 comments on commit 81d7b7c

Please sign in to comment.