Skip to content

Instantiable env - #25

Merged
developit merged 2 commits into
masterfrom
instantiable-env
Mar 2, 2019
Merged

Instantiable env#25
developit merged 2 commits into
masterfrom
instantiable-env

Conversation

@developit

@developitdevelopit commented Feb 1, 2019

Copy link
Copy Markdown
Owner

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

importundomfrom'undom';constdoc1=undom();constdoc2=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:

importundomfrom'undom';// as with #24, documents default to the shared environment:constdoc1=undom();constdoc2=undom();doc1.defaultView.Node===doc1.defaultView.Node;// but now we have an escape hatch:constenv=undom.env();constdoc3=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.

importundomfrom'undom';importserializationfrom'undom/serialization';importshadowfrom'undom/shadow';functionapplyPlugin(dom,plugin){plugin(typeofdom==='function' ? dom() : dom);}// create a clean environmentconstenv=undom.env();applyPlugin(env,serialization);applyPlugin(env,shadow);// now all documents created for the environment have those plugins:constdoc1=env();constdoc2=env();// fun part - this works with the default environment too:applyPlugin(undom,serialization);constdoc3=undom();// has the plugin

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

@bmeurerbmeurer left a comment

Copy link
Copy Markdown
Contributor

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 masterMar 2, 2019
Sign up for freeto 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

@developit@bmeurer