Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit ca48e4a

Browse files
committed
Merge pull request #12 from runrev/peter-b/resume-hooks
[emscripten] Add a mechanism for hooking into engine resume
2 parents b86e0e7 + 4317d5a commit ca48e4a

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

engine/src/em-async.js

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*-Javascript-*-
22
3-
Copyright (C) 2003-2013 Runtime Revolution Ltd.
3+
Copyright (C) 2015 Runtime Revolution Ltd.
44
55
This file is part of LiveCode.
66
@@ -29,6 +29,9 @@ mergeInto(LibraryManager.library, {
2929
// loop.
3030
_continuation: null,
3131

32+
// List of hooks to be run before every resume
33+
_hooks: [],
34+
3235
// List of callbacks to be run just before resuming the main
3336
// loop.
3437
_preResume: [],
@@ -46,6 +49,13 @@ mergeInto(LibraryManager.library, {
4649
return;
4750
}
4851
LiveCodeAsync._initialised = true;
52+
53+
LiveCodeAsync.addResumeHook(function () {
54+
// Run pre-resume callbacks
55+
LiveCodeAsync._preResume.forEach(function (c) { c(); });
56+
// Reset pre-resume callback list
57+
LiveCodeAsync._preResume = [];
58+
});
4959
},
5060

5161
_resumeTimeout: function() {
@@ -85,12 +95,8 @@ mergeInto(LibraryManager.library, {
8595
// of the closure determines the apparent return value of
8696
// EmterpreterAsync.handle().
8797
resume(function (){
88-
// Run pre-resume callbacks
89-
var queueLength = LiveCodeAsync._preResume.length;
90-
for (var i = 0; i < queueLength; i++) {
91-
LiveCodeAsync._preResume[i]();
92-
}
93-
LiveCodeAsync._preResume = []; // Reset pre-resume callback list
98+
// Run pre-resume hooks
99+
LiveCodeAsync._runHooks()
94100

95101
return !LiveCodeAsync.isTimedOut();
96102
});
@@ -154,6 +160,44 @@ mergeInto(LibraryManager.library, {
154160
LiveCodeAsync._preResume.push(delayed);
155161
}
156162
},
163+
164+
// Run pre-resume hooks.
165+
_runHooks: function() {
166+
LiveCodeAsync._hooks.forEach(function (h) { h(); });
167+
},
168+
169+
// Register a closure to be run before every resume. If
170+
// <callback> is already in the list of hooks, does nothing.
171+
//
172+
// callback: closure taking no arguments.
173+
addResumeHook: function(callback) {
174+
LiveCodeAsync._ensureInit();
175+
176+
console.log('addResumeHook');
177+
178+
// Make sure the same hook doesn't get registered twice
179+
if (LiveCodeAsync._hooks.some(function (h) {
180+
return (h === callback);
181+
})) {
182+
return;
183+
}
184+
185+
LiveCodeAsync._hooks.push(callback);
186+
},
187+
188+
// Remove a closure from the list of pre-resume hooks. If
189+
// <callback> is not in the list of hooks, does nothing.
190+
removeResumeHook: function(callback) {
191+
LiveCodeAsync._ensureInit();
192+
193+
// Find and remove the specified hook
194+
var numHooks = LiveCodeAsync._hooks.length;
195+
for (var i = 0; i < numHooks; i++) {
196+
if (LiveCodeAsync._hooks[i] === callback) {
197+
LiveCodeAsync._hooks.splice(i, 1);
198+
}
199+
}
200+
},
157201
},
158202

159203
// Yield for up to <timeout> seconds

0 commit comments

Comments
 (0)