Skip to content

Conversation

@agubler
Copy link
Member

@agubler agubler commented May 31, 2019

Type: feature

The following has been addressed in the PR:

Description:

Adds two new core middleware implementations, destroy and getDom

destroy

Registers a destroy handle that is called when a widget is removed.

const factory = create({ destroy });

const MyWidget = factory(({ middleware: { destroy } }) => {
	destroy(() => {
		// do something when the widget is removed
	});
	return v('div', { key: 'div' });
});

Limited to registering a single handle per unique middleware, for use-cases that require conditional or multiple handles through out the lifetime of a widget the user is required to manage this within their own middleware.

const factory = create({ destroy });

const myMiddleware = factory(({ middleware: { destroy } }) => {
	const destroyFunctions: (() => void)[] = [];
	destroy(() => {
		let handle: () => void | undefined;
		while (cb = destroyFunctions.pop()) {
			cb();
		}
	});
	return {
		foo() {
			// add destroy to `destroyFunctions` array
		}
	}
});

If destroy is called more than once for a widget or middleware it will be ignored.

node

Returns the DOM node for the key of a VNode in a widget.

const factory = create({ node });

const myMiddleware = factory(({ middleware: { node } }) => {
	return {
		doSomething(key: string | number) {
			const node = node.get(key);
			// do something with the node, like return dimensions.
	};
});

Related to #352

@agubler agubler force-pushed the destroy-getDom-middleware branch from 0b78e7c to b09883c Compare May 31, 2019 12:49
@agubler agubler changed the title destroy and getDom Middleware destroy and node Middleware Jun 4, 2019
@agubler
Copy link
Member Author

agubler commented Jun 4, 2019

@matt-gadd updated from getDom to node for the middleware that returns the DOM node for a particular key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants