Skip to content

need effect cleanup functions, and the ability to nest effects #4

Description

@trusktr

First, check out the Solid.js example:

https://codepen.io/trusktr/pen/MYgMNQz/be794bc15e95031af67e36d53bd35812?editors=0010

Now check out this fluid example:

import {state, effect, memo} from '@fluid/core'

const bool = state(true)
setInterval(() => bool.val = !bool.val, 2000)

console.log('start ----------------------------- ')

effect(() => {
	if (bool.val) {
		const count1 = state(0)
		const inter = setInterval(() => count1.val++, 1000)
		
		effect(() => {
			count1.val
			console.log('a')		
		})
		
		// missing cleanup API
	} else {
		const count2 = state(0)
		setInterval(() => count2.val++, 100)
		
		effect(() => {
			count2.val
			console.log('b')
		})
		
		// missing cleanup API
	}
})

CodePen: https://codepen.io/trusktr/pen/yyBdmvx/52cd0a414dc668f72ebb6c6bdb42d656?editors=0010

When fluid changes branches, we need a way to create nested effects and to clean up any previous processes. Nested effects should automatically clean themselves up so they do not keep re-running, plus a cleanup API allows us to cancel any non-effect processes such as setInterval that the library would have no idea is running.

This is a key part that makes the "signals and effects" pattern truly wonderful (for example with this we can create robust async processes to replace difficult Promise patterns, but that example is not shown here).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions