Skip to content

Commit

Permalink
Core: Provide a way to load Prism from inside a Worker without listen…
Browse files Browse the repository at this point in the history
…ing to messages.
  • Loading branch information
Golmote committed Nov 5, 2017
1 parent c33be19 commit e68fc1c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
28 changes: 16 additions & 12 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var uniqueId = 0;

var _ = _self.Prism = {
manual: _self.Prism && _self.Prism.manual,
disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
util: {
encode: function (tokens) {
if (tokens instanceof Token) {
Expand Down Expand Up @@ -481,18 +482,21 @@ if (!_self.document) {
// in Node.js
return _self.Prism;
}
// In worker
_self.addEventListener('message', function(evt) {
var message = JSON.parse(evt.data),
lang = message.language,
code = message.code,
immediateClose = message.immediateClose;

_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
}, false);

if (!_.disableWorkerMessageHandler) {
// In worker
_self.addEventListener('message', function (evt) {
var message = JSON.parse(evt.data),
lang = message.language,
code = message.code,
immediateClose = message.immediateClose;

_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
}, false);
}

return _self.Prism;
}
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var uniqueId = 0;

var _ = _self.Prism = {
manual: _self.Prism && _self.Prism.manual,
disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
util: {
encode: function (tokens) {
if (tokens instanceof Token) {
Expand Down Expand Up @@ -486,18 +487,21 @@ if (!_self.document) {
// in Node.js
return _self.Prism;
}
// In worker
_self.addEventListener('message', function(evt) {
var message = JSON.parse(evt.data),
lang = message.language,
code = message.code,
immediateClose = message.immediateClose;

_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
}, false);

if (!_.disableWorkerMessageHandler) {
// In worker
_self.addEventListener('message', function (evt) {
var message = JSON.parse(evt.data),
lang = message.language,
code = message.code,
immediateClose = message.immediateClose;

_self.postMessage(_.highlight(code, _.languages[lang], lang));
if (immediateClose) {
_self.close();
}
}, false);
}

return _self.Prism;
}
Expand Down

0 comments on commit e68fc1c

Please sign in to comment.