$Pwa.map=new Map();//pwa管理

$Pwa.init=function(js)
{
	if(!js)return
	if(navigator.serviceWorker != null)
	{
		navigator.serviceWorker.register(js)
		.then
		(
			function(registartion)
			{
				$M.info('【sw】',registartion.scope)
			}
		)
		.catch
		(
			function(err)
			{
				$M.info('【sw】','不支持')
			}
		)
	}

	navigator.serviceWorker && navigator.serviceWorker.addEventListener
	(
		'message', function (event) 
		{
		  var json
		  try 
		  {
			json=JSON.parse(event.data)
		  } 
		  catch (error) 
		  {
			  $M.info('【sw】消息解析错误',error)
			  json={}
		  }
		  $Pwa.listen(json.method,json.data)
		}
	);
}

$Pwa.send=function(method,data)
{
	if(!data)return
	var _data={method:method,data:data}
	if(data && data instanceof Object)_data=JSON.stringify(_data)
	navigator.serviceWorker.controller && navigator.serviceWorker.controller.postMessage(_data);
}

$Pwa.listen=function(method,fun)
{
	if(fun && method)
	{
		if(fun instanceof Function)
		{
			$Pwa.map.set(method,fun)
		}
		else
		{
			$Pwa.map.has(method) && $Pwa.map.get(method)(fun)
		}
	}
}
