Comments (3)
Unfortunately I have no idea about web workers. This answer suggests that your suggested code change won't work very well. And I couldn't find out what that self
is. I'll leave this open until somebody provides a reliable alternative.
from msgpack.js.
In the script of Web Workers, the self
property returns WorkerGlobalScope
object (the Worker
global scope).
I reference the solution from the other library:
// Environment detection
if (
typeof module === "object" && module && typeof module.exports === "object"
) {
// Node.js
module.exports = msgpack;
} else {
// Global object
var g;
if (typeof window !== "undefined") {
g = window;
} else if (typeof global !== "undefined") {
g = global;
} else if (typeof self !== "undefined") {
g = self;
} else {
g = this;
}
g[g.msgpackJsName || "msgpack"] = msgpack;
}
from msgpack.js.
https://caniuse.com/?search=globalThis
https://mathiasbynens.be/notes/globalthis has discussion on polyfills
Based on the above, pass the globalThis into your IIFE like this:
(function (_global) { //bind globalThis to _global
...
// Environment detection
if (typeof module === "object" && module && typeof module.exports === "object") {
// Node.js
module.exports = msgpack;
}
else if (_global) {
// Global object
_global[_global.msgpackJsName || "msgpack"] = msgpack;
}
)(globalThis || this || self || window);
Notes:
- Assumes no one bound something unexpected to
globalThis
from the context you call the IIFE. - In most modern JS environments
globalThis
will be defined and that's what will be used - If the JS environment is older, first fallback is
this
.- There are edge cases where
this
is not defined, so the next fallbacks areself || window
- There are edge cases where
self || window
is undefined... or someone could have donevar self=something
so first fallback isthis
. - It is possible someone imported or called your IIFE from something other than the global context. But is this likely? I can't imagine any use case where someone would do this...
- There are edge cases where
- Finally, if
this
was undefined from the context msgpack.js was imported, the fallbacksself || window
will work if some unexpected value wasn't bound to them. It's a risk that someone may have definedvar self=something
in the context of the import, but it would be unusual; especially if the previousglobalThis
andthis
were not defined.
There won't be any perfect solution to polyfill globalThis
... but this is decent I think.
Another option is to not define global.mspack
. and instead just support commonJS and expect browsers to use an applicable module loader that supports commonjs.
from msgpack.js.
Related Issues (20)
- Any plan to publish to NPM as a module? HOT 5
- has a bug HOT 4
- Does it work with System.Decimal? HOT 1
- Date deserialisation doesn't cope with pre-epoch dates HOT 2
- Copy to TypeScript HOT 2
- Encoding in python and decoding in javascript HOT 5
- Wrong float deserialization from typed arrays with non-zero byteOffset
- The example of javascript client and server HOT 1
- No "default" case in 'append' HOT 2
- Doesn't handle uint64 particularly well HOT 7
- Add .d.ts typings file HOT 2
- Skip serializing properties with `undefined` values HOT 1
- DataView read wrong value on msgpack deserialized data HOT 3
- Options undocumented HOT 1
- No support for Lz4 compression as C# package provide HOT 1
- Proposal: converting Integer with DataView
- Callback for ext
- Serializer also serializes properties in object prototype HOT 1
- Wrong array serialization HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from msgpack.js.