Month: May 2017

  • EventEmitter

    Our EventEmitter in PhantomSQL is based on NodeJS version so they should be compatible. Here are couple examples on how to use the emitter.

    Basic usage of registering and listening to an event.

    "use strict";
    
    const {EventEmitter} = require('events');
    
    // Dump all the args
    em.on('hello-event', (...arg)=> {console.info("Hello event handler : " + arg)});
    // Handler without args
    em.on('hello-event', ()=> {console.info("Another handler")});
    // passed in arguments
    em.on('hello-event', (id, val)=> {console.info("Handler :"+id +", "+ val)});
    
    // emit event
    em.emit('hello-event', 123, 'ABC');
    

    A more typical example would be to extend via prototype.

    "use strict";
    const {EventEmitter} = require('events');
    
    function HelloService()
    {
    	// Extends via prototype
    	Object.setPrototypeOf(HelloService.prototype, EventEmitter.prototype);
    	
    	this.hello = function()
    	{
    		console.info("Hello service called");
    		this.emit('hello');
    	}
    }  
    
    const service = new HelloService();
    
    service.on('hello', ()=> {console.info("Hello Handler called")});
    service.hello();
    
  • Creating Javascript accessible object from C++ / CEF

    Example with Chromium Embedded Framework (CEF) on how to create an object in C++ and make it accessible via Javascript.

    console.inof(api)
    Object {ready: true, version: "psql.0.0.1", info: Object, getVersion: function}
    
    void ExtractEngineApp::OnContextCreated(
    	CefRefPtr browser, 
    	CefRefPtr frame, 
    	CefRefPtr context)
    {
    
    	auto info = CefV8Value::CreateObject(NULL, NULL);
    	info->SetValue("major", CefV8Value::CreateString("0"), V8_PROPERTY_ATTRIBUTE_READONLY);
    	info->SetValue("minor", CefV8Value::CreateString("1"), V8_PROPERTY_ATTRIBUTE_READONLY);
    	
    	auto global = context->GetGlobal();
    	auto api = CefV8Value::CreateObject(NULL, NULL);
    
    	global->SetValue("api", api, V8_PROPERTY_ATTRIBUTE_NONE);
    
    	auto fun = CefV8Value::CreateFunction("getVersion", new engine:: PhantomExtensionHandler(this));
    	api->SetValue("getVersion", fun, V8_PROPERTY_ATTRIBUTE_NONE);
    	
    	// Readonly properties
    	api->SetValue("ready", CefV8Value::CreateBool(true), V8_PROPERTY_ATTRIBUTE_READONLY);
    	api->SetValue("version", CefV8Value::CreateString("psql.0.0.1"), V8_PROPERTY_ATTRIBUTE_READONLY);
    
    	// Readonly Object access
    	api->SetValue("info", info, V8_PROPERTY_ATTRIBUTE_READONLY);
    }
    
    
    class PhantomExtensionHandler : public CefV8Handler
    	{
    	public:
    		explicit PhantomExtensionHandler(CefRefPtr client_app)
    			: client_app(client_app)
    			, messageId(0)
    		{
    
    		}
    
    		virtual bool Execute(const CefString& name,
    			CefRefPtr object,
    			const CefV8ValueList& arguments,
    			CefRefPtr& retval,
    			CefString& exception)
    		{
    			if (name == "getVersion")
    			{
    				retval = CefV8Value::CreateString("Version(SemVer) # 0.0.1");
    			}
    
    			return true;
    		}
    
    	private:
    		CefRefPtr client_app;
    		int32 messageId;
    
    		IMPLEMENT_REFCOUNTING(PhantomExtensionHandler);
    	};