<div dir="ltr">Great stuff!<br><br>I'm a bit undecided about the API... I'll try to let it go through my head a bit more, until you post a pull request :)<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On 3 September 2013 16:52, Manuel Quiñones <span dir="ltr"><<a href="mailto:manuq@laptop.org" target="_blank">manuq@laptop.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm back into this.  After discussing with Daniel some weeks ago, we<br>
agreed that the activity is the one who knows if it needs to store,<br>
not the toolkit.  So we prefer to let the activity developer do the<br>
storing, instead of doing when the activity goes to the background.<br>
<br>
Also the API I have proposed at that time was only for text data.<br>
Storing in web activities is a bit more complicated because there is<br>
no file descriptor to write to.  On the other hand, most webapps store<br>
text in JSON format<br>
<br>
So now I'm proposing a helper class called JSONStore that uses the<br>
standard localSorage.  I made it work in Get Things Done activity.  I<br>
will send a PR to add it to sugar-web.  Here it is:<br>
<br>
<a href="https://github.com/manuq/gtd-activity/blob/master/js/jsonstore.js" target="_blank">https://github.com/manuq/gtd-activity/blob/master/js/jsonstore.js</a><br>
<br>
An adventage of using localStorage is that it persists in a normal<br>
browser as well.  See <a href="http://manuq.github.io/gtd-activity/" target="_blank">http://manuq.github.io/gtd-activity/</a><br>
<br>
The usage is like this:<br>
<br>
myStore = jsonstore.JSONStore();<br>
<br>
myReadyCallback = function () {};<br>
window.addEventListener('storeReady', myReadyCallback);<br>
<br>
var value = myStore.read('key'); // read<br>
myStore.write('key', newValue); // write<br>
<br>
// Or use localStorage directly, and then call save():<br>
<br>
var value = localStorage['key'];<br>
localStorage['key'] = newValue;<br>
myStore.save();<br>
<br>
<br>
2013/8/16 Daniel Narvaez <<a href="mailto:dwnarvaez@gmail.com">dwnarvaez@gmail.com</a>>:<br>
<div class="HOEnZb"><div class="h5">> Hey,<br>
><br>
> It seems be have two (mostly) independent questions here<br>
><br>
> - How do we notify an activity of certain global events, being them<br>
> onPause/onStop or onRead/onWrite.<br>
><br>
> If we want them to be passed in activity.setup() I think we should use an<br>
> object, like {"onSomething": ..., "onSomethingElse":..}. That will allow to<br>
> subscribe only to some of the available events. With two events that would<br>
> probably be overkill but if nothing else I think onStart/onPause/onStop<br>
> should be supported, even if we also have onRead and onWrite. They are<br>
> needed for more than writing to the datastore.<br>
><br>
> Though, any reason to not use custom DOM events? This seems exactly the use<br>
> case they are designed for.<br>
> <a href="http://www.w3.org/TR/DOM-Level-3-Events/#extending_events-Custom_Events" target="_blank">http://www.w3.org/TR/DOM-Level-3-Events/#extending_events-Custom_Events</a><br>
><br>
> - Should we have onRead and onWrite events.<br>
><br>
> What you are proposing is a bit different from what you have implemented. In<br>
> your patch onWrite does only a couple of things<br>
><br>
> 1 Proxy both onPause and onStop to the same write method.<br>
> 2 Close the activity in onStop.<br>
><br>
> I think 1 is not enough to justify onWrite existence, it's easy enough for<br>
> the concrete activity to do it if wanted.<br>
> About 2, I think the concrete activity needs more control on when it is shut<br>
> down. It might want to do so after having written some file outside the<br>
> journal, for example.<br>
><br>
> Now, you are proposing that we pass metadata in both read and write,<br>
> similarly to the gtk API. But what about the data? I don't see exactly where<br>
> you are going with this, so it would helpful if you could sketch out that<br>
> API.<br>
><br>
><br>
> That was a bit long, to summarize:<br>
><br>
> * I think onStart/onPause/onStop should be there in any case.<br>
> * On the top of those, your current onWrite implementation adds too little<br>
> to be justified.<br>
> * I tend to think we should use custom DOM events here instead of callbacks,<br>
> if possible.<br>
><br>
> On 16 August 2013 14:01, Manuel Quiñones <<a href="mailto:manuq@laptop.org">manuq@laptop.org</a>> wrote:<br>
>><br>
>> This is one of the most important APIs we will have, so I would<br>
>> like to open discussion on it.<br>
>><br>
>> In GTK activities, the activity developer subclass Activity, and<br>
>> override read() and write() methods.<br>
>><br>
>> In web activities, we don't have that kind of hierarchy.  Instead, the<br>
>> activity developer imports 'activity/activity' module, and calls<br>
>> activity.setup() .  Then she/he can use activity.getDatastoreObject()<br>
>> to read and write.<br>
>><br>
>> In GTK activities, 'read' is called at startup and 'write' is called<br>
>> when the activity goes to the background or when it stops.  Recently,<br>
>> I added in sugar-web those notifications, with the intention to make<br>
>> web activities behave like GTK ones.<br>
>><br>
>> I provided an initial patch and dnarvaez proposed another option:<br>
>> <a href="https://github.com/sugarlabs/sugar-web/pull/74" target="_blank">https://github.com/sugarlabs/sugar-web/pull/74</a><br>
>><br>
>> So we can:<br>
>><br>
>> - Let web developers connect to the 'pause' and 'stop' notifications,<br>
>>   and use the activity datastore object directly. Pro: is more<br>
>>   flexible.  Con: more work in the activity side: error handling,<br>
>>   datastore object handling.<br>
>><br>
>> - Subscribe 'read' and 'write' functions in activity.setup() .  They<br>
>>   will be called as expected, like in GTK.  'read' will have a JSON<br>
>>   string as argument.  'write' must return a JSON string.  Pro:<br>
>>   simpler approach.  Con: less flexibility in the activity side.  A<br>
>>   mockup can be found in this comment:<br>
>><br>
>> <a href="https://github.com/manuq/sugar-web/commit/3c2420f59c9aa8df44d2c0d639fe37b4af63f9fb#commitcomment-3872770" target="_blank">https://github.com/manuq/sugar-web/commit/3c2420f59c9aa8df44d2c0d639fe37b4af63f9fb#commitcomment-3872770</a><br>

>><br>
>> Thoughts?  Any other ideas?<br>
>><br>
>><br>
>> --<br>
>> .. manuq ..<br>
>><br>
>> _______________________________________________<br>
>> Sugar-devel mailing list<br>
>> <a href="mailto:Sugar-devel@lists.sugarlabs.org">Sugar-devel@lists.sugarlabs.org</a><br>
>> <a href="http://lists.sugarlabs.org/listinfo/sugar-devel" target="_blank">http://lists.sugarlabs.org/listinfo/sugar-devel</a><br>
><br>
><br>
><br>
><br>
> --<br>
> Daniel Narvaez<br>
<br>
<br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
.. manuq ..<br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>Daniel Narvaez<br>
</div>