From d519a10f9309ae795ad5f0a34a6f34a263e0400e Mon Sep 17 00:00:00 2001 From: jmoenig Date: Sun, 15 Dec 2019 13:32:10 +0100 Subject: [PATCH] new Snap! API: programmatically broadcast messages and optionally wait from outside Snap! --- HISTORY.md | 4 ++++ snap.html | 4 ++-- src/gui.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- src/threads.js | 4 +++- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index fbea2bd7..3adb89f9 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -5,6 +5,7 @@ * log pen vectors * export pen trails as SVG * access pen trails as SVG_Costume: new "pen vectors" reporter variant of "pen trails" + * new Snap! API: programmatically broadcast messages and optionally wait from outside Snap! * **Notable Changes:** * when creating a costume from pen trails (raster or vector) make its rotation center the position of the sprite * **Notable Fixes:** @@ -13,6 +14,9 @@ * NEW Slovak translation, thanks, Peter Lukacovic * German +### 2019-12-15 +* gui, threads: new Snap! API: programmatically broadcast messages and optionally wait from outside Snap! + ### 2019-12-13 * added direct relabelling option to pen trails blocks' context menus diff --git a/snap.html b/snap.html index 70998752..75668b5a 100755 --- a/snap.html +++ b/snap.html @@ -7,9 +7,9 @@ - + - + diff --git a/src/gui.js b/src/gui.js index 4de9e041..6cc5e18e 100644 --- a/src/gui.js +++ b/src/gui.js @@ -68,8 +68,8 @@ ToggleButtonMorph, contains, ScrollFrameMorph, StageMorph, PushButtonMorph, sb, InputFieldMorph, FrameMorph, Process, nop, SnapSerializer, ListMorph, detect, AlignmentMorph, TabMorph, Costume, MorphicPreferences, Sound, BlockMorph, ToggleMorph, InputSlotDialogMorph, ScriptsMorph, isNil, SymbolMorph, fontHeight, -BlockExportDialogMorph, BlockImportDialogMorph, SnapTranslator, localize, -List, ArgMorph, Uint8Array, HandleMorph, SVG_Costume, TableDialogMorph, +BlockExportDialogMorph, BlockImportDialogMorph, SnapTranslator, localize, List, +ArgMorph, Uint8Array, HandleMorph, SVG_Costume, TableDialogMorph, isString, CommentMorph, CommandBlockMorph, BooleanSlotMorph, RingReporterSlotMorph, BlockLabelPlaceHolderMorph, Audio, SpeechBubbleMorph, ScriptFocusMorph, XML_Element, WatcherMorph, BlockRemovalDialogMorph, saveAs, TableMorph, @@ -79,7 +79,7 @@ BlockEditorMorph, BlockDialogMorph*/ // Global stuff //////////////////////////////////////////////////////// -modules.gui = '2019-December-07'; +modules.gui = '2019-December-15'; // Declarations @@ -6058,6 +6058,50 @@ IDE_Morph.prototype.isIE = function () { return ua.indexOf("MSIE ") > -1 || ua.indexOf("Trident/") > -1; }; +// IDE_Morph external communication API - experimental +/* + programmatically trigger scripts from outside of Snap! +*/ + +IDE_Morph.prototype.broadcast = function(message, callback) { + // same as using the broadcast block - launch all scripts + // in the current project reacting to the specified message, + // if a callback is supplied wait for all processes to terminate + // then call the callback, same as using the "broadcast and wait" block + + var rcvrs = this.stage.children.concat(this.stage), + myself = this, + procs = []; + + function wait() { + if (procs.some(function (any) {return any.isRunning(); })) { + return; + } + if (callback instanceof Function) { + myself.onNextStep = function () { + callback(); + callback = null; + }; + } + } + + if (!isString(message)) { + throw new Error('message must be a String'); + } + this.stage.lastMessage = message; + rcvrs.forEach(function (sprite) { + sprite.allHatBlocksFor(message).forEach(function (block) { + procs.push(myself.stage.threads.startProcess( + block, + sprite, + myself.stage.isThreadSafe, + false, + callback instanceof Function ? wait : null + )); + }); + }); +}; + // ProjectDialogMorph //////////////////////////////////////////////////// // ProjectDialogMorph inherits from DialogBoxMorph: diff --git a/src/threads.js b/src/threads.js index dc3f26ee..cf753327 100644 --- a/src/threads.js +++ b/src/threads.js @@ -61,7 +61,7 @@ StageMorph, SpriteMorph, StagePrompterMorph, Note, modules, isString, copy, isNil, WatcherMorph, List, ListWatcherMorph, alert, console, TableMorph, Color, TableFrameMorph, ColorSlotMorph, isSnapObject, newCanvas, Symbol, SVG_Costume*/ -modules.threads = '2019-December-05'; +modules.threads = '2019-December-15'; var ThreadManager; var Process; @@ -398,6 +398,8 @@ ThreadManager.prototype.removeTerminatedProcesses = function () { ); } } + } else if (proc.onComplete instanceof Function) { + proc.onComplete(); } } else { remaining.push(proc);