Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiable env #25

Merged
merged 2 commits into from
Mar 2, 2019
Merged

Instantiable env #25

merged 2 commits into from
Mar 2, 2019

Conversation

developit
Copy link
Owner

@developit developit commented Feb 1, 2019

This PR builds on #24. In #24, DOM classes/prototypes became shared across all Documents created via undom():

import undom from 'undom';

const doc1 = undom();
const doc2 = undom();
doc1.defaultView.Node === doc1.defaultView.Node  // true

With this PR, undom() continues to return shared constructors. However, a new undom.env() method returns a new factory function (akin to undom()) that can be called to create documents with a new set of constructors:

import undom from 'undom';

// as with #24, documents default to the shared environment:
const doc1 = undom();
const doc2 = undom();
doc1.defaultView.Node === doc1.defaultView.Node;

// but now we have an escape hatch:
const env = undom.env();
const doc3 = env();
doc3.defaultView.Node === doc1.defaultView.Node;  // FALSE!

This means we still encourage using the shared globals, but allow creating a new set of them if necessary.

It also opens up an interesting option for plugins, which were proposed a while ago, but would have been difficult to implement.

import undom from 'undom';
import serialization from 'undom/serialization';
import shadow from 'undom/shadow';

function applyPlugin(dom, plugin) {
  plugin(typeof dom==='function' ? dom() : dom);
}

// create a clean environment
const env = undom.env();
applyPlugin(env, serialization);
applyPlugin(env, shadow);

// now all documents created for the environment have those plugins:
const doc1 = env();
const doc2 = env();

// fun part - this works with the default environment too:
applyPlugin(undom, serialization);
const doc3 = undom(); // has the plugin

…ction that returns a new undom() factory with new prototypes.
@developit developit changed the base branch from master to mutation-observers February 1, 2019 22:17
@developit developit changed the base branch from mutation-observers to master February 1, 2019 22:17
Copy link
Contributor

@bmeurer bmeurer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

@developit developit merged commit e425075 into master Mar 2, 2019
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.

None yet

2 participants