2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2021-02-04 22:04:35 +00:00
|
|
|
* Copyright (c) 2010-2021 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2022-09-17 14:23:02 +00:00
|
|
|
* Change Date: 2026-09-17
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define(['./Parser', './ParseError'], function (Parser, ParseError) {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const DEPENDENCIES = ['Trigger', 'Command']
|
2018-08-16 22:30:59 +00:00
|
|
|
class CommandHandler {
|
|
|
|
constructor(triggers, commands) {
|
|
|
|
this.triggers = triggers
|
|
|
|
this.commands = commands
|
|
|
|
}
|
|
|
|
|
|
|
|
splitCommand(input) {
|
|
|
|
const result = Parser.Command.parse(Parser.Streams.ofString(input))
|
|
|
|
|
|
|
|
if (result.isAccepted()) {
|
|
|
|
return result.value
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
throw new ParseError('Empty trigger')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
applyCommand(command, parameterString) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const result = command
|
|
|
|
.getParameterParser()
|
|
|
|
.parse(Parser.Streams.ofString(parameterString))
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (result.isAccepted()) {
|
|
|
|
return result.value
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
throw new ParseError('Could not parse', { result, parameterString })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getTriggers() {
|
|
|
|
return this.triggers.keys()
|
|
|
|
}
|
|
|
|
|
|
|
|
getCommandByTrigger(trigger) {
|
|
|
|
const data = this.triggers.get(trigger)
|
|
|
|
|
|
|
|
if (data == null) return null
|
|
|
|
|
|
|
|
return this.getCommandByIdentifier(...data)
|
|
|
|
}
|
|
|
|
|
|
|
|
getCommandByIdentifier(packageName, identifier) {
|
|
|
|
return this.commands[`${packageName.replace(/\./g, '-')}:${identifier}`]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CommandHandler.DEPENDENCIES = DEPENDENCIES
|
|
|
|
|
|
|
|
return CommandHandler
|
2020-11-01 16:41:19 +00:00
|
|
|
})
|