GoogleChromeLabs / tasklets

176 stars 17 forks source link

Clarify when globals are created #10

Open jakearchibald opened 7 years ago

jakearchibald commented 7 years ago

Assuming global state may be retained in a tasklet, clarify the scope of the global.

Is it:

// example.com/yo
const tasklet1 = await tasklet.addModule('//other-origin/tasklet.js');

await tasklet1.increment(); // 1
await tasklet1.increment(); // 2

const tasklet2 = await tasklet.addModule('//other-origin/tasklet.js');
await tasklet2.increment(); // ?
// Per instance: 1
// Per script URL + env settings object: 2
// Per script URL + host origin: 2
// Per script URL: 2

Without closing example.com/yo:

// example.com/foo
const tasklet1 = await tasklet.addModule('//other-origin/tasklet.js');

await tasklet1.increment(); // 1
// Per instance: 1
// Per script URL + env settings object: 1
// Per script URL + host origin: 3
// Per script URL: 3

Without closing example.com/yo or example.com/foo:

// otherorigin.example.com/foo
const tasklet1 = await tasklet.addModule('//other-origin/tasklet.js');

await tasklet1.increment(); // 1
// Per instance: 1
// Per script URL + env settings object: 1
// Per script URL + host origin: 1
// Per script URL: 4
surma commented 7 years ago

@jakearchibald brought up a good point in person that sometimes you might want to reference the same tasklet instance. I don’t think our current proxies are transferrable, so there’s currently a way to reference (and interact with) the same tasklet instance from both the ServiceWorker and the main thread.

I think it’s worth considering to return an existing tasklet instance if the same URL + host origin is used (similar to modules).