Skip to content

Instantly share code, notes, and snippets.

@hardboiled
Created June 23, 2017 19:55
Show Gist options
  • Save hardboiled/9a0665d3d66c8af9231f7adde329ab57 to your computer and use it in GitHub Desktop.
Save hardboiled/9a0665d3d66c8af9231f7adde329ab57 to your computer and use it in GitHub Desktop.
//usage of interface
clientInterfaceInstance.trigger('window-focus', screen.width, screen.height)
//definitions
class ClientInterface {
initialize(client) {
this.client = client;
}
//public function
trigger(event) {
argsFromEvent = Array.prototype.slice.call(arguments, 1)
functionName = this.convertEventToCamelCase(event)
if (this.client[event] && (typeof this.client[event] === 'function')) {
this.client[event].apply(this, argsFromEvent);
} else {
//nothing happens, since an event wasn't mapped to a function name.
}
}
//private functions
convertEventToCamelCase(event) {
event.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
}
}
class BaseClient {
someUXinteraction(e) {
//common code here
}
}
class AndroidClient extends BaseClient {
windowFocus(width, height) {
//define android specific code here
}
}
class IosClient extends BaseClient {
windowFocus(width, height) {
//define android specific code here
}
}
class WebClient extends BaseClient {
//doesn't need windowFocus, so leave undefined
someUXinteraction(e) {
//add Web specific code here, since it needs extra animations based off browser type
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment