-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edad433
commit 0cffb1a
Showing
2 changed files
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import events from 'events'; | ||
import { sha256 } from './utils'; | ||
|
||
const eventEmitter = new events.EventEmitter(); | ||
eventEmitter.setMaxListeners(1000); // https://stackoverflow.com/a/26176922 | ||
|
||
export default async function serve(id, action, args) { | ||
const key = sha256(id); | ||
return new Promise(async (resolve, reject) => { | ||
eventEmitter.once(key, (data) => (data.error ? reject(data.e) : resolve(data))); | ||
if (eventEmitter.listenerCount(key) === 1) { | ||
try { | ||
eventEmitter.emit(key, await action(...args)); | ||
} catch (e) { | ||
eventEmitter.emit(key, { error: true, e }); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters